realloc()搞砸了我的数据......发生了什么事?

时间:2012-11-16 07:09:13

标签: c realloc

我正在编写一个变量列表实现。在我的插入步骤中,我检查数组的大小是否已超出,如果是,我将最大容量加倍并调用realloc()为我分配一些新内存。从大小2到4,4到8和8到16工作正常,但是从大小16到32给我在我的数组中有一些随机零。谁能告诉我这里发生了什么?我知道我可以通过malloc使用memcpy然后free使用旧指针来避免使用realloc(),并且可能不会因为这样做而受到影响。但是我的直觉告诉我,有,但无论如何我认为那是realloc的目的。谁能告诉我发生了什么事?此代码中的关键功能是append函数。

#include <stdio.h>
#include <stdlib.h>
#include "problem5.h"
#define FAILURE -1
#define SUCCESS 0

ArrayList ArrayList_Init(int n, int (*append) (ArrayList, int), void (*print) (ArrayList), void (*insert) (ArrayList, int, int), void (*destroy) (ArrayList), int (*valueOf) (ArrayList, int)) 
{
    ArrayList newArrayList = (ArrayList) malloc(sizeof(ArrayList_));
    newArrayList->max_size = n;
    newArrayList->current_size = 0;
    newArrayList->data = malloc(n*sizeof(int));

    newArrayList->append = append;
    newArrayList->destroy = destroy;
    newArrayList->print = print;
    newArrayList->insert = insert;
    newArrayList->valueOf = valueOf;

    return newArrayList;
}// init a new list with capacity n

int append_(ArrayList list, int val)
{
    //if the array is at maximum capacity
        //double the capacity
        //update max_size
    //insert the value in the first open spot in the array (aka index current_size)
    //increment current_size
    if (list->current_size == list->max_size) {
        list->max_size *= 2;
        if (( list->data = realloc(list->data, list->max_size) ) == NULL)
            return FAILURE;
    }
    list->data[list->current_size] = val;
    list->current_size++;
    return SUCCESS;
}

void print_(ArrayList list)
{
    int i;
    printf("List of size %d, max size %d. Contents:\n", list->current_size, list->max_size);
    for (i=0; i<list->current_size; i++)
        printf("%d, ", list->data[i]);
    printf("\n");
}

void insert_(ArrayList list, int val, int index) {

}// insert val into index
void destroy_(ArrayList list)
{
    //free list memory
}
int valueOf_(ArrayList list, int index)
{
    //return value of specified element
    return 0;
}

int main()
{
    ArrayList list;
    int stat, count = 0;

    list = ArrayList_Init(2, append_, print_, insert_, destroy_, valueOf_); // init a new list with capacity 8
    do {
        printf("Appending %d\n", count);
        stat = list->append(list, count) ; // add val to end of the list
        list->print(list);
    } while (stat == SUCCESS && ++count < 20);

    return 0;
}

这是输出:

Appending 0
List of size 1, max size 2. Contents:
0, 
Appending 1
List of size 2, max size 2. Contents:
0, 1, 
Appending 2
List of size 3, max size 4. Contents:
0, 1, 2, 
Appending 3
List of size 4, max size 4. Contents:
0, 1, 2, 3, 
Appending 4
List of size 5, max size 8. Contents:
0, 1, 2, 3, 4, 
Appending 5
List of size 6, max size 8. Contents:
0, 1, 2, 3, 4, 5, 
Appending 6
List of size 7, max size 8. Contents:
0, 1, 2, 3, 4, 5, 6, 
Appending 7
List of size 8, max size 8. Contents:
0, 1, 2, 3, 4, 5, 6, 7, 
Appending 8
List of size 9, max size 16. Contents:
0, 1, 2, 3, 4, 5, 6, 7, 8, 
Appending 9
List of size 10, max size 16. Contents:
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 
Appending 10
List of size 11, max size 16. Contents:
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 
Appending 11
List of size 12, max size 16. Contents:
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 
Appending 12
List of size 13, max size 16. Contents:
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 
Appending 13
List of size 14, max size 16. Contents:
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 
Appending 14
List of size 15, max size 16. Contents:
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 
Appending 15
List of size 16, max size 16. Contents:
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 
Appending 16
List of size 17, max size 32. Contents:
0, 1, 2, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 16, 
Appending 17
List of size 18, max size 32. Contents:
0, 1, 2, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 16, 17, 
Appending 18
List of size 19, max size 32. Contents:
0, 1, 2, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 16, 17, 18, 
Appending 19
List of size 20, max size 32. Contents:
0, 1, 2, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 16, 17, 18, 19, 

1 个答案:

答案 0 :(得分:4)

这样写是非常糟糕的:

list->data = realloc(list->data, list->max_size)

您应该使用新变量,如果重新分配了内存,请写下:

List->data = temp;

它可以保护您免受内存泄漏。

你忘了*sizeof(int)