检测到glibc - 双重免费或腐败

时间:2012-11-13 16:34:02

标签: c arrays memory-leaks glibc

我在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: ========

输出upto printstack()很好。即使我删除了free(stack)行,我在ideone.com中也遇到了运行时错误,但是没有stderr消息。我猜它可能是由于零大小的数组。这可能是什么问题?

2 个答案:

答案 0 :(得分:6)

如有必要,

realloc会释放您的旧版块,因此您应删除free(stack)

中的increase_stacksize

答案 1 :(得分:2)

realloc然后释放原始stack。只需删除那里的free

free(stack);   // remove this line
stack = newstack;

成功realloc后,函数返回的地址不能保证与原始地址相同。如果它是相同的,你最终释放内存两次。如果它不相同,你最终会释放一个未分配的内存位置。

简而言之,free之后不要realloc原作。