glibc detected, realloc(): invalid pointer

前端 未结 1 2079
花落未央
花落未央 2021-02-10 14:18

I apologize for the lengthy code. I have a simple question, but I thought I include my code so it will be clear where I am coming from. I get a realloc corruption. I think the c

1条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-02-10 14:36

    glibc is telling you you're passing in an address that couldn't have been returned from malloc/realloc. This is because you're passing in the address of the pointsAndName stack variable. You need to pass in the value, which is what you received from malloc. Also, whenever you call realloc, you should use a temporary variable. That way, if the realloc fails, you still free the original value.

    struct figure *tempPtr = realloc(pointsAndname, temp * sizeof(struct figure));
    if(tempPtr == NULL)
    {
      // Handle allocation error...
      free(pointsAndname);
    }
    pointsAndname = tempPtr;
    

    0 讨论(0)
提交回复
热议问题