473,406 Members | 2,387 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

can the array size be ZERO

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

Apr 4 '06 #1
8 2250
pr**************@gmail.com wrote:
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.
Apr 4 '06 #2
pr**************@gmail.com wrote:
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.
--
Flash Gordon, living in interesting times.
Web site - http://home.flash-gordon.me.uk/
comp.lang.c posting guidelines and intro:
http://clc-wiki.net/wiki/Intro_to_clc
Apr 4 '06 #3

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

Apr 5 '06 #4
pr**************@gmail.com wrote:
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.


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.
Apr 5 '06 #5

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...

Apr 5 '06 #6
Simon Biber wrote:
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?

Apr 5 '06 #7
"bill" <bi**********@gmail.com> writes:
Simon Biber wrote:
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).

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Apr 5 '06 #8
pr**************@gmail.com wrote:
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.

--
Peter

Apr 5 '06 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: James | last post by:
Hi, I'm hoping someone can help me out. If I declare a class, eg. class CSomeclass { public: var/func etc..... private varfunc etc..
27
by: Ian Tuomi | last post by:
Say I have an array: int foo and it has an unknown number of integers in it. How can I find out how many? I tried: #include <stdio.h> int ArraySize(int array) { int i = 0; while(array !=...
25
by: prashna | last post by:
Hi all, I have seen a piece of code(while doing code review) which declared an array of size 0.One of my friend told although it is not standard C,some compilers will support this..I am very...
20
by: K.M. Jr. | last post by:
Hi all, Consider this line - char s = "\0"; Does this initialize all array elements to zero ? Thanks.
4
by: emma middlebrook | last post by:
Hi Straight to the point - I don't understand why System.Array derives from IList (given the methods/properties actually on IList). When designing an interface you specify a contract. Deriving...
16
by: Chad | last post by:
How do I set the value of a = 3 in the following lines of code? #include <stdio.h> struct letter{ char a; }; struct add { struct letter addit;
19
by: nileshsimaria | last post by:
Hi, I have seen some code were we have array with zero elements (mostly within structure) like, typedef struct foo { int data }foo;
19
by: arnuld | last post by:
/* C++ Primer - 4/e * chapter 4- Arrays & Pointers, exercise 4.28 * STATEMENT * write a programme to read the standard input and build a vector of integers from values that are read....
8
by: Ray D. | last post by:
I'm trying to write a C function to compute the compressed row storage (CRS) vectors of a 2-d matrix. This is used primarily for increasing computing efficiency of matrices whose main elements are...
5
by: =?Utf-8?B?QXlrdXQgRXJnaW4=?= | last post by:
Hi Willy, Thank you very much for your work. C++ code doesnot make any serialization. So at runtime C# code gives an serialization error at "msg_file_s sa = (msg_file_s) bf.Deserialize(ms);"...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.