sbrk(0) 会失败吗?

标签 c glibc sbrk

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

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

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

最佳答案

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

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

关于sbrk(0) 会失败吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21559658/

相关文章:

C: unsigned long int 没有按预期表现

c - 在 Visual Studio 2010 中使用 C99 之前的语法

c++ - 用于突出显示 BNF 语法指定的语法语言的 Lexer?

python - 在 Red Hat 6.6 版的 python2.7 中导入 Tensorflow 时出错。 'GLIBC_2.17 not found'

c++ - 强制 coredump on glib free 错误

c - malloc() 是如何在内部实现的?

c - malloc() 在哪里分配内存?是进程虚拟地址空间的数据段还是堆段?

c++ - 对 `yylex' 的 undefined reference

linux - 难道没有linux系统调用或者glibc函数来复制文件吗?

linux - sys_brk 的对齐要求是什么