Can sbrk (0) fail?

I would like to know if anyone who has already seen sbrk (0) has worked?

I mean, if you can achieve this function, you obviously had access rights to the memory earlier, so check that the current location of the fault should be in order, right?

EDIT: Should I consider throwing an exception, for example?

+3
source share
1 answer

The documents indicate that:

   sbrk() increments the program 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.

If you look at glibc , you will see:

extern void *__curbrk;
  ...
void *
__sbrk (intptr_t increment)
{
  ...
  if (increment == 0)
    return __curbrk; 
  ...

it will not work as it just returns the current value __curbrkif it incrementis zero.

+3
source

All Articles