Structure allocation in C causing memory overwriting (I think)

I thought I understood this stuff, but I'm here at a standstill.

For a structure declared as:

typedef struct _Thing { uint32_t type; struct _Thing *children; unsigned long childCount; char *description; union { uint32_t thirtyTwoBitValue; char *nameValue; } data; } Thing; 

I have a method that reassigns an array to host a new Thing object. It looks like this:

 void AddTopLevelThing(Thing *thing) { Thing *oldThings = things; things = malloc(sizeof(Thing) * thingCount +1); // Add any existing things to the new array for (int i = 0; i < thingCount; ++i) { things[i] = oldThings[i]; } // Add the newest thing to the new array things[thingCount] = *thing; // Increment the thing count thingCount++; } 

Note: things and thing The totality is global. Not a freak. ;-) Oh, and I also understand that this is a leak. One problem at a time ...

To create Thing objects, I created an initialization function. It looks like this:

 Thing* CreateThingWithDescription(char *description) { Thing *thing = malloc(sizeof(Thing)); if (thing == NULL) { printf("Bad thing!, Bad!\n"); return NULL; } // Initialize everything in the structure to 0 memset(thing, 0, sizeof(Thing)); thing->children = NULL; thing->description = strdup(description); return thing; } 

To complicate the situation (not a pun), the Thing object has an array of children that are redistributed (grown) when new objects are added to it. It looks like this:

 void AddChildThingToThing(Thing *parent, Thing *child) { Thing *oldChildren = parent->children; parent->children = malloc(sizeof(Thing) * parent->childCount + 1); if (parent->children == NULL) { printf("Couldn't allocate space for thing children.\n"); parent->children = oldChildren; return; } // Add any existing child things to the new array for (int i = 0; i < parent->childCount; ++i) { parent->children[i] = oldChildren[i]; } // Add the newest child thing to the new array parent->children[parent->childCount] = *child; // Increment the child count parent->childCount = parent->childCount + 1; } 

In any case, it is difficult for me to determine why, when I finished creating my structures and adding child structures, they are often nullified, even if I confirmed their creation (in the debugger) when they were created. When the code in my main execution ends, I should have a tree structure, but instead itโ€™s just a distorted mess of values โ€‹โ€‹that I donโ€™t recognize and donโ€™t understand - thatโ€™s why I think things are being rewritten.

In any case, I hope that I just miss something simple.

Here is my main one, if you want to see how I build a hierarchy of objects:

 int main(int argc, const char * argv[]) { things = NULL; thingCount = 0; Thing *thing = CreateThingWithDescription("This is thing 1"); SetThingName(thing, "Willy Johnson"); AddTopLevelThing(thing); Thing *child = CreateThingWithDescription("This is child thing 1"); SetThingName(child, "Willy Son"); AddChildThingToThing(thing, child); child = CreateThingWithDescription("This is child thing 2"); SetThingName(child, "Willy Daughter"); AddChildThingToThing(thing, child); thing = CreateThingWithDescription("This is thing 2"); SetThingValue(thing, 700); AddTopLevelThing(thing); child = CreateThingWithDescription("This is child thing 3"); SetThingValue(child, 1024); AddChildThingToThing(thing, child); for (int i = 0; i < thingCount; ++i) { PrintThing(&things[i]); } return 0; } 

Note: this is just a demo project to find out what is happening.

+4
source share
1 answer

You need to allocate one more structure, not one byte in your AddTopLevelThing function:

 things = malloc(sizeof(Thing) * (thingCount+1)); 

In addition, you do not free the old memory block after reallocation. And it is better to use realloc ("realloc" takes care of copying old data and freeing old memory, and it can also sometimes perform redistribution "in place", which is much more efficient):

 void AddTopLevelThing(Thing *thing) { thingCount++; things = realloc(things, sizeof(Thing) * thingCount); things[thingCount-1] = *thing; } 
+6
source

Source: https://habr.com/ru/post/1484872/


All Articles