#include "stdlib.h"
#include "stdio.h"
#include "string.h"
int main(int argc, char* argv[])
{
int *test = malloc(15 * sizeof(int));
for(int i = 0;i < 15 ;i ++ )
printf("test is %i\n",test[i]);
memset(test,0,sizeof(int) * 15);
for(int i = 0 ; i < 15; i ++ )
printf("test after memset is %i\n",test[i]);
return 0;
}
我得到的输出很奇怪:
test is 1142126264
test is 32526
...
test is 1701409394
test is 1869348978
test is 1694498930
test after memset is 0
test after memset is 0
test after memset is 0
test after memset is 0
test after memset is 0
...
test after memset is 0
test after memset is 0
test after memset is 0
test after memset is 0
test after memset is 0
为什么会这样?我以为我刚刚malloc
编辑了一些可以使用的新记忆?
那么这个怎么样:
int test[15];
我必须打电话
memset(&test,0,sizeof(int) * 15);
吗?