views:

72

answers:

4

I am trying to allocate a block of memory, and store a list of structures without using multiple mallocs for each... this is just a generic example, I don't have the original code I was working with earlier, but this is the general idea, but my problem was that I was getting heap corruption when other parts of my code executed after the InitPoints() function call. I don't know what part of my code is illegal, but I suspect it is in the for loop of the InitPoints() function. I am trying to use this as table, then I can create additional tables of defined size if I ran out of memory and link them together... so kind of like a dynamic expanding array if that makes any sense.

typedef struct Tb{
   POINT points;
   POINT *next;
 } TABLE;

typedef struct Pt{
   int x;
   int y;
}POINT;

POINT *mypoints;

int main() {
   int size = 10;
   int i = 0;
   mypoints = InitPoints(size);

   for(i=0; i < size; i++)
   {
      printf("mypoint [%d] = (%d,%d)\n",i, mypoints->x, mypoints->y);
      mypoints = mypoints + sizeof(POINT);
   }
  // some other code...
  // i.e. createThread(....)

   return 0;
}

POINT* InitPoints(int size)
{
   POINT *tmp;
   POINT *orig;
   int a = 10;
   int b = 1000;
   orig = (POINT*) malloc (sizeof(POINT) * size);
   if(orig == NULL)
      return NULL;

   tmp = orig;
   for (i = 0; i < size; i++)
   {
      tmp->x = a++;
      tmp->y = b++;
      tmp = tmp + sizeof(POINT);
   }
return orig;
} 
+3  A: 

The problem is in this line:

tmp = tmp + sizeof(POINT);

It should be

++tmp;

The latter says to increment the pointer by one element; since it points to the structure, it increments by the size of the structure. The original code instead increments by n elements where n is the number of bytes in the structure. For example, if int is 32-bits, it will advanced by 8 elements.

wallyk
+3  A: 

This is wrong:

mypoints = mypoints + sizeof(POINT); 

You should review pointer arithmetic in C. Just use:

mypoints += 1; /* or something similar */

(There is a similar problem in your InitPoints function)

Here's one referemce:

http://www.eskimo.com/~scs/cclass/notes/sx10b.html

BobbyShaftoe
thanks, and I am on crack for trying to do this, or do people actually use this? I don't want to use a simple linked list to avoid maintaining a list. I know most of the time I will only have a small number of elements to work with... say 10, but in case i need more space I will just allocate another 10 and so on...
emge
No, it's not particularly bad. Don't be careful about your pointer arithmetic!
BobbyShaftoe
+1  A: 

This is why I would do it

for (i = 0; i < size; i++)
{
    orig[i].x = a++;
    orig[i].y = b++;
}
zebediah49
A: 

In C, adding an integer to a POINT* pointer advances the pointer not by that number of bytes, but by that number of POINT structures.

You have two places in your code where you add sizeof(POINT) to your pointer. Instead you should just add 1.

Ned Batchelder
thanks... I think that will solve my problem...
emge