Home > OS >  How to properly access this realloc-ed array?
How to properly access this realloc-ed array?

Time:12-06

In this code below I am trying to create an array of ints that can be accessed from the main() function, however, Address-sanitizer gives me stack-buffer-overflow-error and I cannot figure out what I am doing wrong. What am I missing?

#include <stdlib.h>

void reallocFail(int **arrayOfInts) {
    *arrayOfInts = (int *)malloc(sizeof(int));
    for (int i = 1; i <= 10; i  ) {
        *arrayOfInts = (int *)realloc(*arrayOfInts, (i) * sizeof(int));
        *arrayOfInts[i - 1] = i;
    }
}

int main(void) {
    int *arrayOfInts;
    reallocFail(&arrayOfInts);
    return 0;
}

CodePudding user response:

For starters this memory allocation before the for loop

*arrayOfInts = (int*)malloc(sizeof(int));

is redundant. You could just write

*arrayOfInts = NULL;

Also you need to check whether memory allocation was successful.

Also this record

    *arrayOfInts[i-1] = i;

is equivalent to

    *( arrayOfInts[i-1] ) = i;

but you need

    ( *arrayOfInts )[i-1] = i;

The function can look the following way

size_t reallocFail( int **arrayOfInts, size_t n )
{
    *arrayOfInts = NULL;
    size_t i = 0;

    if ( n != 0 )
    {
        int *tmp = NULL;

        do
        {
            tmp = realloc( *arrayOfInts, ( i   1 ) * sizeof( int ) );

            if ( tmp != NULL )
            {
                tmp[i]  = i   1;
                *arrayOfInts = tmp;
            }
        } while ( tmp != NULL &&   i != n );
    }

    return i;
}

And the function can be called for example like

int *arrayOfInts = NULL;

size_t n = reallocFail( &arrayOfInts, 10 );

for ( size_t i = 0; i != n; i   )
{
    printf( "%d ", arrayOfInts[i] );
}

putchar( '\n' );

free( arrayOfInts ); 

And ignore the down-voting of the answer. There is nothing wrong in the answer.:)

Here is a demonstration program.

#include <stdio.h>
#include <stdlib.h>

size_t reallocFail( int **arrayOfInts, size_t n )
{
    *arrayOfInts = NULL;
    size_t i = 0;

    if ( n != 0 )
    {
        int *tmp = NULL;

        do
        {
            tmp = realloc( *arrayOfInts, ( i   1 ) * sizeof( int ) );

            if ( tmp != NULL )
            {
                tmp[i]  = i   1;
                *arrayOfInts = tmp;
            }
        } while ( tmp != NULL &&   i != n );
    }

    return i;
}

int main( void ) 
{
    int *arrayOfInts = NULL;

    size_t n = reallocFail( &arrayOfInts, 10 );

    for ( size_t i = 0; i != n; i   )
    {
        printf( "%d ", arrayOfInts[i] );
    }

    putchar( '\n' );

    free( arrayOfInts );

    return 0;
}

The program output is

1 2 3 4 5 6 7 8 9 10 

Of course there is no great sense to reallocate the memory in the loop within the function. The function just demonstrates how to manage the function realloc.

CodePudding user response:

void reallocFail(int **arrayOfInts) 
{
    for (int i = 1; i <= 10; i  ) 
    {
        *arrayOfInts = realloc(*arrayOfInts, i * sizeof(**arrayOfInts));
        arrayOfInts[0][i - 1] = i;
    }
}

int main(void) {
    int *arrayOfInts = NULL;
    reallocFail(&arrayOfInts);
    return 0;
}

But this code can be reduced to one realloc (I understand that you test if realloc works). IT does not check if the realloc has succeeded or failed:

void reallocFail(int **arrayOfInts) 
{
    int *tmp;
    for (int i = 1; i <= 10; i  ) 
    {
        tmp = realloc(*arrayOfInts, i * sizeof(**arrayOfInts));
        if(tmp)
        {
            *arrayOfInts = tmp;
            arrayOfInts[0][i - 1] = i;
        }
        else
            printf("Alloction error\n");
    }
}

CodePudding user response:

There is just a simple typo in *arrayOfInts[i - 1] = i;. suffix operators such as [] bind stronger than prefix operators such as *. Hence you should write:

   (*arrayOfInts)[i - 1] = i;

Note also that you should check for memory reallocation failure and you can initialize *arrayOfInts to NULL as realloc(NULL, size) is equivalent to malloc(size).

Here is a modified version:

#include <string.h>
#include <stdlib.h>

int reallocFail(int **pp, int n) {
    int i;
    *pp = NULL;
    for (i = 0; i < n; i  ) {
        int *p = realloc(*pp, (i   1) * sizeof(*p));
        if (p == NULL)
            break;
        p[i] = i   1;
        *pp = p;
    }
    return i;
}

int main(void) {
    int *arrayOfInts = NULL;
    int n = reallocFail(&arrayOfInts, 10);
    for (int i = 0; i < n; i  ) {
        printf("%d%c", arrayOfInts[i], " \n"[i == n-1]);
    }
    free(arrayOfInt);
    return 0;
}
  • Related