473,614 Members | 2,089 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Array of size zero!

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 curious to know the use
of it..
The code was compiled using Diab C compiler.
Also the array was declared in structure like this

typedef struct someStruct
{
int i;
char array[0];
} someStruct;
Thanks
Regards
Nov 14 '05 #1
25 2684
The standard way of writing this is:
typedef struct someStruct
{
int i;
char array[];
} someStruct;

Without the zero.
This is a flexible array and is supported by C99.

you allocate the structure with more than sizeof(int), to
allocate the size of the array
Nov 14 '05 #2
"prashna" <va******@redif fmail.com> wrote in message
news:d4******** *************** ***@posting.goo gle.com...
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 curious to know the use
of it..
The code was compiled using Diab C compiler.
Also the array was declared in structure like this

typedef struct someStruct
{
int i;
char array[0];
} someStruct;
Thanks
Regards


I think the C99 variant of this is "char array[];". The
zero-length array thing is intended to get around the
problem of allocating a variable-length structure, where
the declared portion of the structure represents a fixed-length
"header" and the size of the variable-length portion is
calculated at run-time.

Some compilers have a non-standard extension that allows
an array length of zero specified, which is the same effect
as the C99 elided length specification.

For compilers that don't support either specification, the
infamous "struct hack" technique is used where the array
length is specified as 1, and size calculations for the
variable-length structure take into consideration the
presence of one element of the array in the structure length
returned by sizeof().
Nov 14 '05 #3
jacob navia wrote:
The standard way of writing this is:
typedef struct someStruct
{
int i;
char array[];
} someStruct;

Without the zero.
This is a flexible array and is supported by C99.

you allocate the structure with more than sizeof(int), to
allocate the size of the array


I geuss this is not correct because it assumes that
array[] starts right after i. As the FAQ tells us,
you should use the offsetof() macro, which can
calculate the byte offset of array in someStruct.
This macro should be defined in <stddef.h>. If not,
use the #definition in the FAQ.

Case

Nov 14 '05 #4
prashna a écrit :
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 curious to know the use
of it..
The code was compiled using Diab C compiler.
Also the array was declared in structure like this

typedef struct someStruct
{
int i;
char array[0];
} someStruct;


It was a pre-C99 standard trial to have a flexible member array.
The struct could be allocated this way :
someStruct *s = malloc(sizeof (someStruct) + 42);
So, with a single malloc, you allocated the struct and the array objects
simultaneously.
In C99 a flexible array member should be declared :
struct someStruct {
int i;
char array[];
};

--
Richard
Nov 14 '05 #5
In <40************ **********@drea der2.news.tisca li.nl> Case - <no@no.no> writes:
jacob navia wrote:
The standard way of writing this is:
typedef struct someStruct
{
int i;
char array[];
} someStruct;

Without the zero.
This is a flexible array and is supported by C99.

you allocate the structure with more than sizeof(int), to
allocate the size of the array


I geuss this is not correct because it assumes that
array[] starts right after i.


Nonsense: no such assumption is needed at all.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #6
"Dan Pop" <Da*****@cern.c h> wrote in message
news:ca******** **@sunnews.cern .ch...
In <40************ **********@drea der2.news.tisca li.nl> Case - <no@no.no> writes:
jacob navia wrote:
The standard way of writing this is:
typedef struct someStruct
{
int i;
char array[];
} someStruct;

Without the zero.
This is a flexible array and is supported by C99.

you allocate the structure with more than sizeof(int), to
allocate the size of the array


I geuss this is not correct because it assumes that
array[] starts right after i.


Nonsense: no such assumption is needed at all.


Right, but Jacob /is/ making that assumption. AIUI, this does not
necessarily allocate sufficient space for 10 elements in array:

struct someStruct *s = malloc(sizeof(i nt) + 10);

But, OTOH, this does:

struct someStruct *s = malloc(sizeof *s + 10);

(Assuming malloc() succeeds in both cases.)

The reason being that padding after i in struct someStruct is not allowed
for.

Alex
Nov 14 '05 #7
In <2j************ @uni-berlin.de> "Alex Fraser" <me@privacy.net > writes:
"Dan Pop" <Da*****@cern.c h> wrote in message
news:ca******* ***@sunnews.cer n.ch...
In <40************ **********@drea der2.news.tisca li.nl> Case - <no@no.no>writes:
>jacob navia wrote:
>> The standard way of writing this is:
>> typedef struct someStruct
>> {
>> int i;
>> char array[];
>> } someStruct;
>>
>> Without the zero.
>> This is a flexible array and is supported by C99.
>>
>> you allocate the structure with more than sizeof(int), to
>> allocate the size of the array
>>
>
>I geuss this is not correct because it assumes that
>array[] starts right after i.


