public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* RE: C and zero-sized arrays - what is the standard?
@ 2003-09-18 12:23 Bansidhar Arvind Deshpande  - CTD, Chennai.
  2003-09-18 13:24 ` Alexandre Courbot
  0 siblings, 1 reply; 6+ messages in thread
From: Bansidhar Arvind Deshpande  - CTD, Chennai. @ 2003-09-18 12:23 UTC (permalink / raw)
  To: gcc-help; +Cc: Alexandre Courbot

Hi alex,

This is for my knowlege in which kind 
of situations you make use of these 
type of structure definitions.



Bansidhar A. Deshpande
Telecom Group, 
HCL Technologies - CTD. 
Chennai.
Mobile : 9840109000
Phone : 044-23728366 Extn : 1324






-----Original Message-----
From: Alexandre Courbot [mailto:Alexandre.Courbot@lifl.fr]
Sent: Thursday, September 18, 2003 5:40 PM
To: gcc-help@gcc.gnu.org
Subject: Re: C and zero-sized arrays - what is the standard?


> Is there a portable way to do this? What is the right form according to
the
> standards?

Forgot to mention I am using gcc 3.3 - as far as I know the [0] form worked 
fine with 2.95.

Alex.

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: C and zero-sized arrays - what is the standard?
  2003-09-18 12:23 C and zero-sized arrays - what is the standard? Bansidhar Arvind Deshpande  - CTD, Chennai.
@ 2003-09-18 13:24 ` Alexandre Courbot
  0 siblings, 0 replies; 6+ messages in thread
From: Alexandre Courbot @ 2003-09-18 13:24 UTC (permalink / raw)
  To: Bansidhar Arvind Deshpande  - CTD, Chennai., gcc-help

> This is for my knowlege in which kind
> of situations you make use of these
> type of structure definitions.

We are using this to store the virtual methods table that belongs to classes 
meta-data in an object-oriented operating system. Every class is defined by 
the same structure (CardClass), but they each class have different methods 
and therefore the number of methods is not fixed (but known statically).

Using a pointer that we would allocate is not an option. First, because we are 
in the kernel-space of the operating system, and second, because this 
operating system is designed for very small and limited embedded systems 
(typically smartcards). By declaring a variable-length table of virtual 
methods we are sure that the vtable will be located right with the rest of 
the structure (and not allocated god-knows-where). This makes it possible to 
write smaller, more efficient code and saves us a pointer to the vtable. And 
yes, 4 bytes are worth saving on a smartcard. ;)

Alex.

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: C and zero-sized arrays - what is the standard?
  2003-09-18 11:48 Alexandre Courbot
  2003-09-18 12:06 ` Alexandre Courbot
  2003-09-18 13:18 ` Eljay Love-Jensen
@ 2003-09-18 13:27 ` Nathan Sidwell
  2 siblings, 0 replies; 6+ messages in thread
From: Nathan Sidwell @ 2003-09-18 13:27 UTC (permalink / raw)
  To: Alexandre Courbot; +Cc: gcc-help

Alexandre Courbot wrote:
> Hi everybody,
> 
> Consider the following code:
> 
> struct MyStruct
> {
>     int a;
>     int b[];
> };

> Is there a portable way to do this? What is the right form according to the 
> standards?
C99 blesses '[]', calling it a flexible array member [6.7.2.1]/16

nathan

-- 
Nathan Sidwell    ::   http://www.codesourcery.com   ::     CodeSourcery LLC
          The voices in my head said this was stupid too
nathan@codesourcery.com    ::     http://www.planetfall.pwp.blueyonder.co.uk


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: C and zero-sized arrays - what is the standard?
  2003-09-18 11:48 Alexandre Courbot
  2003-09-18 12:06 ` Alexandre Courbot
@ 2003-09-18 13:18 ` Eljay Love-Jensen
  2003-09-18 13:27 ` Nathan Sidwell
  2 siblings, 0 replies; 6+ messages in thread
From: Eljay Love-Jensen @ 2003-09-18 13:18 UTC (permalink / raw)
  To: Alexandre Courbot, gcc-help

Hi Alex,

>Is there a portable way to do this? What is the right form according to the standards?

I'm not exactly sure what the standard (ISO 9899) says about zero sized arrays at the end of a struct, or non-specified sized arrays at the end of a struct.  I'm willing to wager a guess that they are invalid.

Usually this technique is used for "stretchy buffers".

