Search code examples
carrayspointersmallocrealloc

Dinamically growing Array until key input Error: invalid next size


I would to extend the array until the program receive "-1" as input. Every time I extend by 1 the current array but after 7 input I have "invalid next size:0x0000000000d47010"

void extend(int *v, int i)
{
    int *p;
    p= (int *) realloc(v, (i+1)*sizeof(int));
    if(p!=NULL)
    v=p;    
}
int main()
{
    int *v;
    v= malloc(sizeof(int));
    int n=0;
    int i=0;

    while(n!=-1)
    {
    scanf("%d", &n);
    v[i]=n;
    extend(v,i);
    i++;
    }
    return 0;
}

EDIT possible solution that made this working

void extend(int *v, int i)
{
    int *p;
    p= (int *) realloc(v, (i+1)*sizeof(int));
    if(p!=NULL)
    v=p;    
}

int main()
{
        int *v;
        v= malloc(sizeof(int));
    int n=0;
    int i=0;

        while(n!=-1)
        {
        scanf("%d", &n);
        if(n!=-1)
        {
        extend(v,i);
        v[i]=n;
        i++;
        }
    }

    prodottoScalare(v, i);

        return 0;
}

Solution

  • Function parameters are its local variables. So in the function extend in this statement

    v=p;
    

    there is assigned local variable v that is destroyed after exiting the function. The original pointer itself used as the argument will not be changed.

    You need to pass the original pointer to the function by reference.

    Also the logic of this while loop

    while(n!=-1)
    {
    scanf("%d", &n);
    v[i]=n;
    extend(v,i);
    i++;
    }
    

    is incorrect. The entered value by the scanf can be equal to -1 or the end of the stream can be encountered. Nevertheless the array is extended.

    Also you should free all allocated memory.

    Take into account that in C main without parameters should be declared like

    int main( void )
    

    The program can look the following way

    #include <stdlib.h>
    #include <stdio.h>
    
    size_t extend( int **a, size_t n, int value )
    {
        int *p = realloc( *a, ( n + 1 ) * sizeof( int ) );
    
        if ( p != NULL )
        {
            *a = p;
            ( *a )[n++] = value;
        }
    
        return n;
    }
    
    int main( void ) 
    {
        int *a = NULL;
        size_t n = 0;
        int value;
    
        while( scanf( "%d", &value ) == 1 && value != -1 )
        {    
            n = extend( &a, n, value );    
        }
    
        for ( size_t i = 0; i < n; i++ ) printf( "%d ", a[i] );
        printf( "\n" );
    
        free( a );
    
        return 0;
    }
    

    If to enter

    0 1 2 3 4 5 6 7 8 9 -1
    

    then the program output will look like

     1 2 3 4 5 6 7 8 9 
    

    Another function's implementation can look like

    #include <stdlib.h>
    #include <stdio.h>
    
    _Bool /* or int */ extend( int **a, size_t n )
    {
        int *p = realloc( *a, ( n + 1 ) * sizeof( int ) );
        _Bool /* or int */ success = p != NULL;
    
        if ( success ) *a = p;
    
        return success;
    }
    
    int main( void ) 
    {
        int *a = NULL;
        size_t n = 0;
        int value;
    
        while( scanf( "%d", &value ) == 1 && value != -1 )
        {    
            if ( extend( &a, n ) ) a[n++] = value;    
        }
    
        for ( size_t i = 0; i < n; i++ ) printf( "%d ", a[i] );
        printf( "\n" );
    
        free( a );
    
        return 0;
    }
    

    The result will be the same as in the first demonstrative program for the same input.