What happens if malloc (STL allocator, etc.) is asked to allocate 0 bytes

Possible duplicate:
what returns malloc (0)?

Does it return a null pointer? Is the behavior standardized?

What about the STL dispenser?

I searched for it, but could not determine the answer I was looking for.

EDIT: The related question does not explain the STL allocator.

I have another important question. What happens if you try to free a null pointer?

allocator.deallocate(0, 1); 
+4
source share
2 answers

malloc(0) can either return 0 or return a unique address (which should not be accessed) - C89 and C99 standards allow any behavior, but do not require any. (Bad design for the standard, and I mind when I was on X3J11, but that's how.)

By the way, this is a duplicate question: what does malloc (0) return?

+11
source

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

I would not play it. :)

+1
source

Source: https://habr.com/ru/post/1343967/


All Articles