0

I've just started learning malloc() and realloc() and when testing them, I came across this issue with reallocating the size of an int array.

the program is supposed to make an array, initially of size two, but it's supposed to increase its size and add values to it ten times. However it doesn't increase its size and the output ends up being array = {0,1} when it should be array = {0,1,2,3,4,5,6,7,8,9}

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

int main(void) {
    int *array= malloc(sizeof(int)*2);
    for (int x = 0; x < 10; x++) {
        array = realloc(array, sizeof(int)*(2+x));
        array[x] = x;
    }
    for (int i = 0; i<(sizeof(array)/sizeof(array[0])); i++) {
        printf("%d\n",array[i]);
    }
    free(array);
}

could someone explain why it doesn't work?? I've tried looking for answers but none of this makes sense to me lol.

  • 2
    `sizeof(pointer)` is always the same no matter how much memory it points to. That information is not baked in, you'll need to keep track of the memory size manually. – yano Nov 22 '22 at 15:17
  • Because `array` is a _pointer_, `sizeof(array)` is `sizeof(int *)` which is 4 bytes on a 32 bit machine and 8 bytes on a 64 bit machine. For most [modern] machines, `sizeof(int)` is 4 [but could be 2 or even 8], so we end up dividing a fixed number by a fixed number based on the architecture. So, we could have: 1, 2, 4, 8 as values. But, never something like 1000. – Craig Estey Nov 22 '22 at 15:19
  • 1
    Store a count of the number of elements in the array, and adjust that count when you allocate more space. Don't forget to check that the memory allocations worked. – Jonathan Leffler Nov 22 '22 at 15:19

2 Answers2

1

The expression

sizeof(array)/sizeof(array[0])

is equivalent to

sizeof( int * )/sizeof( int )

and yields either 2 or 1 depending on the used system.

Also the expression (2+x) in this statement

array = realloc(array, sizeof(int)*(2+x));

used in each iteration of the for loop does not make sense.

It seems you mean something like the following

enum { N = 10 };
size_t n = 2;

int *array = malloc( n * sizeof( int ) );
for ( int x = 0; x < N; x++ ) {
    if ( n <= x )
    {
        n += 2;
        array = realloc( array, n * sizeof( int ) );
    }
    array[x] = x;
}

for ( int i = 0; i < N; i++ ) {
    printf("%d\n",array[i]);
}

free(array);

In general it is safer to use an intermediate pointer in the call pf realloc like for example

int *tmp = realloc( array, n * sizeof( int ) );
if ( tmp != NULL ) array = tmp;

Otherwise you can loose the allocated memory if the call of realloc will return a null pointer.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
0

The problem is that sizeof(array) will just return the size of the pointer (8 bytes on a 64-bit system). You need to track the array size in another variable. For example...

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

int main(void) {
    int *array= malloc(sizeof(int));
    int size;
    for (size = 0; size < 10; size++) {
        array = realloc(array, sizeof(int)*(1+size));
        array[size] = size;
    }
    for (int i = 0; i<size; i++) {
        printf("%d\n",array[i]);
    }
    printf("array size: %ld\n",size*sizeof(int));
    free(array);
}