can the array size be ZERO

  • Thread starter pranav.choudhary
  • Start date
P

pranav.choudhary

Is it legal to keep the size of an array 0. gcc 3.4.2 did not give any
error for the declaration

int a[0];

If the declaration is legal, then what will be the implications of
saying

a[0] = 10;

Will it lead to memory corruption??

Thanx,
pranav
 
F

Flash Gordon

Is it legal to keep the size of an array 0. gcc 3.4.2 did not give any
error for the declaration

int a[0];

It isn't legal.
If the declaration is legal, then what will be the implications of
saying

a[0] = 10;

Will it lead to memory corruption??

If declaring a 0 length array way legal then trying to store something
in the first element would invoke undefined behaviour because there
would be no such element.
 
S

Skarmander

Is it legal to keep the size of an array 0. gcc 3.4.2 did not give any
error for the declaration

int a[0];
This is a gcc extension (though many other compilers support it too), so the
answer in general is "no, this is not legal". You have to supply -pedantic
before gcc will even think of complaining, however.
If the declaration is legal, then what will be the implications of
saying

a[0] = 10;

Will it lead to memory corruption??
Merely declaring an array of size 0 is undefined behavior, so the assignment
wouldn't matter.

If C allowed 0-sized arrays, though, then a[0] would be out of bounds, and
the assignment produces undefined behavior. In short, anything may happen,
including nothing, a compiler error, an OS trap, stack corruption or,
indeed, demons flying out of your nose.

However, the most common application of zero-sized arrays is to provide an
alias for a pointer to an object of unspecified length, allocated by some
unspecified means. This object may happen to be of the appropriate type, in
which case the assignment will do what is expected.

S.
 
P

pranav.choudhary

However, the most common application of zero-sized arrays is to provide an
alias for a pointer to an object of unspecified length, allocated by some
unspecified means. This object may happen to be of the appropriate type, in
which case the assignment will do what is expected.

S.

I did not really get the above few lines. Can you please give an
example or a link or two for reference.

Thanx
 
S

Simon Biber

I did not really get the above few lines. Can you please give an
example or a link or two for reference.

The reason many compilers supported "zero-sized arrays" is to allow this:

struct foo
{
/* other members here */
/* ... */
int a[0]; /* nonstandard but supported by most compilers */
};

void do_something(size_t n)
{
struct foo *bar = malloc(n * sizeof(int));
if(bar)
{
/* use bar.a[0 .. n-1] */

free(bar);
}
}

Now that in C99 we have a standard-approved way of achieving the same
thing, it is no longer necessary to declare zero-sized arrays.

Simon.
 
P

pranav.choudhary

The reason many compilers supported "zero-sized arrays" is to allow this:

struct foo
{
/* other members here */
/* ... */
int a[0]; /* nonstandard but supported by most compilers */
};

void do_something(size_t n)
{
struct foo *bar = malloc(n * sizeof(int));
if(bar)
{
/* use bar.a[0 .. n-1] */

free(bar);
}
}

Now that in C99 we have a standard-approved way of achieving the same
thing, it is no longer necessary to declare zero-sized arrays.

Simon.

Thanks for the input...
 
B

bill

Simon said:
The reason many compilers supported "zero-sized arrays" is to allow this:

struct foo
{
/* other members here */
/* ... */
int a[0]; /* nonstandard but supported by most compilers */
};

void do_something(size_t n)
{
struct foo *bar = malloc(n * sizeof(int));
if(bar)
{
/* use bar.a[0 .. n-1] */

free(bar);
}
}

Now that in C99 we have a standard-approved way of achieving the same
thing, it is no longer necessary to declare zero-sized arrays.

What is the standard-approved way of accomplishing that?
 
K

Keith Thompson

bill said:
Simon said:
The reason many compilers supported "zero-sized arrays" is to allow this:

struct foo
{
/* other members here */
/* ... */
int a[0]; /* nonstandard but supported by most compilers */
}; [snip]
Now that in C99 we have a standard-approved way of achieving the same
thing, it is no longer necessary to declare zero-sized arrays.

What is the standard-approved way of accomplishing that?

"Flexible array members", defined in section 6.7.2.1 of the C99
standard (google "n1124.pdf" for a copy).
 
P

Peter Nilsson

Is it legal to keep the size of an array 0.

That would require a conforming implementation to issue a diagnostic
for the
constraint violation. [In other words, no.]
gcc 3.4.2 did not give any error for the declaration

Then you did not invoke 'gcc 3.4.2' as a conforming implementation. See
the
companion online help for details of how you can invoke your compiler
in
conforming mode.
int a[0];

If the declaration is legal, then what will be the implications of
saying

a[0] = 10;

If it is supported by your compiler, then you'll have to consult your
compiler
documentation on what it means.
Will it lead to memory corruption??

Undefined behaviour can potentially do anything.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,176
Messages
2,570,950
Members
47,503
Latest member
supremedee

Latest Threads

Top