473,327 Members | 1,997 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,327 software developers and data experts.

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 2646
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******@rediffmail.com> wrote in message
news:d4**************************@posting.google.c om...
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**********************@dreader2.news.tiscali.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.ch> wrote in message
news:ca**********@sunnews.cern.ch...
In <40**********************@dreader2.news.tiscali.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(int) + 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.ch> wrote in message
news:ca**********@sunnews.cern.ch...
In <40**********************@dreader2.news.tiscali.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(int) + 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******@rediffmail.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.ch> wrote in message
news:ca**********@sunnews.cern.ch...
In <2j************@uni-berlin.de> "Alex Fraser" <me@privacy.net> writes:
"Dan Pop" <Da*****@cern.ch> wrote in message
news:ca**********@sunnews.cern.ch...
In <40**********************@dreader2.news.tiscali.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(int) + 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
Dan Pop wrote:
In <2j************@uni-berlin.de> "Alex Fraser" <me@privacy.net> writes:

"Dan Pop" <Da*****@cern.ch> wrote in message
news:ca**********@sunnews.cern.ch...
In <40**********************@dreader2.news.tiscali.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(int) + 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?


This is the theoretical case I was addressing. So, no nonsense
according to the C spec! And, come on, what else could Jacob
have meant?

Case
Nov 14 '05 #11
Emmanuel Delahaye <em**********@noos.fr> writes:
In 'comp.lang.c', va******@rediffmail.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.


No, C99 doesn't allow zero-sized arrays. There is a new feature
called "flexible array members" (discussed elsewhere in this thread),
but they're not implemented with zero-sized arrays.

--
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.
Nov 14 '05 #12
In <40**********************@dreader2.news.tiscali.nl > Case - <no@no.no> writes:
Dan Pop wrote:
In <2j************@uni-berlin.de> "Alex Fraser" <me@privacy.net> writes:

"Dan Pop" <Da*****@cern.ch> wrote in message
news:ca**********@sunnews.cern.ch...

In <40**********************@dreader2.news.tiscali.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(int) + 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?


This is the theoretical case I was addressing. So, no nonsense
according to the C spec! And, come on, what else could Jacob
have meant?


Ask him. The point is that offsetof is NEVER necessary in this case and
Jacob said "more than sizeof(int)", which is technically correct.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #13
Dan Pop wrote:
In <40**********************@dreader2.news.tiscali.nl > Case - <no@no.no> writes:

Dan Pop wrote:

In <2j************@uni-berlin.de> "Alex Fraser" <me@privacy.net> writes:

"Dan Pop" <Da*****@cern.ch> wrote in message
news:ca**********@sunnews.cern.ch...
>In <40**********************@dreader2.news.tiscali.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(int) + 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?


This is the theoretical case I was addressing. So, no nonsense
according to the C spec! And, come on, what else could Jacob
have meant?

Ask him. The point is that offsetof is NEVER necessary in this case and
Jacob said "more than sizeof(int)", which is technically correct.


With 'NEVER' do you mean with ALL real-life implementations, or do
you mean according to the C standard?

He said "more than sizeof(int), to allocate the size of the array".
IAW: 'Everything more than sizeof(int), is the size of the array'.
When more == 1, and there's a padding of >= 1 bytes between the int
and the array ... So, not technically correct.

Case

Nov 14 '05 #14
In <40**********************@dreader2.news.tiscali.nl > Case - <no@no.no> writes:
Dan Pop wrote:
In <40**********************@dreader2.news.tiscali.nl > Case - <no@no.no> writes:

Dan Pop wrote:
In <2j************@uni-berlin.de> "Alex Fraser" <me@privacy.net> writes:

>"Dan Pop" <Da*****@cern.ch> wrote in message
>news:ca**********@sunnews.cern.ch...
>
>
>>In <40**********************@dreader2.news.tiscali.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(int) + 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?

This is the theoretical case I was addressing. So, no nonsense
according to the C spec! And, come on, what else could Jacob
have meant?

Ask him. The point is that offsetof is NEVER necessary in this case and
Jacob said "more than sizeof(int)", which is technically correct.


