结构分配在C导致内存覆盖(我认为)



我以为我了解这些东西,但是我在这里被难住了。

给定一个结构体声明为:

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

我有一个方法,重新分配一个数组,以适应添加一个新的东西对象。它看起来像这样:

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++;
}

注意:things和thingCount是全局的。不要狂。哦,我也意识到这是泄漏。一次解决一个问题……

为了创建Thing对象,我创建了一个初始化函数。它看起来像这样:

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

让事情变得更复杂(没有双关语的意思),Thing对象有一个子对象数组,当新对象被添加到它中时,这些子对象被重新分配(成长)。它看起来像这样:

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

无论如何,我有一个艰难的时间弄清楚为什么当我完成了我的结构和添加子结构,他们经常被归零,即使我验证了他们的创建(在调试器中),当他们被创建。当main中的代码完成运行时,我应该有一个树形结构,但它只是一堆乱七八糟的值,我不认识也不理解——这就是为什么我认为有些东西被覆盖了。

无论如何,我希望我只是忽略了一些简单的事情。

如果你想知道我是如何构建我的对象层次结构的,下面是我的main:

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's Son");
  AddChildThingToThing(thing, child);
  child = CreateThingWithDescription("This is child thing 2");
  SetThingName(child, "Willy's 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;
}

注意:这只是一个演示项目,用来弄清楚发生了什么

您需要在AddTopLevelThing函数中多分配一个结构体,而不是一个字节:

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

同样,在重新分配后不释放旧的内存块。最好使用realloc ('realloc'关心复制旧数据和释放旧内存;它有时也可以"就地"执行重新分配,这更有效):

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

最新更新