I'm creating an array with malloc like so:
#include <stdio.h>
#include <stdlib.h>
void main(){
int * m = malloc(3 * sizeof(int));
int i;
m[0] = 1;
m[1] = 2;
m[2] = 3;
for (i = 0; i < 20; i++){
printf("%d \n", m[i]);
}
getchar();
}
As you can see the array should be the size of three integers.
I define the first three elements and then print out 20 elements from the array. but how is this possible? if malloc allocated three integers worth of memory to the array then why does the array still contain additional data past the initial three elements? maybe i am misunderstanding how malloc works, in which case how can i define an array with a strict size that will not include random data which I did not add to it?
thanks