Reducing the size of objects with realloc()

  • Thread starter Spiros Bousbouras
  • Start date
S

Spiros Bousbouras

If you do realloc(p , a) where a is smaller than the size
of the object p points to is there any realistic possibility
that realloc will return 0 ? Is it worth checking for ?
 
H

Harald van Dijk

If you do realloc(p , a) where a is smaller than the size of the object
p points to is there any realistic possibility that realloc will return
0 ? Is it worth checking for ?

Possibly, and yes. If blocks of smaller sizes are allocated in different
regions of memory, and you have lots of small blocks already, then
realloc can fail when shrinking objects. It's a small effort to check for
0 and use the old value, if that's good enough for you.
 
Q

qarnos

If you do realloc(p , a) where a is smaller than the size
of the object p points to is there any realistic possibility
that realloc will return 0 ? Is it worth checking for ?

Is it really worth *not* checking for? What if realloc() decides to
move your object to optimize memory usage?
 
Q

qarnos

Is it really worth *not* checking for? What if realloc() decides to
move your object to optimize memory usage?

Scratch that - I misread the question. But I'd still check for NULL.
 
C

CBFalconer

Spiros said:
If you do realloc(p , a) where a is smaller than the size of
the object p points to is there any realistic possibility that
realloc will return 0? Is it worth checking for?

Yes, it is possible. The system builder was foolish, but still met
the C standard.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top