0

我正在编写一个变量列表实现。在我的插入步骤中,我检查数组的大小是否已达到最大值,如果是,我将最大容量加倍并调用 realloc() 为我分配一些新内存。从大小 2 到 4、4 到 8 和 8 到 16 工作正常,但是从大小 16 到 32 给我的数组中的一些随机零。谁能告诉我这里发生了什么?malloc我知道我可以通过ing 一些新空间,使用memcpy然后ing 旧指针来避免使用 realloc() free......也许这样做不会影响性能。但我的直觉告诉我确实存在,而且无论如何我认为这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, 
4

1 回答 1

4

这样写是非常糟糕的:

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

您应该使用新变量,如果内存被重新分配,您可以这样写:

List->data = temp;

它将保护您免受内存泄漏。

而你忘记了*sizeof(int)

于 2012-11-16T07:16:55.987 回答