Using Realloc in C

14,864

Solution 1

Yes is the short answer. Here's how it would look:

if ( i >= buffer_size )
{
    temp = realloc(buffer, buffer_size*2);
    if (!temp)
        reportError();
    buffer_size *= 2;
    buffer = temp;
}

Note that you still need to use a temporary pointer to hold the result of realloc(); if the allocation fails you still have the original buffer pointer to the still-valid existing buffer.

Solution 2

Realloc is pretty much exactly what you're looking for - you can replace that entire block inside the if ( i >= buffer_size ) with something like:

buffer = (char*)realloc(buffer, buffer_size*2);
buffer_size *= 2;

Notice that this ignores the error condition (if the return from realloc is NULL); catching this condition is left to the reader.

Solution 3

Yes, realloc could be used to slightly simplify your code. If you're not interested in error-handling, then this:

char *tmp = malloc(size*2);
memcpy(temp, buffer, size);
free(buffer);
buffer = tmp;

is essentially equivalent to this:

buffer = realloc(buffer, size*2);

If you are interested in error-handling (and you probably should be), then you will need to check for NULL return values. This is true of your original code too.

Solution 4

Yes, to simplify your code, you can replace

if ( i >= buffer_size )
{
    temp = (char*)malloc(buffer_size*2);
    memcpy( temp, buffer, buffer_size );
    free( buffer );
    buffer_size *= 2;
    buffer = temp;
}

with

if ( i >= buffer_size )
    buffer = realloc(buffer, buffer_size *= 2);

This does not take into account error checking, so you will need to check to make sure realloc doesn't return NULL.

Share:
14,864
PnP
Author by

PnP

Updated on June 08, 2022

Comments

  • PnP
    PnP about 2 years

    Its really a post for some advice in terms of the use of realloc, more specifically, if I could make use of it to simplify my existing code. Essentially, what the below does, it dynamically allocate some memory, if i goes over 256, then the array needs to be increased in size, so I malloc a temp array, with 2x the size, memcpy etc. ( see below ).

    I was just wondering if realloc could be used in the below code, to simplify it, any advice, sample code, or even hints on how to implement it is much appreciated!

    Cheers.

    void reverse(char *s) {
    char p;
    
    switch(toupper(s[0])) 
    {
        case 'A': case 'E': case 'I': case 'O': case 'U':
            p = s[strlen(s)-1];
            while( p >= s )
                putchar( p-- );
            putchar( '\n' );
            break;
        default:
            printf("%s", s);
            break;
    }
    printf("\n");
        }
    
        int main(void) {
    char c;
    int buffer_size = 256;
    char *buffer, *temp;
    int i=0;
    
    buffer = (char*)malloc(buffer_size);
    while (c=getchar(), c!=' ' && c!='\n' && c !='\t') 
    {
        buffer[i++] = c;
        if ( i >= buffer_size )
        {
            temp = (char*)malloc(buffer_size*2);
            memcpy( temp, buffer, buffer_size );
            free( buffer );
            buffer_size *= 2;
            buffer = temp;
        }
    }
    buffer[i] = '\0';
    reverse(buffer);
    
    return 0;
    

    }

  • Kerrek SB
    Kerrek SB over 12 years
    realloc might return NULL, giving you an instant unrecoverable leak.
  • Graham Borland
    Graham Borland over 12 years
    -1. if the realloc fails (by returning NULL), you've lost the reference to the original block.
  • Graham Borland
    Graham Borland over 12 years
    -1. if the realloc fails (by returning NULL), you've lost the reference to the original block.
  • Oliver Charlesworth
    Oliver Charlesworth over 12 years
    @Graham: I explicitly said "if you're not interested in error-handling". The OP doesn't have any error-handling in his original code, either.
  • Kerrek SB
    Kerrek SB over 12 years
    This "error handling" isn't just some child's play like checking std::cin >> n; this one here may and will cause UB almost surely...
  • Seth Carnegie
    Seth Carnegie over 12 years
    @GrahamBorland I said you could replace his original code with that code. He didn't check for NULL there, so neither did I. But I added a note to mention it.
  • Salviati
    Salviati about 5 years
    Correct me if I am wrong, but the buffer=temp, does that not copy over the temp memory address? So if I free temp afterwards, I also free buffer? If true, when do I free the temp variable in order to not waste memory of having two identical strings allocated? Sorry I am trying to learn how this works.
  • Viaceslavus
    Viaceslavus about 2 years
    buffer = temp does copy the address of temp to buffer. But it does not allocate another string...