sbrk(0) 可能失败吗?

3

我想知道是否有人曾经遇到过sbrk(0)失败的情况?

我的意思是,如果您能够访问此函数,那么您显然已经拥有了访问内存的权限,因此检查当前断点位置应该是可以的,对吧?

编辑:例如,我是否应该考虑错误异常?


1
你是说它对你失败了吗? - Robert Harvey
我使用的方式至少从未引导我遇到错误。 - user3210859
2
有趣的是,根据文档的宽泛措辞,它实际上可能会失败。当然,这完全是无稽之谈。 - Damon
Mac OS X 的 sbrk(2) 文档中写道:程序断点的当前值可以通过 sbrk(0) 可靠地返回。 - Jonathan Leffler
1个回答

3

文件指出:

   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的当前值,因此它不可能失败。


夜,非常感谢,终于搞定了。 也看了一下 http://fossies.org/dox/glibc-2.18/sysdeps_2mach_2hurd_2brk_8c_source.html :) - user3210859

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接