sbrk(0)能失败吗?

时间:2014-02-04 17:46:40

标签: c glibc sbrk

我想知道是否有人见过sbrk(0)失败了?

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

编辑:我应该考虑一个错误例外吗?

1 个答案:

答案 0 :(得分:3)

documents表示:

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

如果__curbrk为零,它就不会失败,因为它只返回increment的当前值。