Search code examples
cmemorymemory-managementrealloc

Realloc failure


I'm writing a program that scans a text file for the byte offset of the beginning of each line. The byte offsets are stored in an array of long ints.

The text file is this:

123456
123
123456789
12345
123
12
1
12345678
12345

For testing purposes, the size of the array starts at 2, so that I can test to see if my Realloc function works.

I scan line by line, calculating the length of each line, and storing it as a byte offset. Assume everything before this loop is declared/initialized:

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

if the line number == the size of the array, the loop is about to use the last array element, so I call resize. In resize(), I want to double the current size of the array of byte offset values.

It's worth noting that I'm using "Realloc()" rather than "realloc()" because I have a wrapper library that checks for realloc error then exists upon failure.

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

(Also, is it redundant to wrap realloc inside another function, then wrap it in yet another function? Or is this fine for this purpose? Or should I code the if(ptr!=NULL) into the wrapper?)

My output looks something like this,

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.

Where the three columns are just line number//byte offset//byte length of that line, and the last line is just the printout of the text of that line (this output is from the part where the loop first scans through the file to calculate the offsets, in case it's not clear, I just printout the buffer to be sure it's working.)

Why would I get a Realloc error?

Here is the implementation of Realloc:

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

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

Here's a minimal test-case. Never written one of these before, but I think this is "correct."

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

What I've found is that when I start with size = 2, I get a crash early on. But if I start bigger, say, at 3, it's very hard to replicate the error. Maybe if I try j = a higher value?


Solution

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