我正在用 C 语言编写一个简单的堆栈程序,最后有一个灵活的 void* 数组。当需要将更多元素推入数组时,我使用 realloc() 创建一个新堆栈,释放旧堆栈并将新堆栈分配给旧堆栈。(目前我没有 pop() 函数)。
#include <stdlib.h>
#include <stdio.h>
typedef struct Stack
{
int top, length;
void* start_ptr[];
} Stack;
Stack* init_stack(int n_items)
{
Stack* stack;
stack = malloc(sizeof(int) * 3 + sizeof(void*) * n_items);
stack->top = -1;
stack->length = n_items;
return stack;
}
Stack* increase_stacksize(Stack* stack, int n_itemsToAdd)
{
Stack* newstack;
newstack = realloc(stack, sizeof(*stack) + sizeof(void*) * (n_itemsToAdd + stack->length));
if(newstack != NULL)
//printf("\nDebug print - array reallocated\n");
free(stack);
stack = newstack;
stack->length += n_itemsToAdd;
return stack;
}
Stack* push(Stack* stack, void* item)
{
if(stack->top + 1 == stack->length){
stack = increase_stacksize(stack, 10);
}
int pos = stack->top + 1;
stack->start_ptr[pos] = item;
++(stack->top);
return stack;
}
void printstack(Stack* stack)
{
printf("Number of items in the stack = %d\n", stack->top + 1);
printf("Capacity of the stack = %d\n", stack->length);
printf("Elements in the stack are: \n");
int i;
for(i = 0; i <= stack->top; i++){
int* item_ptr;
void* address;
address = stack->start_ptr[i];
item_ptr = (int*)address;
printf("Position = %d, Item = %d \n", i, *item_ptr);
}
}
int main(void)
{
Stack* stack;
stack = init_stack(5);
int a[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int i;
for(i = 0; i < 10; i++)
{
stack = push(stack, (void*)(a+i));
}
printstack(stack);
free(stack);
return 1;
}
问题是当我在我完成后释放() main() 中的堆栈时,会在 stderr 中打印一条消息:
*** glibc detected *** ./prog: double free or corruption (fasttop): 0x096ee008 ***
======= Backtrace: =========
/lib/libc.so.6[0xb7721fd4]
/lib/libc.so.6(cfree+0x9c)[0xb772387c]
./prog[0x8048731]
./prog[0x8048461]
======= Memory map: ========
虽然 printstack() 的输出很好。即使我删除了 free(stack) 行,我也会在ideone.com中收到运行时错误,但没有 stderr 消息。我猜这可能是由于零大小的数组。这里可能是什么问题?