由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - 奇怪的问题:关于一个简单的malloc()小程序 (转载)
相关主题
菜鸟请教C问题A aimple C++ question
问个fork cow的问题What is wrong with the code?
一个nested class的问题C++ 初级再初级问题
计算组合数C(m,n)为什么foo1可以而foo2不行?
请教一道题 (转载)菜鸟求教,一个c++的困惑
一个古怪的C程序运行错误。大侠们救命, C++ operator new 问题
这个C++程序为什么不能运行C的argc问题
char ** pt1和 char * pt2[] 的区别在哪?突然发现现在很反感malloc了
相关话题的讨论汇总
话题: brk话题: malloc话题: byte话题: 程序话题: num
进入Programming版参与讨论
1 (共1页)
f******e
发帖数: 164
1
【 以下文字转载自 Linux 讨论区 】
发信人: francise (小飞猫), 信区: Linux
标 题: 奇怪的问题:关于一个简单的malloc()小程序
发信站: BBS 未名空间站 (Sun Mar 30 17:43:48 2008)
我写了一段小程序:
#include
#include
int main(int argc, char *argv[]){
char *buffer;
int num_byte;
num_byte = atoi(argv[1])*1024;
while(1)
{buffer=malloc(num_byte);
free(buffer);
}
}
然后我用
strace ./code 126
结果是:
....
brk(0) = 0x804965c
brk(0x8068e74) = 0x8068e74
brk(0x8069000) = 0x8069000
(Nothing further)
为什么只有有限的几个brk()?
问题是,我的程序明明是无限循环啊
t****t
发帖数: 6806
2
malloc == brk?????

【在 f******e 的大作中提到】
: 【 以下文字转载自 Linux 讨论区 】
: 发信人: francise (小飞猫), 信区: Linux
: 标 题: 奇怪的问题:关于一个简单的malloc()小程序
: 发信站: BBS 未名空间站 (Sun Mar 30 17:43:48 2008)
: 我写了一段小程序:
: #include
: #include
: int main(int argc, char *argv[]){
: char *buffer;
: int num_byte;

m*****e
发帖数: 4193
3
First, only the first malloc *could* call brk() since you free it
immediately.
Second, if your existing brk is big enough even the first would not call brk
().
Third, if you malloc() big enough memory, it may does a mmap() instead of
allocating from heap.

【在 f******e 的大作中提到】
: 【 以下文字转载自 Linux 讨论区 】
: 发信人: francise (小飞猫), 信区: Linux
: 标 题: 奇怪的问题:关于一个简单的malloc()小程序
: 发信站: BBS 未名空间站 (Sun Mar 30 17:43:48 2008)
: 我写了一段小程序:
: #include
: #include
: int main(int argc, char *argv[]){
: char *buffer;
: int num_byte;

1 (共1页)
进入Programming版参与讨论
相关主题
突然发现现在很反感malloc了请教一道题 (转载)
const 指针类型转换一个古怪的C程序运行错误。
[C++ boost::interprocess] 讨论贴这个C++程序为什么不能运行
关于C C++ 和java的文件读写问题 char ** pt1和 char * pt2[] 的区别在哪?
菜鸟请教C问题A aimple C++ question
问个fork cow的问题What is wrong with the code?
一个nested class的问题C++ 初级再初级问题
计算组合数C(m,n)为什么foo1可以而foo2不行?
相关话题的讨论汇总
话题: brk话题: malloc话题: byte话题: 程序话题: num