With 'NEVER' do you mean with ALL real-life implementations, or do
you mean according to the C standard?


Both. All you need is sizeof(somestruct) to which you add the number of
bytes you need for array.
He said "more than sizeof(int), to allocate the size of the array".
IAW: 'Everything more than sizeof(int), is the size of the array'.
Severely broken interpretation. The right amount is simply unspecified
in the original statement. E.g. "you need more than your $10 to buy this
car" doesn't imply that you can buy it for $10.01.
When more == 1, and there's a padding of >= 1 bytes between the int
and the array ... So, not technically correct.


Nope.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #15
Dan Pop wrote:
In <40**********************@dreader2.news.tiscali.nl > Case - <no@no.no> writes:

Dan Pop wrote:

In <40**********************@dreader2.news.tiscali.nl > Case - <no@no.no> writes:

Dan Pop wrote:

>In <2j************@uni-berlin.de> "Alex Fraser" <me@privacy.net> writes:
>
>
>
>
>>"Dan Pop" <Da*****@cern.ch> wrote in message
>>news:ca**********@sunnews.cern.ch...
>>
>>
>>
>>>In <40**********************@dreader2.news.tiscali.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(int) + 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?

This is the theoretical case I was addressing. So, no nonsense
according to the C spec! And, come on, what else could Jacob
have meant?
Ask him. The point is that offsetof is NEVER necessary in this case and
Jacob said "more than sizeof(int)", which is technically correct.

I did. See below.

With 'NEVER' do you mean with ALL real-life implementations, or do
you mean according to the C standard?

Both. All you need is sizeof(somestruct) to which you add the number of
bytes you need for array.


Correct!

He said "more than sizeof(int), to allocate the size of the array".
IAW: 'Everything more than sizeof(int), is the size of the array'.

Severely broken interpretation. The right amount is simply unspecified
in the original statement. E.g. "you need more than your $10 to buy this
car" doesn't imply that you can buy it for $10.01.

When more == 1, and there's a padding of >= 1 bytes between the int
and the array ... So, not technically correct.

Nope.


I e-mailed Jacob and he replied that 'it would have been
better to say sizeof(struct) + 67, instead of sizeof(int). There
could be some obscure reasons for sizeof structure != sizeof int.'

So, originally he *did* mean to say that the 67 bytes would all
end up in the char array. In 99% of the cases this is correct
for this struct when using sizeof(int), he also wrote. Once again:
it was this C-spec 1% my initial reply was about.

Case
Nov 14 '05 #16
On Wed, 16 Jun 2004 15:15:00 +0100, Alex Fraser wrote:
"Dan Pop" <Da*****@cern.ch> wrote in message
news:ca**********@sunnews.cern.ch...
In <40**********************@dreader2.news.tiscali.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(int) + 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


Huh ? isn't the offset off empty arrays quaranteed to be the same as
sizeof(thestruct) in which it appears ?
struct someStruct *s = malloc(sizeof(someStruct) + 10);

Nov 14 '05 #17
"Nils O. Selåsdal" <NO*@Utel.no> wrote in message
news:pa***************************@Utel.no...
On Wed, 16 Jun 2004 15:15:00 +0100, Alex Fraser wrote:
AIUI, this does not necessarily allocate sufficient space for 10
elements in array:

struct someStruct *s = malloc(sizeof(int) + 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.]
Huh ?


Please elaborate. Did you read my first reply to Dan Pop?
isn't the offset off empty arrays quaranteed to be the same as
sizeof(thestruct) in which it appears ?
Yes.
struct someStruct *s = malloc(sizeof(someStruct) + 10);


This is equivalent to the second example I gave.

Alex
Nov 14 '05 #18
Case - wrote:

Dan Pop wrote:

>>>>>>

Can we get at least SOME snippage from you two? I doubt there's any need
whatsoever for 9-level deep quotes.


Brian Rodenborn
Nov 14 '05 #19
In 'comp.lang.c', Keith Thompson <ks***@mib.org> 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.


No, C99 doesn't allow zero-sized arrays. There is a new feature
called "flexible array members" (discussed elsewhere in this thread),
but they're not implemented with zero-sized arrays.


