C 为什么printf中断内存分配?
我尝试分配内存,代码:C 为什么printf中断内存分配?,c,C,我尝试分配内存,代码: #include <stdio.h> #include <stdlib.h> #include <malloc.h> int main(){ printf("Allocating memory...\n"); int* a = calloc(1000000000, sizeof(int)); printf("Done! Allocated memory: %zu\n",
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
int main(){
printf("Allocating memory...\n");
int* a = calloc(1000000000, sizeof(int));
printf("Done! Allocated memory: %zu\n", malloc_usable_size(a));
getchar();
return 0;
}
开始时printf的差异。有什么问题吗?“但是内存没有分配”,你怎么知道,“这个内存量”实际上只有4GB,是的,我希望它会过度分配。这基本上意味着“它只显示”它实际使用的内存,并且只在你使用它们时显示4GB。我认为你不会从这个实验中得到任何有用的见解,因为我怀疑它是否具有确定性。我推测这种差异是由于
printf
在内部使用malloc
,并在分配器状态中更改了某些内容。是的,我知道。。。但本例中的保留内存量是一个完全无用的指标,因为它是特定于实现的,而且基本上编译器可以自由地保留它想要的内存(最多4GB)。读一读。这始终取决于您的平台。“但是内存没有分配”,您怎么知道,“这个内存量”实际上只有4GB,是的,我希望它会过度分配。这基本上意味着“它只显示”它实际使用的内存,并且只在你使用它们时显示4GB。我认为你不会从这个实验中得到任何有用的见解,因为我怀疑它是否具有确定性。我推测这种差异是由于printf
在内部使用malloc
,并在分配器状态中更改了某些内容。是的,我知道。。。但本例中的保留内存量是一个完全无用的指标,因为它是特定于实现的,而且基本上编译器可以自由地保留它想要的内存(最多4GB)。读一读。这始终取决于您的平台。
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
int main(){
//printf("Allocating memory...\n");
int* a = calloc(1000000000, sizeof(int));
printf("Done! Allocated memory: %zu\n", malloc_usable_size(a));
getchar();
return 0;
}