3

我想知道是否有人已经看到 sbrk(0) 失败?

我的意思是,如果你能达到这个功能,你显然之前有权访问内存,所以检查当前的中断位置应该没问题,对吧?

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

4

1 回答 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; 
  ...

它不会失败,因为它只返回__curbrkif的当前值increment为零。

于 2014-02-04T18:00:00.343 回答