c - 重新分配失败

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

我的输出看起来像这样,

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 = 更高的值?

最佳答案

来自 realloc 手册页:

...returns a pointer to the newly allocated memory, which is suitably aligned for any kind of variable and may be different from ptr, or NULL if the request fails.

我认为问题在于您丢弃了 realloc 的返回值,它可能是与您传入的指针完全不同的指针。

我发现在您的 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);

关于c - 重新分配失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10710695/

相关文章:

c - 使用 malloc() 函数和指针理解代码

c - 如何从矮人信息中获取结构成员偏移量?

memory - 保留内存的设备驱动程序

c - 指示哪个线程设置计时器处理程序?

java - 如果每个线程都必须访问相同的数组,则会影响性能

iphone - Iphone App在4.0或更低版本的SDK上崩溃

c++ - 指针指针的自动内存管理

javascript - 一旦内存达到 4GB 标记,如何防止 Microsoft Edge 重新加载我的页面?

C 宏表达式返回值

c - 在 C 中访问传递给具有空参数列表的函数的参数