4

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);
Community
  • 1
  • 1
pic11
  • 14,267
  • 21
  • 83
  • 119
  • On a linux box typing man malloc provides useful information (and confirms Jim's answer) – Tom Mar 16 '11 at 17:43

2 Answers2

11

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?

Community
  • 1
  • 1
Jim Balter
  • 16,163
  • 3
  • 43
  • 66
  • 1
    Considering that the question is tagged C and C++, it would be worth making explicit which "Standard" you are talking about. Also, since any call to malloc may return 0, it seems no particular provision was made for malloc(0) at all. – Pascal Cuoq Mar 16 '11 at 17:27
  • @Pascal however it may seem to you, the C standard does make particular provision for malloc(0). Notably, malloc(0) returning 0 does not mean that there is no more memory. – Jim Balter Mar 16 '11 at 17:36
1

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

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

Edwin Buck
  • 69,361
  • 7
  • 100
  • 138