Yes, I had cancelled my post... not everywhere, sadly...

--
-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 #20
In <40**********************@dreader2.news.tiscali.nl > Case - <no@no.no> writes:
I e-mailed Jacob and he replied that 'it would have been
better to say sizeof(struct) + 67, instead of sizeof(int). There
could be some obscure reasons for sizeof structure != sizeof int.'
Which doesn't necessarily mean that the padding is inserted *before*
the array.
So, originally he *did* mean to say that the 67 bytes would all
end up in the char array.
I was commenting about what he wrote, not about what he meant (I'm not
a mind reader) and what he wrote is still technically correct.
In 99% of the cases this is correct
for this struct when using sizeof(int), he also wrote. Once again:
it was this C-spec 1% my initial reply was about.


I claim it's 100% versus 0% as the padding *between* the two members is
purely theoretical. Feel free to prove me wrong, by mentioning an
*existing* *real world* implementation that inserts padding there.

Of course, this doesn't mean that correct code should use sizeof(int)
instead of sizeof(somestruct).

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #21
Dan Pop wrote:
I was commenting about what he wrote, not about what he meant (I'm not
a mind reader) and what he wrote is still technically correct.


With natural language, there's always a personal interpretation. It
was your personal choice to interpret in a technical way. It was my
personal choice to try to figure out what was meant, by looking at the
context.

Also, you called the following a "Severely broken interpretation.":

He said "more than sizeof(int), to allocate the size of the array".
IAW: 'Everything more than sizeof(int), is the size of the array'.

Language technically spoken, my interpretation is a correct possibility.
(Which happened to be precisely what the OP *meant*.)

So, how did you know my interpretation was wrong? Reading minds ....

Case

Nov 14 '05 #22
"Case" <no@no.no> wrote in message
news:40***********************@news.xs4all.nl...
Dan Pop wrote:
I was commenting about what he wrote, not about what he meant (I'm not
a mind reader) and what he wrote is still technically correct.


With natural language, there's always a personal interpretation. It
was your personal choice to interpret in a technical way. It was my
personal choice to try to figure out what was meant, by looking at the
context.


Actually, I think Dan was quite right to interpret it how he did, from a
technical standpoint. However, regardless of the intent, a likely
interpretation (the one that you and I formed by making an assumption) that
makes the comment technically incorrect is worth pointing out.

Alex
Nov 14 '05 #23
In <40***********************@news.xs4all.nl> Case <no@no.no> writes:
Dan Pop wrote:
I was commenting about what he wrote, not about what he meant (I'm not
a mind reader) and what he wrote is still technically correct.
With natural language, there's always a personal interpretation. It
was your personal choice to interpret in a technical way.


In a technical newsgroup, a technical interpretation is more than purely
a matter of personal choice.
It was my
personal choice to try to figure out what was meant, by looking at the
context.
Mind reading is always a risky endeavour. You could have formulated
your critique as: "if by ''this`` you meant ''that``, then you're wrong"
and I wouldn't have had any objection at all. As a matter of fact, you
mentioned the need for offsetof in a correct solution, which is pure
bullshit.
Also, you called the following a "Severely broken interpretation.":

He said "more than sizeof(int), to allocate the size of the array".
IAW: 'Everything more than sizeof(int), is the size of the array'.
And I have provided a counterexample, haven't I?
Language technically spoken, my interpretation is a correct possibility.
Nope. He said more *without* specifying how much more. Therefore, your
statement is not equivalent to his, even in a technical context.
His statement is correct for a certain value of "more" and this is enough.
(Which happened to be precisely what the OP *meant*.)
Which is *irrelevant* to this disucussion, as you should have realised
long ago... We were discussing whether his statement was technically
correct or not, not what he actually meant when he made it (which involves
mind reading).
So, how did you know my interpretation was wrong? Reading minds ....


I've never said that your interpretation didn't match his intent, have I?

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #24

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


Does it works for all types(including structures) or it works only for
array of char's?

Nov 14 '05 #25

Richard Delorme wrote:
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 #26

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 !=...
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);"...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.