Array of size ZERO
-
hello guys...today i was reading somewhere on internet that we can have array of size 0, which actually means an array with no elements. What is the genuine use of it? Can we say that this is another bug which is not handled (just like bounds checking of arrays) in c/c++?
-
hello guys...today i was reading somewhere on internet that we can have array of size 0, which actually means an array with no elements. What is the genuine use of it? Can we say that this is another bug which is not handled (just like bounds checking of arrays) in c/c++?
Allowing an array size of zero is a good thing. The array still exists, even if it is empty. We can pass it, process it and otherwise use it without having to write special code. Often, database and table queries return zero rows. Since the rows returned can be viewed as an array of rows, the number of rows is the size of the array.
-
hello guys...today i was reading somewhere on internet that we can have array of size 0, which actually means an array with no elements. What is the genuine use of it? Can we say that this is another bug which is not handled (just like bounds checking of arrays) in c/c++?
yeah, so ? Well, you need an empty array to be able to fill it.
Watched code never compiles.
-
hello guys...today i was reading somewhere on internet that we can have array of size 0, which actually means an array with no elements. What is the genuine use of it? Can we say that this is another bug which is not handled (just like bounds checking of arrays) in c/c++?
In my old "C" days I used zero sized arrays as placeholders for dynamic sized structs. Something like this:
typedef struct _VARIABLE_SIZED_STRUCT { //Other members BYTE data[0]; } VARIABLE_SIZED_STRUCT, *PVARIABLE_SIZED_STRUCT; PVARIABLE_SIZED_STRUCT pDynamic = (PVARIABLE_SIZED_STRUCT)malloc(FIELD_OFFSET(VARIABLE_SIZED_STRUCT,data[iSize]));
The zero sized placeholder is antiquated and no longer needed with the C++ language. In fact I believe support for zero sized array was removed from recent MSVC compilers. Best Wishes, -David Delaune
-
hello guys...today i was reading somewhere on internet that we can have array of size 0, which actually means an array with no elements. What is the genuine use of it? Can we say that this is another bug which is not handled (just like bounds checking of arrays) in c/c++?
Visual Studio 2010 will flag an error for
int i[0];
You can however create it dynamically -int* i = new i[0];
«_Superman_» _I love work. It gives me something to do between weekends.
-
hello guys...today i was reading somewhere on internet that we can have array of size 0, which actually means an array with no elements. What is the genuine use of it? Can we say that this is another bug which is not handled (just like bounds checking of arrays) in c/c++?
As Superman already wrote, declaring an array with zero size is not allowed, at least on Microsoft Visual C++. Something interesting is that, on versions prior to Visual Studio 2010, which is the first one that implements
static_assert
, such functionality was realized using the _STATIC_ASSERT Macro[^]. That macro tests at compile time for the given condition and give the Compiler Error C2466[^] if not satisfied. Ops!C2466
means cannot allocate an array of constant size 0. By going deeper, we can search the macro on thecrtdbg.h
header:#define _STATIC_ASSERT(expr) typedef char __static_assert_t[ (expr) ]
Now, let's try to imagine how it works...
-
hello guys...today i was reading somewhere on internet that we can have array of size 0, which actually means an array with no elements. What is the genuine use of it? Can we say that this is another bug which is not handled (just like bounds checking of arrays) in c/c++?
You can in C99 (I think VC++ supports this syntax in structures if you use /Ze but it's not a full C99 flexible array thing) but you can't in C90 or C++. The sort of thing it's useful for in C99 is for arrays with variable sized structures. You can write:
struct foo
{
int number_of_elements_;
int elements_[];
};and then have the constructor sort out sizing the array. In C90 and C++ you'd have to have one element and manually size everything. Cheers, Ash
-
Allowing an array size of zero is a good thing. The array still exists, even if it is empty. We can pass it, process it and otherwise use it without having to write special code. Often, database and table queries return zero rows. Since the rows returned can be viewed as an array of rows, the number of rows is the size of the array.
Jalapeno Bob wrote:
Allowing an array size of zero is a good thing.
Here would be a possible initial form of the thing :) :
template <typename T>
class SimpleArray
{
T* m_pData;
int m_iLength;public:
SimpleArray() : m_pData(NULL), m_iLength(0) {};
...
};They sought it with thimbles, they sought it with care; They pursued it with forks and hope; They threatened its life with a railway-share; They charmed it with smiles and soap. :)