#include <stdio.h>
#include <stdlib.h>
int count = 0;
int * new_array() {
int i, *array = (int *) malloc(sizeof(int) * 9);
for(i = 0; i <= 9; i++)
array[i] = count++;
for(i = 0; i <= 9; i++)
printf("%d ", array[i]);
printf("\n");
return array;
}
int main(void) {
int i;
int *a;
for(i = 0; i < 10; i++) {
a = new_array();
}
return 0;
}
This works fine in 64-bit and the output is as expected.
However, in 32-bit, the output turns out to be: 0 1 2 3 4 5 6 7 8 9
And the error message appears:
prog: malloc.c:2372: sysmalloc: Assertion `(old_top == (((mbinptr) (((char *) &((av)->bins[((1) - 1) * 2])) - __builtin_offsetof (struct malloc_chunk, fd)))) && old_size == 0) || ((unsigned long) (old_size) >= (unsigned long)((((__builtin_offsetof (struct malloc_chunk, fd_nextsize))+((2 *(sizeof(size_t))) - 1)) & ~((2 *(sizeof(size_t))) - 1))) && ((old_top)->size & 0x1) && ((unsigned long) old_end & pagemask) == 0)' failed.
I don't understand why. As I'm concerned, it also should fail in 64-bit, since I only allocated 9 integer-size but accessed the 10th element of array. If this is the case, why I should care about the length? I can just give a random number as the length.