的malloc分配内存比RAM更 [英] Malloc allocates memory more than RAM

查看:194
本文介绍了的malloc分配内存比RAM更的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是执行的,在一个12 MB的计算机mallocs 13 MB的程序(QEMU模拟!)。不仅如此,我甚至通过记忆浏览,并在其中填满垃圾......

I just executed a program that mallocs 13 MB in a 12 MB machine (QEMU Emulated!) . Not just that, i even browsed through the memory and filled junk in it...

void 
large_mem(void) 
{
  #define LONGMEM  13631488
  long long *ptr = (long long *)malloc(LONGMEM);
  long long i;
  if(!ptr) {
     printf("%s(): array allocation of size %lld failed.\n",__func__,LONGMEM);
     ASSERT(0);
  }
  for(i = 0 ; i < LONGMEM ; i++ ) { 
    *(ptr+i)=i;
  }
  free(ptr);
}

这怎么可能?我期待分段错误。

How is it possible ? I was expecting a segmentation fault.

推荐答案

这就是所谓的虚拟内存这是分配给你的程序。这不是的真正的内存,您拨打的RAM。

It's called virtual memory which is allocated for your program. It's not real memory which you call RAM.

有对虚拟内存最大限制为好,但它比RAM高。它是由你的操作系统实现(定义)。

There is a max limit for virtual memory as well, but it's higher than RAM. It's implemented (and defined) by your operating system.

这篇关于的malloc分配内存比RAM更的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