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;
|
|