C语言 Realloc failure



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

文本文件是这样的:

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)编码到包装器中吗?

我的输出看起来像这样,

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;
}

我发现,当我从大小 = 2 开始时,我会在早期崩溃。但是,如果我从更大开始,比如说,从 3 开始,就很难复制错误。也许如果我尝试j =更高的值?

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);

最新更新