[英]realloc function in C causes “realloc(): invalid pointer:” Error
I am trying to create a Dynamic Integer Array in C which should automatically double its size once full. 我正在尝试在C中创建一个动态整数数组,一旦充满,它应该自动将其大小加倍。
To extend the size of the Array I want to use the realloc function. 为了扩展数组的大小,我想使用realloc函数。 Unfortunately there seems to be some problem with the pointer to the data of my DynamicArray and GCC crashes :( Does anyone have a clue why? If I realloc just after using malloc, there is no crash. I have checked the pointer address and it is not changed between malloc and realloc. 不幸的是,指向我的DynamicArray的数据的指针似乎出了点问题,GCC崩溃了:(有人知道为什么吗?如果我在使用malloc之后重新分配,就没有崩溃。我已经检查了指针地址,它是在malloc和realloc之间未更改。
The following are my header file declarations for the struct: 以下是我的结构头文件声明:
struct DynamicArray
{
unsigned int size;
unsigned int capacity;
int *data;
}; // ----- end of struct DynamicArray -----
typedef struct DynamicArray DynamicArray;
// ##### EXPORTED FUNCTION DECLARATIONS
DynamicArray* dn_new ( unsigned int capacity );
int dn_append ( DynamicArray *a, int elem );
This is my main class used for testing: 这是我用于测试的主要课程:
int
main ( int argc, char *argv[] )
{
DynamicArray *a1;
int i;
a1 = dn_new ( 5 );
// here six integers are inserted, the sixth causes the realloc crash as this is where I want to extend
dn_append ( a1, 5 );
dn_append ( a1, 7 );
dn_append ( a1, 8 );
dn_append ( a1, 11 );
dn_append ( a1, 13 );
dn_append ( a1, 15 );
return 0;
} // ----- end of function main -----
These are the functions dn_append and dn_new which are called by the main: 这些是主要调用的函数dn_append和dn_new:
DynamicArray*
dn_new ( unsigned int capacity )
{
struct DynamicArray *a = malloc(sizeof(struct DynamicArray));
a->data = (int *)calloc(capacity,capacity * sizeof(int));
printf("allocated %d\n",a->data);
a->size=0;
a->capacity=capacity;
printf("array created\n");
if (a == NULL){
return NULL;
}
else{
return a;
}
}
int
dn_append ( DynamicArray *a, int elem )
{
short extended = 0;
if (a->size==a->capacity){
a->capacity=a->capacity*2;
printf("allocated %d\n",a->data);
//here is exactly where the error occurs
a->data = (int *)realloc(a->data,a->capacity * sizeof(int));
printf("array extended to have capacity for %d integers\n",a->capacity);
extended=1;
}
a->data[(a->size-1)] = elem;
a->size++;
printf("element %d appended to array\n",elem);
return extended;
}
Consider what happens when you first insert a value into an empty array. 考虑第一次将值插入空数组时会发生什么。 Your dn_append
code has: 您的dn_append
代码具有:
a->data[(a->size-1)] = elem;
a->size++;
When a->size
is 0, this writes into index -1
, off the start of the array. 当a->size
为0时,它会从数组的开头写入索引-1
。 This apparently overwrites some of malloc's internal data, causing it to think the pointer is invalid when you later realloc
it... 这显然会覆盖一些malloc的内部数据,使其认为指针是无效的,当你以后realloc
它...
a->data = (int *)calloc(capacity,capacity * sizeof(int));
The second parammeter is wrong; 第二个参数错误; you should remove the capacity, and use only sizeof(int)
: 您应该删除容量,并仅使用sizeof(int)
:
a->data = (int*) calloc(capacity, sizeof(int));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.