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-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
>
|