访问/释放动态分配结构数组时的不良行为

0x3F

每次运行时,都会在 GDB 中输出分段错误。我正在尝试制作一个动态的结构数组。访问/释放数组中的任何内容时出现错误。

结构:

typedef struct{
  SDL_Texture* texture;
  SDL_Rect textureSelect;
  SDL_Rect objectCollision;//this doesnt have the accuracy of floats, so that might take more work than I want. Use this to get world coordinates
  SDL_Rect objectTransform;
  float objectVelX;
  float objectVelY;
  float objectX;
  float objectY;
  float objectRotation;
  unsigned short hasCollision;
  unsigned short hasPhysics;
  unsigned short used;
  unsigned short visible;
}Object;

罪魁祸首函数之一:

unsigned short firstCalled = 0;
Object *objects = NULL;
unsigned long objectAmmount = 0;

unsigned long AddObject(SDL_Rect collisionRect, SDL_Rect selectTexture, const char *textureUrl){

  if(firstCalled){//for first time calling

    objects = (Object *)malloc(sizeof(Object));//this is fine

    if(objects == NULL){

      printf("Critical memory allocation error\n");

    } else{
      printf("Allocated memory\n");
    }

  } else{

    Object *tempObjects = (Object *)realloc(objects, (objectAmmount + 2) * sizeof(Object));//have to do 1+ to make room for more

    if(tempObjects == NULL){

      printf("CRITICAL*** Out of memory/memory error\n");

    } else{

      printf("Reallocation successfull\n");

      objects = tempObjects;
      tempObjects = NULL;

    }
  }
//Which then goes on to set each variable to a wanted default value

objects[objectAmmount].textureSelect = selectTexture;
objects[objectAmmount].objectCollision = collisionRect;
objects[objectAmmount].objectTransform.x = 0;
//...

objectAmmount++;



if(firstCalled){//for first time calling
    //just give the function that called this this number because this is the object ID;
    firstCalled = 1;
    return objectAmmount - 1;
  } else{
    return objectAmmount;
  }
}

在渲染函数中:

Object *tempAccessObject = NULL;
void Render(){
  for(i = 0; i <= GetObjectCount(); i++){
    tempAccessObject = GetObject(i);//GetObject returns a pointer to the specific point in the array
    //use the variables in the array to render the objects
   }
}

最后,销毁函数:

void DestroyScene(){
  firstCalled = 0;
  if(objects != NULL){
    printf("Nothing to free\n");
    unsigned long i;
    for(i = 0; i <= objectAmmount; i++){
      if(objects[i].texture != NULL){
        SDL_DestroyTexture(objects[i].texture);
      }
    }
    free(objects);
    objects = NULL;
  }

  objectAmmount = 0;
}
克里斯特纳

看起来像是索引问题。以这个例子循环:

for(i = 0; i <= objectAmmount; i++){
  if(objects[i].texture != NULL){
    SDL_DestroyTexture(objects[i].texture);
  }
}

该条件表达式应该是i < objectAmmount您不想访问的,objects[objectAmmount]因为它将未初始化或超出您分配的内存范围,具体取决于您调用的次数AddObjectmalloc只有 1 个元素,但realloc每次额外 2 个元素,这掩盖了像 valgrind 这样的程序的这个问题。

猜测(因为您没有包含 for 的代码GetObjectCount)您的主Render循环也有这个问题。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

函数返回后释放结构的动态分配数组。

来自分类Dev

正确释放动态分配的结构

来自分类Dev

结构数组,动态分配

来自分类Dev

动态分配结构数组

来自分类Dev

从结构中的2D数组释放动态分配的内存

来自分类Dev

为结构数组动态分配内存

来自分类Dev

分段故障动态分配的结构数组

来自分类Dev

如何制作动态分配的结构数组?

来自分类Dev

c动态分配结构数组

来自分类Dev

分段故障动态分配的结构数组

来自分类Dev

从文件C动态分配结构数组

来自分类Dev

动态分配多个结构的数组

来自分类Dev

访问动态分配数组的元素

来自分类Dev

strcat的不良行为

来自分类Dev

释放动态分配的内存

来自分类Dev

释放动态分配的内存

来自分类Dev

请帮助我释放此动态分配的数组

来自分类Dev

使用动态分配的数组时出错

来自分类Dev

动态分配的结构数组传递给函数并通过索引访问

来自分类Dev

C-释放动态分配的结构体数组会导致“无效的下一个大小(快速)”错误

来自分类Dev

在C中填充动态分配的2D char数组时的奇怪行为

来自分类Dev

动态分配包含动态分配数组的结构体数组

来自分类Dev

动态分配char数组和int数组的结构

来自分类Dev

将值分配给指针引用的结构的动态分配结构成员数组时,openacc错误

来自分类Dev

访问通过套接字接收的动态分配的结构

来自分类Dev

Laravel 5删除斜杠时的不良行为

来自分类Dev

显示工作空间时活动概述的不良行为

来自分类Dev

在另一个动态分配的数组中释放一个动态分配的数组

来自分类Dev

在结构中动态分配数组-C