Array of size zero!

D

Dan Pop

In said:
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
 
C

Case

Dan said:
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
 
A

Alex Fraser

Case said:
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
 
D

Dan Pop

In said:
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
 
V

vashwath

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[];
};

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

vashwath

Richard said:
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[];
};
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top