2

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

文本文件是这样的:

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

4

1 回答 1

2

From the realloc man page:

...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.

I believe the problem is that you're throwing away the return value of realloc, which could be an entirely different pointer from what you passed in.

I see that in your resize function you're trying to use the return value, but you have a bug in that function that ends up throwing the return value on the floor. Your resize needs to be:

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

and then call it this way:

if (line == cursize) resize(&table, &cursize);
于 2012-05-22T23:32:03.323 回答