RE: [Dev-C++] dynamic allocation and structs
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
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
>
|