【发布时间】:2013-12-21 02:44:46
【问题描述】:
当指针数组的大小本身为 4 并且当我尝试打印第 5 个值时,它会给出一个随机数。如何?告诉我这种随机分配是如何发生的。谢谢!
#include< stdio.h>
#include< stdlib.h>
int main()
{
int* p_array;
int i;
// call calloc to allocate that appropriate number of bytes for the array
p_array = (int *)calloc(4,sizeof(int)); // allocate 4 ints
for(i=0; i < 4; i++)
{
p_array[i] = 1;
}
for(i=0; i < 4; i++)
{
printf("%d\n",p_array[i]);
}
printf("%d\n",p_array[5]); // when the size of pointer array is itself 4 and when i try to print 5th value it gives a random number.How?
free(p_array);
return 0;
}
【问题讨论】:
标签: pointers memory heap-memory calloc