在这个问题,有人建议意见,我应该不会投的结果malloc,即
int *sieve = malloc(sizeof(int) * length);
Run Code Online (Sandbox Code Playgroud)
而不是:
int *sieve = (int *) malloc(sizeof(int) * length);
Run Code Online (Sandbox Code Playgroud)
为什么会这样呢?
我知道将动态分配的数组的长度传递给操作它们的函数是一种常见的约定:
void initializeAndFree(int* anArray, size_t length);
int main(){
size_t arrayLength = 0;
scanf("%d", &arrayLength);
int* myArray = (int*)malloc(sizeof(int)*arrayLength);
initializeAndFree(myArray, arrayLength);
}
void initializeAndFree(int* anArray, size_t length){
int i = 0;
for (i = 0; i < length; i++) {
anArray[i] = 0;
}
free(anArray);
}
Run Code Online (Sandbox Code Playgroud)
但是如果我无法从指针中获取已分配内存的长度,那么free()当我提供的内容完全相同时,"自动"知道如何解除分配?作为一名C程序员,为什么我不能进入魔术?
从哪里free()获得免费(har-har)知识?
我试图弄清楚在分配失败之前我可以分配多少内存.
这个简单的C++代码分配一个缓冲区(大小为1024字节),分配缓冲区的最后五个字符,报告,然后删除缓冲区.然后它将缓冲区的大小加倍并重复直到它失败.
除非我遗漏了某些东西,否则代码能够在MacBook Pro出现故障之前分配高达65TB的内存.这甚至可能吗?它如何分配比我在机器上更多的内存?我一定很遗憾.
int main(int argc, char *argv[])
{
long long size=1024;
long cnt=0;
while (true)
{
char *buffer = new char[size];
// Assume the alloc succeeded. We are looking for the failure after all.
// Try to write to the allocated memory, may fail
buffer[size-5] = 'T';
buffer[size-4] = 'e';
buffer[size-3] = 's';
buffer[size-2] = 't';
buffer[size-1] = '\0';
// report
if (cnt<10)
cout << "size[" << cnt << "]: " << (size/1024.) << "Kb ";
else if …Run Code Online (Sandbox Code Playgroud) 我正在玩,了解可以分配多少内存.最初我认为可以分配的最大内存等于物理内存(RAM).我通过运行命令检查了Ubuntu 12.04上的RAM,如下所示:
~$ free -b
total used free shared buffers cached
Mem: 3170848768 2526740480 644108288 0 265547776 1360060416
-/+ buffers/cache: 901132288 2269716480
Swap: 2428497920 0 2428497920
Run Code Online (Sandbox Code Playgroud)
如上所示,总物理内存为3Gig(3170848768字节),其中只有644108288字节是空闲的,所以我假设我最多只能分配这么多内存.我通过编写下面只有两行的小程序来测试它:
char * p1 = new char[644108290] ;
delete p1;
Run Code Online (Sandbox Code Playgroud)
由于代码运行完美,这意味着它成功分配了内存.此外,我尝试分配的内存大于可用的物理空闲内存,但它没有抛出任何错误.然后每个问题
我认为它必须使用虚拟内存.所以我测试了免费交换内存的代码,它也工作.
char * p1 = new char[2428497920] ;
delete p1;
Run Code Online (Sandbox Code Playgroud)
我试图分配免费交换加上可用RAM字节的内存
char * p1 = new char[3072606208] ;
delete p1;
Run Code Online (Sandbox Code Playgroud)
但是这次代码失败了抛出bad_alloc异常.为什么代码这次不起作用.
现在我在编译时在新程序中分配了内存,如下所示:
char p[3072606208] ;
char p2[4072606208] ;
char p3[5072606208];
cout<<"Size of array p = " <<sizeof p <<endl;
cout<<"Size …Run Code Online (Sandbox Code Playgroud) 我mmap()用来将共享内存对象映射到进程。我的问题分为两部分:
1)mmap()linux进程的大小限制是多少?(有此限制吗?)
2)进程运行一段时间后,我认为进程虚拟内存地址空间将以某种方式分散。这会影响我mmap()在此过程中可以做的最大尺寸吗?
使用的linux内核是2.6.27。共享内存对象的大小约为32MB。我试图访问mmap()由于没有足够的虚拟地址空间而导致此类共享内存对象失败的可能性。
首先,我注意到当malloc内存与calloc时,内存占用量不同.我正在使用几GB的数据集.这些数据是随机的.
I expected that I could just malloc a large amount of memory and read whatever random data was in it cast to a float. However, looking at the memory footprint in the process viewer the memory is obviously not being claimed (vs. calloc where I see a large foot print). I ran a loop to write data into the memory and then I saw the memory footprint climb. Am I correct in saying that the memory isn't actually claimed …
为了测试内存不足行为,我使用GCC 4.7.1在32位Linux 3.2上编译了以下C程序,没有任何编译器标志:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
int main() {
while (malloc(4096)) ;
printf("%s", strerror(errno));
return 0;
}
Run Code Online (Sandbox Code Playgroud)
当我运行程序时,我发现在分配了大约2.5 GB的驻留内存后,malloc失败("无法分配内存").
该机器具有2GB的物理内存和4GB的交换空间.在程序运行期间没有观察到内核消息.
那么为什么Linux停止发布内存呢?
相关问题:malloc可以分配的最大内存,但它没有解决Linux的细节问题.
可以从程序分配的内存量是否有限制?我的意思是,是否有任何程序保护,例如,在无限循环中分配内存?
什么时候调用malloc()返回NULL指针?
可能重复:
malloc可以分配的最大内存!
我怎么知道我可以占用的堆的最大大小malloc().我使用的是MS Visual Studio 2010.
c ×6
linux ×4
malloc ×4
memory ×4
c++ ×2
allocation ×1
casting ×1
free ×1
gcc ×1
heap ×1
mmap ×1
new-operator ×1
system-calls ×1
ubuntu ×1