Realloc失败

时间:2012-05-22 21:50:34

标签: c memory memory-management realloc

我正在编写一个程序,扫描文本文件中每行开头的字节偏移量。字节偏移量存储在一个长整数数组中。

文本文件是:

123456
123
123456789
12345
123
12
1
12345678
12345

出于测试目的,数组的大小从2开始,以便我可以测试我的Realloc函数是否有效。

我逐行扫描,计算每一行的长度,并将其存储为字节偏移量。假设声明/初始化此循环之前的所有内容:

while(fgets(buffer, BUFSIZ, fptr) != NULL)
    {
        otPtr[line] = offset; 
        len = strlen(buffer)*sizeof(char);
        offset += len;
        printf("%03d %03d %03d %s", line, otPtr[line], len, buffer);
        line++;
        if(line == cursize) resize(table, &cursize);
    }

如果行号==数组的大小,循环即将使用最后一个数组元素,所以我调用resize。在resize()中,我想将字节偏移值数组的当前大小加倍。

值得注意的是我使用的是“Realloc()”而不是“realloc()”因为我有一个包装库来检查realloc错误然后在失败时存在。

int resize(long int* table, int* cursize)
{
    long int* ptr;
    *cursize *= 2;
    printf("Reallocating to %d elements...\n", *cursize);
    ptr = (long int*)Realloc(table, (*cursize)*sizeof(long int));
    if(ptr != NULL)
        table = ptr;
}

(另外,将realloc包装在另一个函数中是多余的,然后将它包装在另一个函数中?或者这样可以用于此目的吗?或者我应该将if(ptr!= NULL)编码到包装器中?)< / p>

我的输出看起来像这样,

Building offset table...
Sizeof(char) in bytes: 1
001 000 008 123456
002 008 005 123
003 013 011 123456789
Reallocating to 8 elements...
004 024 007 12345
005 031 005 123
006 036 004 12
007 040 003 1
Reallocating to 16 elements...
Realloc error
Process returned 0 (0x0)   execution time : 0.031 s
Press any key to continue.

其中三列只是行号//字节偏移量//该行的字节长度,最后一行只是该行文本的打印输出(此输出来自循环首次扫描的部分)用于计算偏移量的文件,如果不清楚,我只需打印出缓冲区以确保其正常工作。)

为什么我会收到Realloc错误?

以下是Realloc的实现:

void *Realloc(void *ptr, size_t numMembers)
{
     void *newptr;

     if ((newptr = (void *) realloc(ptr, numMembers)) == NULL)
     {
         printf("Realloc error");
         exit(1);
     }
     return newptr;
}

这是一个最小的测试用例。从来没有写过其中的一个,但我认为这是“正确的。”

#include<stdio.h>
#include<stdlib.h>

void *Realloc(void *ptr, size_t numMembers);

int main(void)
{
    void resize(int** table, int* size);
    int size = 2;
    int j = 15;
    int i = 0;
    int* table;

    table = (int*)calloc(size, sizeof(int));

    while(i<j)
    {
        printf("Give number: ");
        scanf("%d", &table[i]);
        i++;
        if(i == size) resize(&table, &size);
    }
    i = 0;
    printf("Printing table...\n");
    while(i < j)
    {
        printf("%d ", table[i]);
        i++;
    }
}

void *Realloc(void *ptr, size_t numMembers)
{
     void *newptr;

     if ((newptr = (void *) realloc(ptr, numMembers)) == NULL)
     {
         printf("Realloc error");
         exit(1);
     }
     return newptr;
}

void resize(int** table, int* size)
{
        int* ptr;
        *size *= 2;
        printf("Reallocating to %d...\n", *size);
        ptr = (int*)Realloc(*table, *size*sizeof(int));
        if(ptr != NULL)
            table = ptr;
}

我发现当我从size = 2开始时,我很早就崩溃了。但是如果我从3开始变大,那么很难复制错误。也许如果我尝试j =更高的值?

1 个答案:

答案 0 :(得分:2)

来自realloc手册页:

  

...返回指向新分配的内存的指针,即   适合任何类型的变量,可能不同   ptr ,如果请求失败,则为NULL。

我认为问题在于你丢弃了realloc的返回值,这可能与你传入的内容完全不同。

我在你的resize函数中看到你正在尝试使用返回值,但是你在该函数中有一个错误,最终会将返回值抛到地板上。您需要resize

int resize(long int** pTable, int* cursize)
{
    long int* ptr;
    *cursize *= 2;
    printf("Reallocating to %d elements...\n", *cursize);
    ptr = (long int*)Realloc(*pTable, (*cursize)*sizeof(long int));
    if(ptr != NULL)
        *pTable = ptr;
}

然后以这种方式调用它:

if (line == cursize) resize(&table, &cursize);