Thread: [Dev-C++] dynamic allocation and structs
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Scott W. <se...@al...> - 2002-10-24 12:18:03
|
Hi,
I'm working in C (no C++ please). Got a couple of questions related to
dynamic allocation and in particular, dynamically allocating an array of
structs.
First, what is the duration of memory allocated by malloc (etc.)? In
particular, I want to dynamically allocate/de-allocate several global
variables within a single subroutine called from main and so want to be sure
that the allocated memory exists beyond the scope of that subroutine.
Second, it is my understanding that it is perfectly acceptable to have an
array of structs. E.g.,
struct mystruct {
double var1;
long var2;
etc...
}
. . .
struct mystruct data[100];
Is this correct? If so, what then is the calling sequence to dynamically
allocate an array of structs, and, in particularly, make the result
available globally?
Thanks for your help (& sorry if this is naive).
Scott Williams
|
|
From: Per W. <pw...@ia...> - 2002-10-24 13:12:52
|
// Alternative 1, array of entries.
mystruct *myarray;
void func(int count) {
int i;
myarray = (mystruct*)malloc(count*sizeof(mystruct));
for (i = 0; i < count; i++) {
myarray[i].xx = yy;
}
}
// Alternative 2, array of pointers to entries.
mystruct **myarray;
void func(int count) {
int i;
myarray = (mystruct**)malloc(count*sizeof(*mystruct));
for (i = 0; i < count; i++) {
myarray[i] = (mystruct*)malloc(sizeof(mystruct));
myarray[i]->xx = yy;
}
}
/Per W
On Thu, 24 Oct 2002, Scott Williams wrote:
> Hi,
>
> I'm working in C (no C++ please). Got a couple of questions related to
> dynamic allocation and in particular, dynamically allocating an array of
> structs.
>
> First, what is the duration of memory allocated by malloc (etc.)? In
> particular, I want to dynamically allocate/de-allocate several global
> variables within a single subroutine called from main and so want to be sure
> that the allocated memory exists beyond the scope of that subroutine.
>
> Second, it is my understanding that it is perfectly acceptable to have an
> array of structs. E.g.,
>
> struct mystruct {
> double var1;
> long var2;
> etc...
> }
>
> . . .
>
> struct mystruct data[100];
>
> Is this correct? If so, what then is the calling sequence to dynamically
> allocate an array of structs, and, in particularly, make the result
> available globally?
>
> Thanks for your help (& sorry if this is naive).
>
> Scott Williams
>
>
>
> -------------------------------------------------------
> This sf.net email is sponsored by: Influence the future
> of Java(TM) technology. Join the Java Community
> Process(SM) (JCP(SM)) program now.
> http://ad.doubleclick.net/clk;4729346;7592162;s?http://www.sun.com/javavote
> _______________________________________________
> Dev-cpp-users mailing list
> Dev...@li...
> TO UNSUBSCRIBE: http://www.noicys.cjb.net/devcpp/ub.htm
> https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
>
|
|
From: Ioannis V. <no...@ho...> - 2002-10-24 20:57:27
|
> -----Original Message-----
> From: dev...@li...
> [mailto:dev...@li...] On Behalf
> Of Per Westermark
> Sent: Thursday, October 24, 2002 4:13 PM
> To: Scott Williams
> Cc: dev...@li...
> Subject: Re: [Dev-C++] dynamic allocation and structs
>
>
>
> // Alternative 1, array of entries.
> mystruct *myarray;
> void func(int count) {
> int i;
> myarray = (mystruct*)malloc(count*sizeof(mystruct));
> for (i = 0; i < count; i++) {
> myarray[i].xx = yy;
> }
> }
>
> // Alternative 2, array of pointers to entries.
> mystruct **myarray;
> void func(int count) {
> int i;
> myarray = (mystruct**)malloc(count*sizeof(*mystruct));
> for (i = 0; i < count; i++) {
> myarray[i] = (mystruct*)malloc(sizeof(mystruct));
> myarray[i]->xx = yy;
> }
> }
The Alternative 2 is wrong. You use a ** pointer and then you assign
values as if it is a *.
A working example of what you are trying to do:
#include <stdlib.h>
#include <stdio.h>
int main()
{
struct mystruct
{
int x;
};
struct mystruct **myarray;
int i, j;
/* sizeof(*myarray)==sizeof(struct mystruct *) */
myarray=malloc(100*sizeof(*myarray));
/* sizeof(**myarray)==sizeof(struct mystruct).
A 200x100 array on the free store is created */
for(i=0; i<100; i++)
myarray[i]=malloc(200*sizeof(**myarray));
/* Do something */
for(i=0; i<100; i++)
for(j=0; j<200; j++)
{
myarray[i][j].x=i+j;
printf("%d\n", myarray[i][j].x);
}
for(i=0; i<100; i++)
free(myarray[i]);
free(myarray);
return 0;
}
Alternative 3 (which must be prefered if possible instead of A-2):
#include <stdlib.h>
int main()
{
struct mystruct
{
int x;
};
struct mystruct (*myarray)[100];
/* sizeof(*myarray)==sizeof(struct mystruct).
A 200x100 array on the free store is created */
myarray=malloc(200*sizeof(*myarray));
/* Do something */
free(myarray);
return 0;
}
Ioannis
* Ioannis Vranos
* Programming pages: http://www.noicys.freeurl.com
* Alternative URL: http://run.to/noicys
|
|
From: Per W. <pw...@ia...> - 2002-10-24 22:02:19
|
Well Ioannis, I did manage to get a star on the wrong side of the struct
type name, see inserted correction below, so the example was slightly
broken. However, the example was not a total goose chase try at a
two-dimensional matrix of entries, but an example of creating a
one-dimensional matrix (a vector) of pointers to single entries, which was
exactly what the heading suggested "array of pointers to entries". If the
code wasn't obvious, a hint to that fact should have been that I only
used a single for loop to assign values to the entries.
One reason to use alternative two, a vector of pointers to elements
instead of a vector of elements, is that sorting of the vector only
require changing the place of the pointers, instead of copying the full
structures.
Another reason is that it might be used to create a sparse matrix, where
the vector might be very long, but only some index values of the vector
will have an allocated entry.
A third reason is that a vector of pointers might be to store different
types of structs in the same vector, as in:
enum {
CIRCLE,
LINE,
BOX
};
struct Circle {
int type;
int x,y;
int radius;
};
struct Line {
int type;
int x1,y1,x2,y2;
}
struct Box {
int type;
int x1,y1,x2,y2;
};
void fill_array() {
Circle *pCircle;
Line *pLine;
Box *pBox;
int item_count,i;
item_count = 0;
pCircle = (Circle*)malloc(sizeof(Circle));
pCircle->type = CIRCLE;
pCircle->x = 10;
pCircle->y = 20;
pCircle->radius = 5;
myarray[item_count++] = pCircle;
pBox = (Box*)malloc(sizeof(Box));
pBox->type = BOX;
pBox->x1 = 15;
pBox->y1 = 2;
pBox->x2 = 18;
pBox->y2 = 5;
myarray[item_count++] = pBox;
...
// Draw items.
for (i = 0; i < item_count; i++) {
if (myarray[i]) {
switch (myarray[i]->type) {
case CIRCLE:
pCircle = (Circle*)myarray[i];
// Draw circle
break;
case BOX:
pBox = (Box*)myarray[i];
// Draw box
break;
case LINE:
pLine = (Line*)myarray[i];
// Draw line
break;
default:
// Complain about unsupported object type.
}
}
}
By the way, to clean a vector of pointers to objects would require:
for (i = 0; i < count; i++)
free(myarray[i]); // Release individual elements
free(myarray); // Release array of pointers
/Per W
On Thu, 24 Oct 2002, Ioannis Vranos wrote:
> > -----Original Message-----
> > From: dev...@li...
> > [mailto:dev...@li...] On Behalf
> > Of Per Westermark
> > Sent: Thursday, October 24, 2002 4:13 PM
> > To: Scott Williams
> > Cc: dev...@li...
> > Subject: Re: [Dev-C++] dynamic allocation and structs
> >
> >
> >
> > // Alternative 1, array of entries.
> > mystruct *myarray;
> > void func(int count) {
> > int i;
> > myarray = (mystruct*)malloc(count*sizeof(mystruct));
> > for (i = 0; i < count; i++) {
> > myarray[i].xx = yy;
> > }
> > }
> >
> > // Alternative 2, array of pointers to entries.
> > mystruct **myarray;
> > void func(int count) {
> > int i;
> > myarray = (mystruct**)malloc(count*sizeof(*mystruct));
======> myarray = (mystruct**)malloc(count*sizeof(mystruct*));
> > for (i = 0; i < count; i++) {
> > myarray[i] = (mystruct*)malloc(sizeof(mystruct));
> > myarray[i]->xx = yy;
> > }
> > }
>
>
> The Alternative 2 is wrong. You use a ** pointer and then you assign
> values as if it is a *.
No, I use a ** pointer to assign an array of pointers.
Then I use a * pointer to assign an entry.
>
>
> A working example of what you are trying to do:
A working example of something I didn't tried to do :-)
>
> #include <stdlib.h>
> #include <stdio.h>
>
> int main()
> {
> struct mystruct
> {
> int x;
> };
>
> struct mystruct **myarray;
>
> int i, j;
>
> /* sizeof(*myarray)==sizeof(struct mystruct *) */
> myarray=malloc(100*sizeof(*myarray));
>
> /* sizeof(**myarray)==sizeof(struct mystruct).
> A 200x100 array on the free store is created */
> for(i=0; i<100; i++)
> myarray[i]=malloc(200*sizeof(**myarray));
>
> /* Do something */
> for(i=0; i<100; i++)
> for(j=0; j<200; j++)
> {
> myarray[i][j].x=i+j;
>
> printf("%d\n", myarray[i][j].x);
> }
>
>
> for(i=0; i<100; i++)
> free(myarray[i]);
>
> free(myarray);
>
> return 0;
> }
>
>
>
>
> Alternative 3 (which must be prefered if possible instead of A-2):
>
> #include <stdlib.h>
>
> int main()
> {
> struct mystruct
> {
> int x;
> };
>
> struct mystruct (*myarray)[100];
>
> /* sizeof(*myarray)==sizeof(struct mystruct).
> A 200x100 array on the free store is created */
> myarray=malloc(200*sizeof(*myarray));
>
> /* Do something */
>
>
> free(myarray);
>
> return 0;
> }
Can't be alternative 3, since it isn't a solution to the same probem.
This also assumes that you at compile time know one of the dimensions. In
C, you can't make this a template function, so you can't adjust the length
of the myarray declaration.
Let's go one step further. Let's assume I already know both dimensions:
struct mystruct (*myarray)[200][300];
myarray = malloc(sizeof(*myarray));
>
>
>
> Ioannis
>
> * Ioannis Vranos
> * Programming pages: http://www.noicys.freeurl.com
> * Alternative URL: http://run.to/noicys
>
>
>
>
>
> -------------------------------------------------------
> This sf.net email is sponsored by: Influence the future
> of Java(TM) technology. Join the Java Community
> Process(SM) (JCP(SM)) program now.
> http://ads.sourceforge.net/cgi-bin/redirect.pl?sunm0003en
> _______________________________________________
> Dev-cpp-users mailing list
> Dev...@li...
> TO UNSUBSCRIBE: http://www.noicys.cjb.net/devcpp/ub.htm
> https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
>
|
|
From: Per W. <pw...@ia...> - 2002-10-24 22:53:08
|
Ah, I did manage to add a bit of C++ in my example of storing different
object types in a vector.
Every reference to "Circle", "Line", "Box" should of course be
"struct Circle", "struct Line" and "struct Box".
And of course all // comments should be /* comment */
What can I say. I kinda prefer C++ to C. Downgrading is a bit like having
one arm tied up behind the back.
/Per W
On Fri, 25 Oct 2002, Per Westermark wrote:
> Well Ioannis, I did manage to get a star on the wrong side of the struct
> type name, see inserted correction below, so the example was slightly
> broken. However, the example was not a total goose chase try at a
> two-dimensional matrix of entries, but an example of creating a
> one-dimensional matrix (a vector) of pointers to single entries, which was
> exactly what the heading suggested "array of pointers to entries". If the
> code wasn't obvious, a hint to that fact should have been that I only
> used a single for loop to assign values to the entries.
>
> One reason to use alternative two, a vector of pointers to elements
> instead of a vector of elements, is that sorting of the vector only
> require changing the place of the pointers, instead of copying the full
> structures.
>
> Another reason is that it might be used to create a sparse matrix, where
> the vector might be very long, but only some index values of the vector
> will have an allocated entry.
>
> A third reason is that a vector of pointers might be to store different
> types of structs in the same vector, as in:
>
> enum {
> CIRCLE,
> LINE,
> BOX
> };
>
> struct Circle {
> int type;
> int x,y;
> int radius;
> };
>
> struct Line {
> int type;
> int x1,y1,x2,y2;
> }
>
> struct Box {
> int type;
> int x1,y1,x2,y2;
> };
>
> void fill_array() {
> Circle *pCircle;
> Line *pLine;
> Box *pBox;
> int item_count,i;
>
> item_count = 0;
> pCircle = (Circle*)malloc(sizeof(Circle));
> pCircle->type = CIRCLE;
> pCircle->x = 10;
> pCircle->y = 20;
> pCircle->radius = 5;
> myarray[item_count++] = pCircle;
>
> pBox = (Box*)malloc(sizeof(Box));
> pBox->type = BOX;
> pBox->x1 = 15;
> pBox->y1 = 2;
> pBox->x2 = 18;
> pBox->y2 = 5;
> myarray[item_count++] = pBox;
>
> ...
>
> // Draw items.
> for (i = 0; i < item_count; i++) {
> if (myarray[i]) {
> switch (myarray[i]->type) {
> case CIRCLE:
> pCircle = (Circle*)myarray[i];
> // Draw circle
> break;
> case BOX:
> pBox = (Box*)myarray[i];
> // Draw box
> break;
> case LINE:
> pLine = (Line*)myarray[i];
> // Draw line
> break;
> default:
> // Complain about unsupported object type.
> }
> }
> }
>
>
> By the way, to clean a vector of pointers to objects would require:
> for (i = 0; i < count; i++)
> free(myarray[i]); // Release individual elements
> free(myarray); // Release array of pointers
>
> /Per W
>
> On Thu, 24 Oct 2002, Ioannis Vranos wrote:
>
> > > -----Original Message-----
> > > From: dev...@li...
> > > [mailto:dev...@li...] On Behalf
> > > Of Per Westermark
> > > Sent: Thursday, October 24, 2002 4:13 PM
> > > To: Scott Williams
> > > Cc: dev...@li...
> > > Subject: Re: [Dev-C++] dynamic allocation and structs
> > >
> > >
> > >
> > > // Alternative 1, array of entries.
> > > mystruct *myarray;
> > > void func(int count) {
> > > int i;
> > > myarray = (mystruct*)malloc(count*sizeof(mystruct));
> > > for (i = 0; i < count; i++) {
> > > myarray[i].xx = yy;
> > > }
> > > }
> > >
> > > // Alternative 2, array of pointers to entries.
> > > mystruct **myarray;
> > > void func(int count) {
> > > int i;
> > > myarray = (mystruct**)malloc(count*sizeof(*mystruct));
> ======> myarray = (mystruct**)malloc(count*sizeof(mystruct*));
> > > for (i = 0; i < count; i++) {
> > > myarray[i] = (mystruct*)malloc(sizeof(mystruct));
> > > myarray[i]->xx = yy;
> > > }
> > > }
> >
> >
> > The Alternative 2 is wrong. You use a ** pointer and then you assign
> > values as if it is a *.
> No, I use a ** pointer to assign an array of pointers.
> Then I use a * pointer to assign an entry.
> >
> >
> > A working example of what you are trying to do:
> A working example of something I didn't tried to do :-)
> >
> > #include <stdlib.h>
> > #include <stdio.h>
> >
> > int main()
> > {
> > struct mystruct
> > {
> > int x;
> > };
> >
> > struct mystruct **myarray;
> >
> > int i, j;
> >
> > /* sizeof(*myarray)==sizeof(struct mystruct *) */
> > myarray=malloc(100*sizeof(*myarray));
> >
> > /* sizeof(**myarray)==sizeof(struct mystruct).
> > A 200x100 array on the free store is created */
> > for(i=0; i<100; i++)
> > myarray[i]=malloc(200*sizeof(**myarray));
> >
> > /* Do something */
> > for(i=0; i<100; i++)
> > for(j=0; j<200; j++)
> > {
> > myarray[i][j].x=i+j;
> >
> > printf("%d\n", myarray[i][j].x);
> > }
> >
> >
> > for(i=0; i<100; i++)
> > free(myarray[i]);
> >
> > free(myarray);
> >
> > return 0;
> > }
> >
> >
> >
> >
> > Alternative 3 (which must be prefered if possible instead of A-2):
> >
> > #include <stdlib.h>
> >
> > int main()
> > {
> > struct mystruct
> > {
> > int x;
> > };
> >
> > struct mystruct (*myarray)[100];
> >
> > /* sizeof(*myarray)==sizeof(struct mystruct).
> > A 200x100 array on the free store is created */
> > myarray=malloc(200*sizeof(*myarray));
> >
> > /* Do something */
> >
> >
> > free(myarray);
> >
> > return 0;
> > }
>
> Can't be alternative 3, since it isn't a solution to the same probem.
> This also assumes that you at compile time know one of the dimensions. In
> C, you can't make this a template function, so you can't adjust the length
> of the myarray declaration.
> Let's go one step further. Let's assume I already know both dimensions:
> struct mystruct (*myarray)[200][300];
> myarray = malloc(sizeof(*myarray));
> >
> >
> >
> > Ioannis
> >
> > * Ioannis Vranos
> > * Programming pages: http://www.noicys.freeurl.com
> > * Alternative URL: http://run.to/noicys
> >
> >
> >
> >
> >
> > -------------------------------------------------------
> > This sf.net email is sponsored by: Influence the future
> > of Java(TM) technology. Join the Java Community
> > Process(SM) (JCP(SM)) program now.
> > http://ads.sourceforge.net/cgi-bin/redirect.pl?sunm0003en
> > _______________________________________________
> > Dev-cpp-users mailing list
> > Dev...@li...
> > TO UNSUBSCRIBE: http://www.noicys.cjb.net/devcpp/ub.htm
> > https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
> >
>
>
>
> -------------------------------------------------------
> This sf.net email is sponsored by: Influence the future
> of Java(TM) technology. Join the Java Community
> Process(SM) (JCP(SM)) program now.
> http://ads.sourceforge.net/cgi-bin/redirect.pl?sunm0003en
> _______________________________________________
> Dev-cpp-users mailing list
> Dev...@li...
> TO UNSUBSCRIBE: http://www.noicys.cjb.net/devcpp/ub.htm
> https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
>
|
|
From: Ioannis V. <no...@ho...> - 2002-10-25 02:26:11
|
> -----Original Message-----
> From: Per Westermark [mailto:pw...@ia...]
> Sent: Friday, October 25, 2002 1:02 AM
> To: Ioannis Vranos
> Cc: dev...@li...
> Subject: RE: [Dev-C++] dynamic allocation and structs
>
>
> Well Ioannis, I did manage to get a star on the wrong side of
> the struct
> type name, see inserted correction below, so the example was slightly
> broken. However, the example was not a total goose chase try at a
> two-dimensional matrix of entries, but an example of creating a
> one-dimensional matrix (a vector) of pointers to single
> entries, which was
> exactly what the heading suggested "array of pointers to
> entries".
Lets avoid using the term "vector". Well an array of single pointers is
a 2 dimensional array of elements so it is the same.
> If the
> code wasn't obvious, a hint to that fact should have been that I only
> used a single for loop to assign values to the entries.
Which i think doesn't work.
>
> One reason to use alternative two, a vector of pointers to elements
> instead of a vector of elements, is that sorting of the vector only
> require changing the place of the pointers, instead of
> copying the full
> structures.
So you wanted to create a single-dimentional array of elements? Then a
single * would be enough.
>
> Another reason is that it might be used to create a sparse
> matrix, where
> the vector might be very long, but only some index values of
> the vector
> will have an allocated entry.
>
> A third reason is that a vector of pointers might be to store
> different
> types of structs in the same vector, as in:
>
> enum {
> CIRCLE,
> LINE,
> BOX
> };
>
> struct Circle {
> int type;
> int x,y;
> int radius;
> };
>
> struct Line {
> int type;
> int x1,y1,x2,y2;
> }
>
> struct Box {
> int type;
> int x1,y1,x2,y2;
> };
>
> void fill_array() {
> Circle *pCircle;
> Line *pLine;
> Box *pBox;
> int item_count,i;
>
> item_count = 0;
> pCircle = (Circle*)malloc(sizeof(Circle));
> pCircle->type = CIRCLE;
> pCircle->x = 10;
> pCircle->y = 20;
> pCircle->radius = 5;
> myarray[item_count++] = pCircle;
>
> pBox = (Box*)malloc(sizeof(Box));
> pBox->type = BOX;
> pBox->x1 = 15;
> pBox->y1 = 2;
> pBox->x2 = 18;
> pBox->y2 = 5;
> myarray[item_count++] = pBox;
>
> ...
>
> // Draw items.
> for (i = 0; i < item_count; i++) {
> if (myarray[i]) {
> switch (myarray[i]->type) {
> case CIRCLE:
> pCircle = (Circle*)myarray[i];
> // Draw circle
> break;
> case BOX:
> pBox = (Box*)myarray[i];
> // Draw box
> break;
> case LINE:
> pLine = (Line*)myarray[i];
> // Draw line
> break;
> default:
> // Complain about unsupported object type.
> }
> }
> }
I did not get your meaning. Where is myarray definition. I assume
myarray is a single arrray of void *. And?
Ioannis
* Ioannis Vranos
* Programming pages: http://www.noicys.freeurl.com
* Alternative URL: http://run.to/noicys
|
|
From: Per W. <pw...@ia...> - 2002-10-25 07:53:49
|
The term vector is valid, since the allocated information is only supposed
to be used with one single index, i.e. one-dimensional.
It must always be observed - yes this is outside scope of Dev-C++ but
standard C/C++ requirements - that a while a pointer might point to the
forst element of a vector, i.e.
mystruct *p;
p[0].x = 1;
p[1].x = 2;
...
it is not required or guaranteed that there are more than one element the
pointer is pointing at. Never assume a p[1] just because p[0] is fine.
The code below uses vector[i]->id, not vector[i][0].id, to signify that
this is a one-dimensional allocation, and that while the compiler would
accept vector[i][0], vector[i][1], vector[i][1] would point outside
allocated memory.
Full source compilable in c:
----test.c----
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// Obviously this struct should contain more than just an integer.
typedef struct {
int id;
char *str;
} mystruct;
int cmp(const void* a,const void* b) {
const mystruct *p1 = *(const mystruct**)a;
const mystruct *p2 = *(const mystruct**)b;
return p1->id - p2->id;
}
int main() {
const int count = 10;
mystruct **vector;
char buf[100];
int i;
// Allocate vector of pointers TO SINGLE ELEMENTS!
// Yes, it does look like allocating vector of pointers to vectors,
// but that is the charm of C/C++. It is up to the end user to
// know if a pointer points to first element of a vector, or just
// to a single element.
vector = (mystruct**)malloc(count*sizeof(mystruct*));
for (i = 0; i < count; i++) {
// Allocate count number of single elements.
vector[i] = (mystruct*)malloc(sizeof(mystruct));
}
// Assign values.
for (i = 0; i < count; i++) {
vector[i]->id = rand()%100;
sprintf(buf,"This was element vector[%d]",i);
vector[i]->str = strdup(buf);
}
// Sort values.
qsort(vector,count,sizeof(*vector),cmp);
// Print values.
for (i = 0; i < count; i++) {
printf("Vector element %d has value %d and str '%s'\n",
i,vector[i]->id,vector[i]->str);
}
// Clean up.
for (i = 0; i < count; i++) {
free(vector[i]->str);
free(vector[i]);
}
free(vector);
return 0;
}
-------------------
/Per W
On Fri, 25 Oct 2002, Ioannis Vranos wrote:
> > -----Original Message-----
> > From: Per Westermark [mailto:pw...@ia...]
> > Sent: Friday, October 25, 2002 1:02 AM
> > To: Ioannis Vranos
> > Cc: dev...@li...
> > Subject: RE: [Dev-C++] dynamic allocation and structs
> >
> >
> > Well Ioannis, I did manage to get a star on the wrong side of
> > the struct
> > type name, see inserted correction below, so the example was slightly
> > broken. However, the example was not a total goose chase try at a
> > two-dimensional matrix of entries, but an example of creating a
> > one-dimensional matrix (a vector) of pointers to single
> > entries, which was
> > exactly what the heading suggested "array of pointers to
> > entries".
>
>
> Lets avoid using the term "vector". Well an array of single pointers is
> a 2 dimensional array of elements so it is the same.
>
>
> > If the
> > code wasn't obvious, a hint to that fact should have been that I only
> > used a single for loop to assign values to the entries.
>
> Which i think doesn't work.
>
>
>
> >
> > One reason to use alternative two, a vector of pointers to elements
> > instead of a vector of elements, is that sorting of the vector only
> > require changing the place of the pointers, instead of
> > copying the full
> > structures.
>
>
> So you wanted to create a single-dimentional array of elements? Then a
> single * would be enough.
>
>
>
> >
> > Another reason is that it might be used to create a sparse
> > matrix, where
> > the vector might be very long, but only some index values of
> > the vector
> > will have an allocated entry.
> >
> > A third reason is that a vector of pointers might be to store
> > different
> > types of structs in the same vector, as in:
> >
> > enum {
> > CIRCLE,
> > LINE,
> > BOX
> > };
> >
> > struct Circle {
> > int type;
> > int x,y;
> > int radius;
> > };
> >
> > struct Line {
> > int type;
> > int x1,y1,x2,y2;
> > }
> >
> > struct Box {
> > int type;
> > int x1,y1,x2,y2;
> > };
> >
> > void fill_array() {
> > Circle *pCircle;
> > Line *pLine;
> > Box *pBox;
> > int item_count,i;
> >
> > item_count = 0;
> > pCircle = (Circle*)malloc(sizeof(Circle));
> > pCircle->type = CIRCLE;
> > pCircle->x = 10;
> > pCircle->y = 20;
> > pCircle->radius = 5;
> > myarray[item_count++] = pCircle;
> >
> > pBox = (Box*)malloc(sizeof(Box));
> > pBox->type = BOX;
> > pBox->x1 = 15;
> > pBox->y1 = 2;
> > pBox->x2 = 18;
> > pBox->y2 = 5;
> > myarray[item_count++] = pBox;
> >
> > ...
> >
> > // Draw items.
> > for (i = 0; i < item_count; i++) {
> > if (myarray[i]) {
> > switch (myarray[i]->type) {
> > case CIRCLE:
> > pCircle = (Circle*)myarray[i];
> > // Draw circle
> > break;
> > case BOX:
> > pBox = (Box*)myarray[i];
> > // Draw box
> > break;
> > case LINE:
> > pLine = (Line*)myarray[i];
> > // Draw line
> > break;
> > default:
> > // Complain about unsupported object type.
> > }
> > }
> > }
>
>
> I did not get your meaning. Where is myarray definition. I assume
> myarray is a single arrray of void *. And?
>
> Ioannis
>
> * Ioannis Vranos
> * Programming pages: http://www.noicys.freeurl.com
> * Alternative URL: http://run.to/noicys
>
>
> -------------------------------------------------------
> This sf.net email is sponsored by: Influence the future
> of Java(TM) technology. Join the Java Community
> Process(SM) (JCP(SM)) program now.
> http://ads.sourceforge.net/cgi-bin/redirect.pl?sunm0003en
> _______________________________________________
> Dev-cpp-users mailing list
> Dev...@li...
> TO UNSUBSCRIBE: http://www.noicys.cjb.net/devcpp/ub.htm
> https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
>
|
|
From: Ioannis V. <no...@ho...> - 2002-10-25 08:03:21
|
> -----Original Message----- > From: Per Westermark [mailto:pw...@ia...] > Sent: Friday, October 25, 2002 10:53 AM > To: Ioannis Vranos > Cc: Dev-C++ Mailing List > Subject: RE: [Dev-C++] dynamic allocation and structs > > > The term vector is valid, since the allocated information is > only supposed > to be used with one single index, i.e. one-dimensional. I said avoid using the term "vector" for built-in arrays so as to not confuse them with C++'s standard library vectors. Call them "built in arrays". > It must always be observed - yes this is outside scope of Dev-C++ but > standard C/C++ requirements - that a while a pointer might > point to the > forst element of a vector, i.e. > mystruct *p; > p[0].x = 1; > p[1].x = 2; > ... > it is not required or guaranteed that there are more than one > element the > pointer is pointing at. Never assume a p[1] just because p[0] is fine. > The code below uses vector[i]->id, not vector[i][0].id, to > signify that > this is a one-dimensional allocation, and that while the > compiler would > accept vector[i][0], vector[i][1], vector[i][1] would point outside > allocated memory. i think the second [] swits better notationally. It's a personal taste. Ioannis * Ioannis Vranos * Programming pages: http://www.noicys.freeurl.com * Alternative URL: http://run.to/noicys |
|
From: Per W. <pw...@ia...> - 2002-10-25 08:50:59
|
The trick is that vector isn't programming. It is a standard - and very old - mathematical term for a one-dimensional matrix. There has been millions of vector implementations before STL did a template version, and the probability of a finding a STL construct in a C program is not that high :-) While code might syntactically switch around between (*a).x, a->x a[0].x the third form is definitely dangerous to use when a is only a pointer to a single element. If there is an element zero, the logical conclusion is that there must be a element one. While one goal of programming is to make the programs work, it shouldn't be neglected that comming generations of programmers inherriting old code does immediately see what the code does. When seeing a x[0] statement, the natural reaction is - or should be - to start hunt for the total size of the vector. /Per W On Fri, 25 Oct 2002, Ioannis Vranos wrote: > > -----Original Message----- > > From: Per Westermark [mailto:pw...@ia...] > > Sent: Friday, October 25, 2002 10:53 AM > > To: Ioannis Vranos > > Cc: Dev-C++ Mailing List > > Subject: RE: [Dev-C++] dynamic allocation and structs > > > > > > The term vector is valid, since the allocated information is > > only supposed > > to be used with one single index, i.e. one-dimensional. > > > I said avoid using the term "vector" for built-in arrays so as to not > confuse them with C++'s standard library vectors. Call them "built in > arrays". > > > > > It must always be observed - yes this is outside scope of Dev-C++ but > > standard C/C++ requirements - that a while a pointer might > > point to the > > forst element of a vector, i.e. > > mystruct *p; > > p[0].x = 1; > > p[1].x = 2; > > ... > > it is not required or guaranteed that there are more than one > > element the > > pointer is pointing at. Never assume a p[1] just because p[0] is fine. > > The code below uses vector[i]->id, not vector[i][0].id, to > > signify that > > this is a one-dimensional allocation, and that while the > > compiler would > > accept vector[i][0], vector[i][1], vector[i][1] would point outside > > allocated memory. > > > i think the second [] swits better notationally. It's a personal taste. > > > > Ioannis > > * Ioannis Vranos > * Programming pages: http://www.noicys.freeurl.com > * Alternative URL: http://run.to/noicys > > > ------------------------------------------------------- > This sf.net email is sponsored by: Influence the future > of Java(TM) technology. Join the Java Community > Process(SM) (JCP(SM)) program now. > http://ads.sourceforge.net/cgi-bin/redirect.pl?sunm0004en > _______________________________________________ > Dev-cpp-users mailing list > Dev...@li... > TO UNSUBSCRIBE: http://www.noicys.cjb.net/devcpp/ub.htm > https://lists.sourceforge.net/lists/listinfo/dev-cpp-users > |
|
From: Ioannis V. <no...@ho...> - 2002-10-25 09:38:54
|
> -----Original Message----- > From: Per Westermark [mailto:pw...@ia...] > Sent: Friday, October 25, 2002 11:51 AM > To: Ioannis Vranos > Cc: 'Dev-C++ Mailing List' > Subject: RE: [Dev-C++] dynamic allocation and structs > > > The trick is that vector isn't programming. It is a standard > - and very > old - mathematical term for a one-dimensional matrix. Yes, but when we speak of C++ code, calling a built in array a "vector" can only be confusing. > There has been > millions of vector implementations before STL did a template > version, and > the probability of a finding a STL construct in a C program > is not that > high :-) Yes, but it still can be confusing to C++ programmers. > > While code might syntactically switch around between > (*a).x, > a->x > a[0].x > > the third form is definitely dangerous to use when a is only > a pointer to > a single element. If there is an element zero, the logical > conclusion is > that there must be a element one. Yes, if there is only one element i would use the -> only. When we have more the [] is more suitable. Both are valid. > > While one goal of programming is to make the programs work, > it shouldn't > be neglected that comming generations of programmers > inherriting old code > does immediately see what the code does. When seeing a x[0] > statement, the > natural reaction is - or should be - to start hunt for the > total size of > the vector. Yes. However i did created a 200x100 array on the free store so the second [] was suitable. :) Ioannis * Ioannis Vranos * Programming pages: http://www.noicys.freeurl.com * Alternative URL: http://run.to/noicys |
|
From: Per W. <pw...@ia...> - 2002-10-25 09:44:06
|
Same answer as to previous mail. We were specifically talking about a C implementation. By the way, a vector is a vector in C, C++, Java, Fortran, Modula 2, Pascal, ... It isn't about the language, but about the concept. And it doesn't matter if I implement it myself, buy an existing library or have it built into the language. /Per W On Fri, 25 Oct 2002, Ioannis Vranos wrote: > > -----Original Message----- > > From: Per Westermark [mailto:pw...@ia...] > > Sent: Friday, October 25, 2002 11:51 AM > > To: Ioannis Vranos > > Cc: 'Dev-C++ Mailing List' > > Subject: RE: [Dev-C++] dynamic allocation and structs > > > > > > The trick is that vector isn't programming. It is a standard > > - and very > > old - mathematical term for a one-dimensional matrix. > > > Yes, but when we speak of C++ code, calling a built in array a "vector" > can only be confusing. > > > > > There has been > > millions of vector implementations before STL did a template > > version, and > > the probability of a finding a STL construct in a C program > > is not that > > high :-) > > > Yes, but it still can be confusing to C++ programmers. > > > > > > > While code might syntactically switch around between > > (*a).x, > > a->x > > a[0].x > > > > the third form is definitely dangerous to use when a is only > > a pointer to > > a single element. If there is an element zero, the logical > > conclusion is > > that there must be a element one. > > > Yes, if there is only one element i would use the -> only. When we have > more the [] is more suitable. Both are valid. > > > > > > > > While one goal of programming is to make the programs work, > > it shouldn't > > be neglected that comming generations of programmers > > inherriting old code > > does immediately see what the code does. When seeing a x[0] > > statement, the > > natural reaction is - or should be - to start hunt for the > > total size of > > the vector. > > > Yes. However i did created a 200x100 array on the free store so the > second [] was suitable. :) > > > Ioannis > > * Ioannis Vranos > * Programming pages: http://www.noicys.freeurl.com > * Alternative URL: http://run.to/noicys > > > > ------------------------------------------------------- > This sf.net email is sponsored by: Influence the future > of Java(TM) technology. Join the Java Community > Process(SM) (JCP(SM)) program now. > http://ads.sourceforge.net/cgi-bin/redirect.pl?sunm0004en > _______________________________________________ > Dev-cpp-users mailing list > Dev...@li... > TO UNSUBSCRIBE: http://www.noicys.cjb.net/devcpp/ub.htm > https://lists.sourceforge.net/lists/listinfo/dev-cpp-users > |
|
From: Carlos d. M. <cg...@wo...> - 2002-10-25 10:20:10
|
Some things about pasts messages, not very important. The "or when exiting the scope", I wrote it, sorry, I was answering other question, it was an error when clicking at th mail I wanted to answer, now I don't remember which mail I was answering. One of you said: If array[0] is logical think there is array[1]. That's right in programming, not in reality. Reality: White House is whiteHousesArray[0]. But there is no a second White House If I say "I am the third son of my parents" you know there are the second and the first sons, but you don't know if there is a fourth, or we have to suppose there are a infinity? You can know previous, not the following. Programming: If there is not array[1], why bother writing array[0], when you can say *array? Although vector has the mathematical meaning of one dimensional array, and we were talking about C, it would be better leave 'vector' for the STL container, because that's our (or my) first thought when see 'vector' here, so I read understanding one thing and in the middle of the message, or some messages later, I notice I've misunderstood everything, or simply I expected other thing. I was tempted to not read this thread of messages, losing some interesting things. |
|
From: Per W. <pw...@ia...> - 2002-10-25 10:59:41
|
On Fri, 25 Oct 2002, Carlos Garc=EDa del Monte wrote: > Some things about pasts messages, not very important. > The "or when exiting the scope", I wrote it, sorry, I was answering > other question, it was an error when clicking at th mail I wanted to > answer, now I don't remember which mail I was answering. >=20 > One of you said: > If array[0] is logical think there is array[1]. > That's right in programming, not in reality. > Reality: White House is whiteHousesArray[0]. But there is no a second > White House > If I say "I am the third son of my parents" you know there are the > second and the first sons, but you don't know if there is a fourth, or > we have to suppose there are a infinity? You can know previous, not the > following. > Programming: If there is not array[1], why bother writing array[0], when > you can say *array? Good. That was my point. My code used the a->x syntax, since there wasn't a a[1].x to access. In this case, the full expression became myarray[i]->x instead of myarray[i][0].x, since myarray was an array of pointers to single element, i.e. one-dimensional. >=20 > Although vector has the mathematical meaning of one dimensional array, > and we were talking about C, it would be better leave 'vector' for the > STL container, because that's our (or my) first thought when see > 'vector' here, so I read understanding one thing and in the middle of > the message, or some messages later, I notice I've misunderstood > everything, or simply I expected other thing. I was tempted to not read > this thread of messages, losing some interesting things. >=20 >=20 >=20 > ------------------------------------------------------- > This sf.net email is sponsored by: Influence the future=20 > of Java(TM) technology. Join the Java Community=20 > Process(SM) (JCP(SM)) program now.=20 > http://ads.sourceforge.net/cgi-bin/redirect.pl?sunm0004en > _______________________________________________ > Dev-cpp-users mailing list > Dev...@li... > TO UNSUBSCRIBE: http://www.noicys.cjb.net/devcpp/ub.htm > https://lists.sourceforge.net/lists/listinfo/dev-cpp-users >=20 |
|
From: Abhijit S. <mu...@gm...> - 2002-10-25 12:12:04
|
Sorry if I'm converging, but I just found 2 cents in my hat... I remember reading on the Allegro list that data alloc'ed in the Allegro DLL is not accessible directly in the executable, so they use some fancy footwork to manage all that. Isn't it unsafe to assume that the heap is shared across programs and libraries? ___________________________________________________________ Abhijit Shylanath E-mail: mu...@gm... || ibr...@bi... Web-site: http://mudeth.tripod.com/ |
|
From: Per W. <pw...@ia...> - 2002-10-25 12:55:51
|
Since this is Dev-C++, i.e windows programing. The DLL is loaded in the memory space of your program. If multiple applications links to the same DLL, the DLL with "switch" between each applications memory space, depending on which application to make the call. The biggest problem with memory in a DLL is to have the DLL allocate C++ objects of new class types. You must export a static factory method, or global function for the allocation. There are a number of really good documents about this problem, but sorry, I don't have them accessible right now. /Per W On Fri, 25 Oct 2002, Abhijit Shylanath wrote: > Sorry if I'm converging, but I just found 2 cents in my hat... I remember > reading on the Allegro list that data alloc'ed in the Allegro DLL is not > accessible directly in the executable, so they use some fancy footwork to > manage all that. > > Isn't it unsafe to assume that the heap is shared across programs and > libraries? > > ___________________________________________________________ > > Abhijit Shylanath > > E-mail: mu...@gm... || ibr...@bi... > Web-site: http://mudeth.tripod.com/ > > > |
|
From: Scott W. <se...@al...> - 2002-10-25 13:43:13
|
Thanks for the replies to my question (and the entertaining discussion it
led to). It was all very helpful.
Scott
-----Original Message-----
From: Per Westermark [mailto:pw...@ia...]
Sent: Thursday, October 24, 2002 9:13 AM
To: Scott Williams
Cc: dev...@li...
Subject: Re: [Dev-C++] dynamic allocation and structs
// Alternative 1, array of entries.
mystruct *myarray;
void func(int count) {
int i;
myarray = (mystruct*)malloc(count*sizeof(mystruct));
for (i = 0; i < count; i++) {
myarray[i].xx = yy;
}
}
// Alternative 2, array of pointers to entries.
mystruct **myarray;
void func(int count) {
int i;
myarray = (mystruct**)malloc(count*sizeof(*mystruct));
for (i = 0; i < count; i++) {
myarray[i] = (mystruct*)malloc(sizeof(mystruct));
myarray[i]->xx = yy;
}
}
/Per W
On Thu, 24 Oct 2002, Scott Williams wrote:
> Hi,
>
> I'm working in C (no C++ please). Got a couple of questions related to
> dynamic allocation and in particular, dynamically allocating an array of
> structs.
>
> First, what is the duration of memory allocated by malloc (etc.)? In
> particular, I want to dynamically allocate/de-allocate several global
> variables within a single subroutine called from main and so want to be
sure
> that the allocated memory exists beyond the scope of that subroutine.
>
> Second, it is my understanding that it is perfectly acceptable to have an
> array of structs. E.g.,
>
> struct mystruct {
> double var1;
> long var2;
> etc...
> }
>
> . . .
>
> struct mystruct data[100];
>
> Is this correct? If so, what then is the calling sequence to dynamically
> allocate an array of structs, and, in particularly, make the result
> available globally?
>
> Thanks for your help (& sorry if this is naive).
>
> Scott Williams
>
>
>
> -------------------------------------------------------
> This sf.net email is sponsored by: Influence the future
> of Java(TM) technology. Join the Java Community
> Process(SM) (JCP(SM)) program now.
>
http://ad.doubleclick.net/clk;4729346;7592162;s?http://www.sun.com/javavote
> _______________________________________________
> Dev-cpp-users mailing list
> Dev...@li...
> TO UNSUBSCRIBE: http://www.noicys.cjb.net/devcpp/ub.htm
> https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
>
|
|
From: Louis G. <lg...@ya...> - 2002-10-26 00:30:27
|
What was the final answer to the the original question asked:
"what is the duration of memory allocated by malloc (etc.)? "
If not freed manually or by a destructor, Does the memory ALWAYS get
released when the main() program ends with a normal exit ?
--- Scott Williams <se...@al...> wrote:
> Thanks for the replies to my question (and the entertaining discussion
> it
> led to). It was all very helpful.
>
> Scott
>
> -----Original Message-----
> From: Per Westermark [mailto:pw...@ia...]
> Sent: Thursday, October 24, 2002 9:13 AM
> To: Scott Williams
> Cc: dev...@li...
> Subject: Re: [Dev-C++] dynamic allocation and structs
>
>
>
> // Alternative 1, array of entries.
> mystruct *myarray;
> void func(int count) {
> int i;
> myarray = (mystruct*)malloc(count*sizeof(mystruct));
> for (i = 0; i < count; i++) {
> myarray[i].xx = yy;
> }
> }
>
> // Alternative 2, array of pointers to entries.
> mystruct **myarray;
> void func(int count) {
> int i;
> myarray = (mystruct**)malloc(count*sizeof(*mystruct));
> for (i = 0; i < count; i++) {
> myarray[i] = (mystruct*)malloc(sizeof(mystruct));
> myarray[i]->xx = yy;
> }
> }
>
> /Per W
>
> On Thu, 24 Oct 2002, Scott Williams wrote:
>
> > Hi,
> >
> > I'm working in C (no C++ please). Got a couple of questions related
> to
> > dynamic allocation and in particular, dynamically allocating an array
> of
> > structs.
> >
> > First, what is the duration of memory allocated by malloc (etc.)? In
> > particular, I want to dynamically allocate/de-allocate several global
> > variables within a single subroutine called from main and so want to
> be
> sure
> > that the allocated memory exists beyond the scope of that subroutine.
> >
> > Second, it is my understanding that it is perfectly acceptable to have
> an
> > array of structs. E.g.,
> >
> > struct mystruct {
> > double var1;
> > long var2;
> > etc...
> > }
> >
> > . . .
> >
> > struct mystruct data[100];
> >
> > Is this correct? If so, what then is the calling sequence to
> dynamically
> > allocate an array of structs, and, in particularly, make the result
> > available globally?
> >
> > Thanks for your help (& sorry if this is naive).
> >
> > Scott Williams
> >
> >
> >
> > -------------------------------------------------------
> > This sf.net email is sponsored by: Influence the future
> > of Java(TM) technology. Join the Java Community
> > Process(SM) (JCP(SM)) program now.
> >
>
http://ad.doubleclick.net/clk;4729346;7592162;s?http://www.sun.com/javavote
> > _______________________________________________
> > Dev-cpp-users mailing list
> > Dev...@li...
> > TO UNSUBSCRIBE: http://www.noicys.cjb.net/devcpp/ub.htm
> > https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
> >
>
>
>
>
> -------------------------------------------------------
> This sf.net email is sponsored by: Influence the future
> of Java(TM) technology. Join the Java Community
> Process(SM) (JCP(SM)) program now.
> http://ads.sourceforge.net/cgi-bin/redirect.pl?sunm0004en
> _______________________________________________
> Dev-cpp-users mailing list
> Dev...@li...
> TO UNSUBSCRIBE: http://www.noicys.cjb.net/devcpp/ub.htm
> https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
__________________________________________________
Do you Yahoo!?
Y! Web Hosting - Let the expert host your web site
http://webhosting.yahoo.com/
|
|
From: Scott W. <se...@al...> - 2002-10-26 03:24:28
|
Don't know if anyone responded yet but I think the answer is: Yes, unless
your OS messes up. The release of the memory wasn't actually my concern,
however, I just wanted to be sure memory I allocated in one part of my
program was going to be available elsewhere.
Scott
-----Original Message-----
From: Louis Godio [mailto:lg...@ya...]
Sent: Friday, October 25, 2002 8:30 PM
To: se...@al...; Per Westermark
Cc: dev...@li...
Subject: RE: [Dev-C++] dynamic allocation and structs
What was the final answer to the the original question asked:
"what is the duration of memory allocated by malloc (etc.)? "
If not freed manually or by a destructor, Does the memory ALWAYS get
released when the main() program ends with a normal exit ?
--- Scott Williams <se...@al...> wrote:
> Thanks for the replies to my question (and the entertaining
> discussion it led to). It was all very helpful.
>
> Scott
>
> -----Original Message-----
> From: Per Westermark [mailto:pw...@ia...]
> Sent: Thursday, October 24, 2002 9:13 AM
> To: Scott Williams
> Cc: dev...@li...
> Subject: Re: [Dev-C++] dynamic allocation and structs
>
> // Alternative 1, array of entries.
> mystruct *myarray;
> void func(int count) {
> int i;
> myarray = (mystruct*)malloc(count*sizeof(mystruct));
> for (i = 0; i < count; i++) {
> myarray[i].xx = yy;
> }
> }
>
> // Alternative 2, array of pointers to entries.
> mystruct **myarray;
> void func(int count) {
> int i;
> myarray = (mystruct**)malloc(count*sizeof(*mystruct));
> for (i = 0; i < count; i++) {
> myarray[i] = (mystruct*)malloc(sizeof(mystruct));
> myarray[i]->xx = yy;
> }
> }
>
> /Per W
>
> On Thu, 24 Oct 2002, Scott Williams wrote:
>
>> Hi,
>>
>> I'm working in C (no C++ please). Got a couple of questions
>> related to dynamic allocation and in particular, dynamically
>> allocating an array of structs.
>>
>> First, what is the duration of memory allocated by malloc
>> (etc.)? In particular, I want to dynamically allocate/de-
>> allocate several global variables within a single subroutine
>> called from main and so want to be sure that the allocated
>> memory exists beyond the scope of that subroutine.
>>
>> Second, it is my understanding that it is perfectly acceptable
>> to have an array of structs. E.g.,
>>
>> struct mystruct {
>> double var1;
>> long var2;
>> etc...
>> }
>>
>> . . .
>>
>> struct mystruct data[100];
>>
>> Is this correct? If so, what then is the calling sequence to
>> dynamically allocate an array of structs, and, in particularly,
>> make the result available globally?
>>
>> Thanks for your help (& sorry if this is naive).
>>
>> Scott Williams
|
|
From: Per W. <pw...@ia...> - 2002-10-26 10:28:56
|
Unless you work in embedded environments - set-top boxes, cameras, burglar
alarms, ... - you can be pretty sure that you have an OS that will release
all memory uppon termination of your program.
However, you normally should take care of releasing your memory anyway for
the following reasons:
- If the program does a lot of alloc/free, and you want to make sure that
the program isn't leaking memory, constantly growing in size. There are a
number of memory heap check programs that on termination of the program
prints out a reference to all memory not released. Obviously, this isn't
meaningful if you intentionally ignores the release of some memory. The
printout will contain a lot of nice then.
- If your aim is to change from C to C++. When the OS releases memory, it
will never call any object destructors, and a number of C++ objects might
have other side effects besides just consuming memory :-) such as
=> Closing and removing temporary files.
=> Removing a hourglass icon, and replacing with normal arrow icon.
=> Terminating another program (IDE don't need a compiler anymore).
When you do implement a C++ class, you really do expect the destructor to
be called, and consider it quite rude if someone don't release the object
before ending the program.
/Per W
On Fri, 25 Oct 2002, Scott Williams wrote:
> Don't know if anyone responded yet but I think the answer is: Yes, unless
> your OS messes up. The release of the memory wasn't actually my concern,
> however, I just wanted to be sure memory I allocated in one part of my
> program was going to be available elsewhere.
>
> Scott
>
> -----Original Message-----
> From: Louis Godio [mailto:lg...@ya...]
> Sent: Friday, October 25, 2002 8:30 PM
> To: se...@al...; Per Westermark
> Cc: dev...@li...
> Subject: RE: [Dev-C++] dynamic allocation and structs
>
> What was the final answer to the the original question asked:
> "what is the duration of memory allocated by malloc (etc.)? "
>
> If not freed manually or by a destructor, Does the memory ALWAYS get
> released when the main() program ends with a normal exit ?
>
> --- Scott Williams <se...@al...> wrote:
> > Thanks for the replies to my question (and the entertaining
> > discussion it led to). It was all very helpful.
> >
> > Scott
> >
> > -----Original Message-----
> > From: Per Westermark [mailto:pw...@ia...]
> > Sent: Thursday, October 24, 2002 9:13 AM
> > To: Scott Williams
> > Cc: dev...@li...
> > Subject: Re: [Dev-C++] dynamic allocation and structs
> >
> > // Alternative 1, array of entries.
> > mystruct *myarray;
> > void func(int count) {
> > int i;
> > myarray = (mystruct*)malloc(count*sizeof(mystruct));
> > for (i = 0; i < count; i++) {
> > myarray[i].xx = yy;
> > }
> > }
> >
> > // Alternative 2, array of pointers to entries.
> > mystruct **myarray;
> > void func(int count) {
> > int i;
> > myarray = (mystruct**)malloc(count*sizeof(*mystruct));
> > for (i = 0; i < count; i++) {
> > myarray[i] = (mystruct*)malloc(sizeof(mystruct));
> > myarray[i]->xx = yy;
> > }
> > }
> >
> > /Per W
> >
> > On Thu, 24 Oct 2002, Scott Williams wrote:
> >
> >> Hi,
> >>
> >> I'm working in C (no C++ please). Got a couple of questions
> >> related to dynamic allocation and in particular, dynamically
> >> allocating an array of structs.
> >>
> >> First, what is the duration of memory allocated by malloc
> >> (etc.)? In particular, I want to dynamically allocate/de-
> >> allocate several global variables within a single subroutine
> >> called from main and so want to be sure that the allocated
> >> memory exists beyond the scope of that subroutine.
> >>
> >> Second, it is my understanding that it is perfectly acceptable
> >> to have an array of structs. E.g.,
> >>
> >> struct mystruct {
> >> double var1;
> >> long var2;
> >> etc...
> >> }
> >>
> >> . . .
> >>
> >> struct mystruct data[100];
> >>
> >> Is this correct? If so, what then is the calling sequence to
> >> dynamically allocate an array of structs, and, in particularly,
> >> make the result available globally?
> >>
> >> Thanks for your help (& sorry if this is naive).
> >>
> >> Scott Williams
>
>
|
|
From: Ioannis V. <no...@ho...> - 2002-10-26 13:58:10
|
> -----Original Message----- > From: dev...@li...=20 > [mailto:dev...@li...] On Behalf=20 > Of Louis Godio > Sent: Saturday, October 26, 2002 3:30 AM > To: se...@al...; Per Westermark > Cc: dev...@li... > Subject: RE: [Dev-C++] dynamic allocation and structs >=20 >=20 >=20 > What was the final answer to the the original question asked: > "what is the duration of memory allocated by malloc (etc.)? " >=20 > If not freed manually or by a destructor, Does the memory ALWAYS get > released when the main() program ends with a normal exit ?=20 Yes. Ioannis * Ioannis Vranos * Programming pages: http://www.noicys.freeurl.com * Alternative URL: http://run.to/noicys |
|
From: Ioannis V. <no...@ho...> - 2002-10-24 20:29:23
|
> -----Original Message-----
> From: dev...@li...=20
> [mailto:dev...@li...] On Behalf=20
> Of Scott Williams
> Sent: Thursday, October 24, 2002 3:18 PM
> To: dev...@li...
> Subject: [Dev-C++] dynamic allocation and structs
>=20
>=20
> Hi,
>=20
> I'm working in C (no C++ please). Got a couple of questions=20
> related to
> dynamic allocation and in particular, dynamically allocating=20
> an array of
> structs.
>=20
> First, what is the duration of memory allocated by malloc (etc.)? In
> particular, I want to dynamically allocate/de-allocate several global
> variables within a single subroutine called from main and so=20
> want to be sure
> that the allocated memory exists beyond the scope of that subroutine.
>=20
> Second, it is my understanding that it is perfectly=20
> acceptable to have an
> array of structs. E.g.,
>=20
> struct mystruct {
> double var1;
> long var2;
> etc...
> }
>=20
> . . .
>=20
> struct mystruct data[100];
>=20
> Is this correct? If so, what then is the calling sequence to=20
> dynamically
> allocate an array of structs, and, in particularly, make the result
> available globally?
>=20
> Thanks for your help (& sorry if this is naive).
Yes you can have an array of structs, either in automatic storage like
your example above, or on the free store. Objects on the free store
exist till you free() that memory while your program is running. If you
do not free() that memory and lose its address (in some point you have
no pointer pointing to it), then you have a memory leak.
Ioannis
* Ioannis Vranos
* Programming pages: http://www.noicys.freeurl.com
* Alternative URL: http://run.to/noicys
|