There are several approaches to the stretchy buffer problem:
+ Use an unspecified array size at the end of the struct. (non-ISO 9899)
+ Use an array size of ZERO at the end of the struct. (non-ISO 9899)
+ Use an array size of ONE at the end of the struct.
+ Use an array size of the LARGEST possible buffer size at the end of the struct.

I'm in the last camp.  For safety reasons, it is best to err on the size of "buffers specified too big" rather than "buffers specified too small" (causing overruns, terror, raining cats-n-dogs, plagues of locusts).  However, note, all of these techniques are ISO 9899 suspect.

#define MAX_BUF 1024
struct MyBuf
{
  int length;
  int array[MAX_BUF];
};
extern int calc_buf_size(int n_elements);

When allocating a stretchy buffer, using the last convention, you'd do something like:
MyBuf* buf = malloc(calc_buf_size(3));

And your "calc_buf_size" routine does the math, ala:
int calc_buf_size(int n)
{
  return sizeof(MyBuf) - ((MAX_BUF - n) * sizeof(int));
}

I like to isolate the buf size calculation, so that it's easier to modify in one place than if the calculation were strewn throughout the code.

When allocating a .data section (i.e., a global) or a stack based MyBuf, it will consume the maximum buf size.  For embedded systems, that may be unacceptable.  However, having the data array that over-runs the bounds of the array is suspect and I would expect its behavior to be platform (OS + compiler) dependent.

(Hmmm, I don't think I really answered your question.  I more-or-less danced around it.  Sorry.  Let me try again.)

>Is there a portable way to do this?

Yes, but it'll be very ugly, a lot more work (for little-to-no gain), is at variance to how stretchy buffers are normally done by embedded systems programmers, and a maintenance nightmare.

So I won't bother explaining too much.  Let me just say:  it involves having explicitly sized structures for every size that you are interested in.

In the pre-Darwin days of Mac OS, they had str15, str63, str255 (and others) for Pascal style strings, for example.

>What is the right form according to the standards?

There is no "right form".  Using stretchy buffers is just something not supported by the language proper, and every time they're done it's ad hoc and slightly-to-greatly suspect.

I recommend the "largest specified buffer or smaller" technique.  Others swear by the "buffer-size-one plus" technique.  Either way, portability is a concern -- and can be extremely frustrating.

--Eljay

PS:  I'm not familiar with the new "dynamic" array facility of C99.  So some of my advice may have been superceded by new language facilities.


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: C and zero-sized arrays - what is the standard?
  2003-09-18 11:48 Alexandre Courbot
@ 2003-09-18 12:06 ` Alexandre Courbot
  2003-09-18 13:18 ` Eljay Love-Jensen
  2003-09-18 13:27 ` Nathan Sidwell
  2 siblings, 0 replies; 6+ messages in thread
From: Alexandre Courbot @ 2003-09-18 12:06 UTC (permalink / raw)
  To: gcc-help

> Is there a portable way to do this? What is the right form according to the
> standards?

Forgot to mention I am using gcc 3.3 - as far as I know the [0] form worked 
fine with 2.95.

Alex.


^ permalink raw reply	[flat|nested] 6+ messages in thread

* C and zero-sized arrays - what is the standard?
@ 2003-09-18 11:48 Alexandre Courbot
  2003-09-18 12:06 ` Alexandre Courbot
                   ` (2 more replies)
  0 siblings, 3 replies; 6+ messages in thread
From: Alexandre Courbot @ 2003-09-18 11:48 UTC (permalink / raw)
  To: gcc-help

Hi everybody,

Consider the following code:

struct MyStruct
{
    int a;
    int b[];
};

struct MyStruct i = { 10, {1, 2, 3, 4} };
struct MyStruct j = { 20, {10} };

int main(int argc, char * argv[])
{
    return 0;
}

This kind of code is very common in embedded systems programming. The above 
code works fine with GCC, but some other compilers require MyStruct to be 
declared as 

struct MyStruct
{
    int a;
    int b[0];
};

Is there a portable way to do this? What is the right form according to the 
standards?

Thanks,
Alex.

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2003-09-18 13:27 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2003-09-18 12:23 C and zero-sized arrays - what is the standard? Bansidhar Arvind Deshpande  - CTD, Chennai.
2003-09-18 13:24 ` Alexandre Courbot
  -- strict thread matches above, loose matches on Subject: below --
2003-09-18 11:48 Alexandre Courbot
2003-09-18 12:06 ` Alexandre Courbot
2003-09-18 13:18 ` Eljay Love-Jensen
2003-09-18 13:27 ` Nathan Sidwell

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).