我想知道是否有人已经看到 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/