1

// new T[0] allocate a zero sized array can have values?

auto pv=new int[0];
cout<<pv<<endl; //0x... ?
*pv=123;
cout<<*pv<<endl; //123 ?
delete[] pv;

Why? if so, what's difference between new T[0] and new T[1]

Why can I set the value of 0 sized array ...?

Larry
  • 155
  • 7
  • You are using pointer arithmetic to scribble over adjacent memory – Aluan Haddad May 13 '20 at 12:19
  • 4
    Does this answer your question? [C++ new int\[0\] -- will it allocate memory?](https://stackoverflow.com/questions/1087042/c-new-int0-will-it-allocate-memory) – RoQuOTriX May 13 '20 at 12:20
  • 1
    @RoQuOTriX Not really; whether it allocates memory or not, `pv[0]` is logically out of bounds. – Asteroids With Wings May 13 '20 at 12:23
  • Does it actually allocate the memory? – Larry May 13 '20 at 12:27
  • @AsteroidsWithWings I think we can't discuss about UB. As after `auto pv=new int[0];` everything could happen, I wouldn't argue that something could be logically out of bounds – RoQuOTriX May 13 '20 at 12:27
  • @Larry It allocates some memory. It does not allocate memory that is available for use by you as an `int` element of the array. – Asteroids With Wings May 13 '20 at 12:27
  • @RoQuOTriX `new int[0]` does not have undefined behaviour. So the out-of-bounds expression (via the dereference) is the _only_ relevant thing. – Asteroids With Wings May 13 '20 at 12:28
  • @RoQuOTriX Allocating the memory is not UB by itself. – Acorn May 13 '20 at 12:28
  • @AsteroidsWithWings oh yes you are right. But the dereferencing is UB – RoQuOTriX May 13 '20 at 12:30
  • 1
    @Larry To maintain the compiler's heap data structure, it may allocate memory, but that is only for the structure's internals. – PaulMcKenzie May 13 '20 at 12:30
  • @RoQuOTriX Yes, because it is an operation that attempts to navigate logically out of bounds of the array. – Asteroids With Wings May 13 '20 at 12:30
  • @PaulMcKenzie It's also because every [concurrently extant] object in C++ must have a unique address. – Asteroids With Wings May 13 '20 at 12:31
  • So, the system does allocate some ghost memory, but I can't use it, and I must deallocate this ghost memory? delete [] pv; ? Is it right – Larry May 13 '20 at 12:31
  • @Larry -- Imagine if you created a home-made `vector` class, where on construction, you took in a `size_t size` parameter to denote the number of entries. The user passes in a `0` for `size`. It is perfectly valid in your class to do something like `new T[size]`, where `size==0`. – PaulMcKenzie May 13 '20 at 12:34
  • It depends on memory allocator in you libc/libstd C++. More likely yes then no, since memory allocators usually allocating minimally `sizeof(size_t)` bytes. I.e. for 64bit instruction set, for range `0..8` memory allocator will reserve 8 bytes. In any case no any guaranty according to standard this is `undefined behavior`. – Victor Gubin May 14 '20 at 07:32

1 Answers1

7

It is legal to create a new int[0] (though at first glance it may not appear to be useful!).

However, your use of it is just like any other buffer overrun: your program has undefined behaviour.

C++ does not check array bounds for you. That's your job.

Going past them can appear to work; it can cause a crash; it can instantaneously transport the sun to another part of the galaxy.

Just don't do it.

Asteroids With Wings
  • 17,071
  • 2
  • 21
  • 35