Nonsense: no such assumption is needed at all.


Right, but Jacob /is/ making that assumption.


Where?!? He says more than sizeof(int), which might be misleading, but
not technically wrong.
AIUI, this does not
necessarily allocate sufficient space for 10 elements in array:

struct someStruct *s = malloc(sizeof(i nt) + 10);
It should, except for deliberately perverse implementations , but, in
theory, you're correct. OTOH, Jacob didn't say anywhere that this is
the right way of allocating memory for the structure, did he?
But, OTOH, this does:

struct someStruct *s = malloc(sizeof *s + 10);

(Assuming malloc() succeeds in both cases.)

The reason being that padding after i in struct someStruct is not allowed
for. ^^^


The reason being that padding after i in struct someStruct is
theoretically allowed. But, since it would merely waste memory, don't
expect to find it, until someone revives the DS9K project.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #8
In 'comp.lang.c', va******@rediff mail.com (prashna) wrote:
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
Wrong. It's a C99 feature.
C,some compilers will support this..I am very curious to know the use
of it..
The code was compiled using Diab C compiler.
Also the array was declared in structure like this

typedef struct someStruct
{
int i;
char array[0];
} someStruct;


It's used to do some linear mapping. It's a sort of trick... Not very good C
(outbounds-based technique), but it works and can be useful.

--
-ed- get my email here: http://marreduspam.com/ad672570
The C-language FAQ: http://www.eskimo.com/~scs/C-faq/top.html
C-reference: http://www.dinkumware.com/manuals/reader.aspx?lib=c99
FAQ de f.c.l.c : http://www.isty-info.uvsq.fr/~rumeau/fclc/
Nov 14 '05 #9
"Dan Pop" <Da*****@cern.c h> wrote in message
news:ca******** **@sunnews.cern .ch...
In <2j************ @uni-berlin.de> "Alex Fraser" <me@privacy.net > writes:
"Dan Pop" <Da*****@cern.c h> wrote in message
news:ca******* ***@sunnews.cer n.ch...
In <40************ **********@drea der2.news.tisca li.nl> Case -
<no@no.no> writes:

>jacob navia wrote:
>> The standard way of writing this is:
>> typedef struct someStruct
>> {
>> int i;
>> char array[];
>> } someStruct; [snip] >> you allocate the structure with more than sizeof(int), to
>> allocate the size of the array
>
>I geuss this is not correct because it assumes that
>array[] starts right after i.

Nonsense: no such assumption is needed at all.
Right, but Jacob /is/ making that assumption.


Where?!? He says more than sizeof(int), which might be misleading, but
not technically wrong.


By fortunate vagueness, yes. That too is an assumption, but a reasonable
one, don't you think?
AIUI, this does not
necessarily allocate sufficient space for 10 elements in array:

struct someStruct *s = malloc(sizeof(i nt) + 10);


It should, except for deliberately perverse implementations , but, in
theory, you're correct. OTOH, Jacob didn't say anywhere that this is
the right way of allocating memory for the structure, did he?


See above.
But, OTOH, this does:

struct someStruct *s = malloc(sizeof *s + 10);

(Assuming malloc() succeeds in both cases.)

The reason being that padding after i in struct someStruct is not allowed
for. ^^^


(I wrote this as applying to the first sentence but it doesn't read that
way. Oops.)
The reason being that padding after i in struct someStruct is
theoretically allowed. But, since it would merely waste memory, don't
expect to find it, until someone revives the DS9K project.


Agreed, in this specific case it is extremely unlikely. But for some
combinations of types of i and/or array, it's almost "guaranteed " (eg struct
{int i, double array[]}).

Alex
Nov 14 '05 #10

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

Similar topics

2
3324
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
138983
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 != NULL) i++; return i;
20
2303
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
7282
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 from an interface and only implementing some of it means something is wrong: either the interface specification is wrong e.g. not minimal or the derivation is wrong e.g. the type can't actually honour this contract.
16
4701
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
7067
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
6060
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. allocate an array of the same size as the vector and copy elements from the vector into the array */
8
3361
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 zeros. So for example, if you were given the following matrix: A = { 0, 0, 1, 0; 0, 3, 0, 0; 6, 0, 4, 0} the function would compute three vectors - the non-zero element
5
3787
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);" I thought that it is very hard to memory map structure array. I need both read and write memory mapped file at both side of C# and C++.
0
8182
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8130
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8627
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8433
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7093
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6088
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5540
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
2568
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1747
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.