Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what happens if malloc (STL allocator, etc)requested to allocate 0 bytes [duplicate]

Tags:

c++

c

Possible Duplicate:
what does malloc(0) return ?

Does it return zero pointer? Is the behavior standardized?

How about STL allocator?

I googled it, but couldn't pinpoint the answer I was looking for.

EDIT: The linked question doesn't explain STL allocator.

I have another relevant question. What happens if one tries to deallocate zero pointer?

allocator.deallocate(0, 1);
like image 273
pic11 Avatar asked Dec 17 '22 16:12

pic11


2 Answers

malloc(0) may either return 0 or it may return a unique address (which shall not be accessed) -- the C89 and C99 Standards allow either behavior but do not mandate either one. (Bad design for a standard and I objected when I was on X3J11 but that's how it is.)

BTW, this is a duplicate question: what does malloc(0) return?

like image 146
Jim Balter Avatar answered Jan 22 '23 08:01

Jim Balter


Tested on Linux 2.6.34.7-66.fc13.x86_64, it returns a unique address.

I wouldn't try to dereference it though. :)

like image 29
Edwin Buck Avatar answered Jan 22 '23 10:01

Edwin Buck