我想知道是否有人曾经遇到过sbrk(0)失败的情况?
我的意思是,如果您能够访问此函数,那么您显然已经拥有了访问内存的权限,因此检查当前断点位置应该是可以的,对吧?
编辑:例如,我是否应该考虑错误异常?
文件指出:
sbrk() increments the program's data space by increment bytes.
Calling sbrk() with an increment of 0 can be used to find the current
location of the program break.
...
On success, sbrk() returns the previous program break. (If the break
was increased, then this value is a pointer to the start of the newly
allocated memory). On error, (void *) -1 is returned, and errno is
set to ENOMEM.
如果你看一下glibc的实现,你会发现:
extern void *__curbrk;
...
void *
__sbrk (intptr_t increment)
{
...
if (increment == 0)
return __curbrk;
...
如果increment为零,它只返回__curbrk
的当前值,因此它不可能失败。
sbrk(2)
文档中写道:程序断点的当前值可以通过sbrk(0)
可靠地返回。 - Jonathan Leffler