malloc问题

HULIHONG 2012-07-25 06:14:05
linux gcc 内存128MB,没有交换分区

#include <stdio.h>
#include <stdlib.h>

#define SIZE 1024*1024

int main(int argc, char **argv)
{
int count = 0;
char *buf = NULL;

while (1)
{
buf = (char *)malloc(SIZE);
if (!buf);
{
break;
}
// memset(buf, 1, SIZE);
printf("current allocated %d MB", count);
}
return 0;
}
注释掉红色部分和放开
结果分别是多少?
...全文
211 15 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
ies_sweet 2012-07-26
  • 打赏
  • 举报
回复
这个问题我还真不知道,如果有机会调试一下就好了
赵4老师 2012-07-26
  • 打赏
  • 举报
回复
详细请参考linux malloc源代码。
HULIHONG 2012-07-26
  • 打赏
  • 举报
回复
不过对于linux一般采用的C库是glibc,那么malloc大于128K的内存时是不会在内存中分配的,而是通过mmap在虚拟内存中分配的。又因为没有交换分区,那么就没有虚拟内存可用,count的值我认为会是1(++count),不过在相应的环境中malloc又是成功的,所以很是困惑,望大牛们不吝赐教。
HULIHONG 2012-07-26
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 的回复:]

我知道楼主要问什么了,这个涉及到Linux的内存分配

对于Linux,如果你分配了内存并不去写他,实际上并不会真的分配内存(类似于Windows的VirtualAlloc的一些概念),所以如果不加memset,count的值可以很大

但是如果加了memset,延迟分配则会生效,这时候count不可能大于128

这个问题印象中某本书说过,《unix环境高级编程》还是《深入Linu……
[/Quote]


谢谢,也许这是个比较合理的解释了
zfk198687 2012-07-25
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 的回复:]
我知道楼主要问什么了,这个涉及到Linux的内存分配

对于Linux,如果你分配了内存并不去写他,实际上并不会真的分配内存(类似于Windows的VirtualAlloc的一些概念),所以如果不加memset,count的值可以很大

但是如果加了memset,延迟分配则会生效,这时候count不可能大于128

这个问题印象中某本书说过,《unix环境高级编程》还是《深入Linux……
[/Quote]
额,学习了!
jackyjkchen 2012-07-25
  • 打赏
  • 举报
回复
我知道楼主要问什么了,这个涉及到Linux的内存分配

对于Linux,如果你分配了内存并不去写他,实际上并不会真的分配内存(类似于Windows的VirtualAlloc的一些概念),所以如果不加memset,count的值可以很大

但是如果加了memset,延迟分配则会生效,这时候count不可能大于128

这个问题印象中某本书说过,《unix环境高级编程》还是《深入Linux内核架构》,忘了
HULIHONG 2012-07-25
  • 打赏
  • 举报
回复
linux gcc 内存128MB,没有交换分区

#include <stdio.h>
#include <stdlib.h>

#define SIZE 1024*1024

int main(int argc, char **argv)
{
int count = 0;
char *buf = NULL;

while (1)
{
buf = (char *)malloc(SIZE);
if (!buf)
{
break;
}
// meset(buf, 1, SIZE);
printf("current allocated %d MB", ++count);
}
return 0;
}
jackyjkchen 2012-07-25
  • 打赏
  • 举报
回复
楼主是不是少写了代码,没有影响啊
HULIHONG 2012-07-25
  • 打赏
  • 举报
回复
linux gcc 内存128MB,没有交换分区
pathuang68 2012-07-25
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 的回复:]

#include <stdio.h>
#include <stdlib.h>

#define SIZE 1024*1024

int main(int argc, char **argv)
{
int count = 0;
char *buf = NULL;

while (1)
{
buf = (char *)malloc(SIZE);
if……
[/Quote]

meset应该是memset。

理论上memset(buf, 1, SIZE);是否被注释,输出的count应该是一样的。

count到底是多少,不同的机器应该是不一样的(因为可用的内存是不一样的)。
IVERS0N 2012-07-25
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 的回复:]

#include <stdio.h>
#include <stdlib.h>

#define SIZE 1024*1024

int main(int argc, char **argv)
{
int count = 0;
char *buf = NULL;

while (1)
{
buf = (char *)malloc(SIZE);
if……
[/Quote]

是否有memset 没影响
HULIHONG 2012-07-25
  • 打赏
  • 举报
回复
#include <stdio.h>
#include <stdlib.h>

#define SIZE 1024*1024

int main(int argc, char **argv)
{
int count = 0;
char *buf = NULL;

while (1)
{
buf = (char *)malloc(SIZE);
if (!buf)
{
break;
}
// meset(buf, 1, SIZE);

printf("current allocated %d MB", ++count);
}
return 0;
}
HULIHONG 2012-07-25
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 的回复:]

一直是 0. 因为
1. 程序里面没有改变过 count 的值.
2. if(!buf); 后面多了个分号, 所以一开始就跳出循环了.
[/Quote]

1.printf("current allocated %d MB", ++count);
2.分号是没有的
IVERS0N 2012-07-25
  • 打赏
  • 举报
回复
没影响
www_adintr_com 2012-07-25
  • 打赏
  • 举报
回复
一直是 0. 因为
1. 程序里面没有改变过 count 的值.
2. if(!buf); 后面多了个分号, 所以一开始就跳出循环了.

70,024

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