Before people go why would i want to create an array of size 0 - this is due to writing a test program.
at the moment i have two diffeent ways of doing - not sure which one is even slightly correct
1.

uint8_t  * BufferB = new uint8_t[0];
        uint8_t* p;      
        p = BufferB;
....
        delete BufferB;

2.

uint8_t bufferB[0]= {'1'};

i have to use uint8_t and also an array insted of a vector

Recommended Answers

All 14 Replies

Before people go why would i want to create an array of size 0

well im not seaoned programmer but how can we create an array of size ZERO? It should contain atleast one element but you asking to create an array with no elements (if im not mistaken).

yeah i am its a boundary test for a program. I Have tried looking online but everyone seems to have conflicting views whether you are able to do this or not

i was going about my program in complety the wrong way - although i still want to know if you can actually set an array to size 0?

google is you friend, learn to use it. Click here

commented: Haha, Very NICE! +1

google is you friend, learn to use it. Click here

i know google is my freind - i did use it before posting here went through 10 pages of results hence why i said there seems to be conflicting views hence why asking the question

ps love the link - really clever

Well, even it is possible, what is the use of it? will it be anything useful?

why does it have to be an array? Having a size 0 array is really pointless since it'll always be size 0 since arrays are statically initiated. If you make a vector, then you can change the size dynamically and add objects later when you need.

vector <objType> arr;

This is the key point. What is the use of the array which has no elements? I dont know if we can do this in C# (declaring an array with size 0). I hope NO. This means this should be considered as bug in language (as im not seasoned programmer and still learning so im brave to say this).

You can't have an array of size 0, in the sense that every array type includes the number of elements as part of the type, and for the type to be well defined, the number of elements must be strictly positive. Part of the reason for this requirement is to be able to guarantee that sizeof(x) is nonzero regardless of the type of x.

So your second example is not well-formed, although I imagine there might be a compiler out there that accepts it.

Your first example, on the other hand, is fine. There is nothing wrong with using "new" to create a pointer to "the first element" of a 0-element dynamically allocated array. Of course, that first element does not exist, so the pointer is an off-the-end pointer for that array. It's a valid pointer; it's guaranteed to be distinct from other pointers; and you should delete it at some point if you wish to avoid memory leaks. What you can't do is dereference it, because it doesn't point to an element. That's true of any off-the-end pointer, not just this one.

It is possible to declare array[0] in c#. But in c# it's a class and it has a method called resize() which gets called automatically when the number of element exceeds the available space in array. Same with java.

commented: Isn't this a C++ question? -3

Well, even it is possible,

thats the question i was asking . The use of it as i said was to test the boundry of a program

and to whom suggested using a vector i wish i could use it but cannot

It is possible to declare array[0] in c#. But in c# it's a class and it has a method called resize() which gets called automatically when the number of element exceeds the available space in array. Same with java.

i don't know c# the program i have written is in c++
i knew you could do it with java but am coming from a java background to c++

This is what i ended up doing:
create an array of size 1 then when calling it in a function which takes in a size set this to zero.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.