linux-c-programming.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* data structure question
@ 2005-05-15  9:32 gumbold
  2005-05-15 11:02 ` Glynn Clements
  0 siblings, 1 reply; 9+ messages in thread
From: gumbold @ 2005-05-15  9:32 UTC (permalink / raw)
  To: linux-c-programming

Hi

why somebody wants to write such thing
struct abc {
    __u16 a;
    __u16 b;
    char abc[0];
} __attribute__((packed));
Specialy for zerod array of chars.

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

* Re: data structure question
  2005-05-15  9:32 data structure question gumbold
@ 2005-05-15 11:02 ` Glynn Clements
  2005-05-15 11:24   ` gumbold
  0 siblings, 1 reply; 9+ messages in thread
From: Glynn Clements @ 2005-05-15 11:02 UTC (permalink / raw)
  To: gumbold; +Cc: linux-c-programming


gumbold wrote:

> why somebody wants to write such thing
> struct abc {
>     __u16 a;
>     __u16 b;
>     char abc[0];
> } __attribute__((packed));
> Specialy for zerod array of chars.

A structure which ends with a zero-length array is intended for use as
the "header" for a variable-sized block of data, e.g.:

	struct abc *new_abc(__u16 a, __u16 b, const char *string)
	{
		int len = strlen(string);
		struct abc *p = malloc(sizeof(struct abc) + len + 1);
	
		p->a = a;
		p->b = b;
		memcpy(p->abc, string, len + 1);
	
		return p;
	}

-- 
Glynn Clements <glynn@gclements.plus.com>

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

* Re: data structure question
  2005-05-15 11:02 ` Glynn Clements
@ 2005-05-15 11:24   ` gumbold
  2005-05-15 14:23     ` Glynn Clements
  0 siblings, 1 reply; 9+ messages in thread
From: gumbold @ 2005-05-15 11:24 UTC (permalink / raw)
  To: linux-c-programming

Glynn Clements wrote:

>gumbold wrote:
>
>  
>
>>why somebody wants to write such thing
>>struct abc {
>>    __u16 a;
>>    __u16 b;
>>    char abc[0];
>>} __attribute__((packed));
>>Specialy for zerod array of chars.
>>    
>>
>
>A structure which ends with a zero-length array is intended for use as
>the "header" for a variable-sized block of data, e.g.:
>
>	struct abc *new_abc(__u16 a, __u16 b, const char *string)
>	{
>		int len = strlen(string);
>		struct abc *p = malloc(sizeof(struct abc) + len + 1);
>	
>		p->a = a;
>		p->b = b;
>		memcpy(p->abc, string, len + 1);
>	
>		return p;
>	}
>
>  
>
So if my compiler can't handle such code, can i change it to
struct abc {
    __u16 a;
    __u16 b;
    char abc[1];
} __attribute__((packed));

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

* Re: data structure question
  2005-05-15 11:24   ` gumbold
@ 2005-05-15 14:23     ` Glynn Clements
  2005-05-16  4:00       ` gumbold
  0 siblings, 1 reply; 9+ messages in thread
From: Glynn Clements @ 2005-05-15 14:23 UTC (permalink / raw)
  To: gumbold; +Cc: linux-c-programming


gumbold wrote:

> > > why somebody wants to write such thing
> > > struct abc {
> > >     __u16 a;
> > >     __u16 b;
> > >     char abc[0];
> > > } __attribute__((packed));
> > > Specialy for zerod array of chars.
> > 
> > A structure which ends with a zero-length array is intended for use as
> > the "header" for a variable-sized block of data, e.g.:
> > 
> > 	struct abc *new_abc(__u16 a, __u16 b, const char *string)
> > 	{
> > 		int len = strlen(string);
> > 		struct abc *p = malloc(sizeof(struct abc) + len + 1);
> > 	
> > 		p->a = a;
> > 		p->b = b;
> > 		memcpy(p->abc, string, len + 1);
> > 	
> > 		return p;
> > 	}
> > 
> >   
> 
> So if my compiler can't handle such code, can i change it to
> struct abc {
>     __u16 a;
>     __u16 b;
>     char abc[1];
> } __attribute__((packed));

Yes, although you would need to adjust the size calculation if you
need to calculate the exact size of the overall block.

Zero-length arrays aren't valid in C89, but are in C99. Also, gcc has
supported them as an extension since long before it started supporting
C99.

-- 
Glynn Clements <glynn@gclements.plus.com>

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

* Re: data structure question
  2005-05-15 14:23     ` Glynn Clements
@ 2005-05-16  4:00       ` gumbold
  2005-05-16 15:28         ` Glynn Clements
  0 siblings, 1 reply; 9+ messages in thread
From: gumbold @ 2005-05-16  4:00 UTC (permalink / raw)
  To: linux-c-programming

Glynn Clements wrote:

>gumbold wrote:
>
>  
>
>>>>why somebody wants to write such thing
>>>>struct abc {
>>>>    __u16 a;
>>>>    __u16 b;
>>>>    char abc[0];
>>>>} __attribute__((packed));
>>>>Specialy for zerod array of chars.
>>>>        
>>>>
>>>A structure which ends with a zero-length array is intended for use as
>>>the "header" for a variable-sized block of data, e.g.:
>>>
>>>	struct abc *new_abc(__u16 a, __u16 b, const char *string)
>>>	{
>>>		int len = strlen(string);
>>>		struct abc *p = malloc(sizeof(struct abc) + len + 1);
>>>	
>>>		p->a = a;
>>>		p->b = b;
>>>		memcpy(p->abc, string, len + 1);
>>>	
>>>		return p;
>>>	}
>>>
>>>  
>>>      
>>>
>>So if my compiler can't handle such code, can i change it to
>>struct abc {
>>    __u16 a;
>>    __u16 b;
>>    char abc[1];
>>} __attribute__((packed));
>>    
>>
>
>Yes, although you would need to adjust the size calculation if you
>need to calculate the exact size of the overall block.
>
>Zero-length arrays aren't valid in C89, but are in C99. Also, gcc has
>supported them as an extension since long before it started supporting
>C99.
>
>  
>
So char abc[0] actualy pointer to chat? It should be 4 byte long on x86.
I can't see it with sizeof.

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

* Re: data structure question
  2005-05-16  4:00       ` gumbold
@ 2005-05-16 15:28         ` Glynn Clements
  2005-05-16 18:31           ` gumbold
  2005-05-16 19:07           ` gumbold
  0 siblings, 2 replies; 9+ messages in thread
From: Glynn Clements @ 2005-05-16 15:28 UTC (permalink / raw)
  To: gumbold; +Cc: linux-c-programming


gumbold wrote:

> So char abc[0] actualy pointer to chat?

No, it's an array of char.

> It should be 4 byte long on x86. I can't see it with sizeof.

It's zero bytes long.

If you had:

	struct abc {
	    __u16 a;
	    __u16 b;
	    char abc[4];
	};

then sizeof(struct abc) would be 8 and the offset of abc from the
beginning of the structure would be 4.

With:

	struct abc {
	    __u16 a;
	    __u16 b;
	    char abc[0];
	};

sizeof(struct abc) would be 4 and the offset of abc from the beginning
of the structure would still be 4 (i.e. p->abc refers to the first
byte after the structure).

-- 
Glynn Clements <glynn@gclements.plus.com>

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

* Re: data structure question
  2005-05-16 15:28         ` Glynn Clements
@ 2005-05-16 18:31           ` gumbold
  2005-05-17  7:36             ` Glynn Clements
  2005-05-16 19:07           ` gumbold
  1 sibling, 1 reply; 9+ messages in thread
From: gumbold @ 2005-05-16 18:31 UTC (permalink / raw)
  To: Glynn Clements; +Cc: linux-c-programming

Glynn Clements wrote:

>gumbold wrote:
>
>  
>
>>So char abc[0] actualy pointer to chat?
>>    
>>
>
>No, it's an array of char.
>
>  
>
>>It should be 4 byte long on x86. I can't see it with sizeof.
>>    
>>
>
>It's zero bytes long.
>
>If you had:
>
>	struct abc {
>	    __u16 a;
>	    __u16 b;
>	    char abc[4];
>	};
>
>then sizeof(struct abc) would be 8 and the offset of abc from the
>beginning of the structure would be 4.
>
>With:
>
>	struct abc {
>	    __u16 a;
>	    __u16 b;
>	    char abc[0];
>	};
>
>sizeof(struct abc) would be 4 and the offset of abc from the beginning
>of the structure would still be 4 (i.e. p->abc refers to the first
>byte after the structure).
>
>  
>
I understand. But if you doing `p->abc = (char *)malloc(111)' so you 
copy 4 bytes to the place
started from p->abc, but it is out of struct abc memory (it is only 4 
byte). Should be error?


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

* Re: data structure question
  2005-05-16 15:28         ` Glynn Clements
  2005-05-16 18:31           ` gumbold
@ 2005-05-16 19:07           ` gumbold
  1 sibling, 0 replies; 9+ messages in thread
From: gumbold @ 2005-05-16 19:07 UTC (permalink / raw)
  To: Glynn Clements; +Cc: linux-c-programming

Glynn Clements wrote:

>gumbold wrote:
>
>  
>
>>So char abc[0] actualy pointer to chat?
>>    
>>
>
>No, it's an array of char.
>
>  
>
>>It should be 4 byte long on x86. I can't see it with sizeof.
>>    
>>
>
>It's zero bytes long.
>
>If you had:
>
>	struct abc {
>	    __u16 a;
>	    __u16 b;
>	    char abc[4];
>	};
>
>then sizeof(struct abc) would be 8 and the offset of abc from the
>beginning of the structure would be 4.
>
>With:
>
>	struct abc {
>	    __u16 a;
>	    __u16 b;
>	    char abc[0];
>	};
>
>sizeof(struct abc) would be 4 and the offset of abc from the beginning
>of the structure would still be 4 (i.e. p->abc refers to the first
>byte after the structure).
>
>  
>
Ok. i understand everything. thank you very much for help.

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

* Re: data structure question
  2005-05-16 18:31           ` gumbold
@ 2005-05-17  7:36             ` Glynn Clements
  0 siblings, 0 replies; 9+ messages in thread
From: Glynn Clements @ 2005-05-17  7:36 UTC (permalink / raw)
  To: gumbold; +Cc: linux-c-programming


gumbold wrote:

> > then sizeof(struct abc) would be 8 and the offset of abc from the
> > beginning of the structure would be 4.
> > 
> > With:
> > 
> > 	struct abc {
> > 	    __u16 a;
> > 	    __u16 b;
> > 	    char abc[0];
> > 	};
> > 
> > sizeof(struct abc) would be 4 and the offset of abc from the beginning
> > of the structure would still be 4 (i.e. p->abc refers to the first
> > byte after the structure).
> 
> I understand. But if you doing `p->abc = (char *)malloc(111)'

That would be an error. p->abc is an array, not a pointer.

Arrays and pointers aren't the same. They are often confused, for
several reasons:

1. An array is automatically converted to a pointer to its first
element whenever it occurs as an expression. This doesn't apply to
lvalues (e.g. in declarations or as the argument to sizeof).

2. In a declaration, the syntax foo[...] declares an array named foo,
but in the expression foo[...], foo is a pointer (it might be an array
which has been converted to a pointer according to point 1 above, but
it's still a pointer).

3. In a function prototype, pointer arguments can be declared using
either "T foo[]" or "T *foo".

-- 
Glynn Clements <glynn@gclements.plus.com>

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

end of thread, other threads:[~2005-05-17  7:36 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2005-05-15  9:32 data structure question gumbold
2005-05-15 11:02 ` Glynn Clements
2005-05-15 11:24   ` gumbold
2005-05-15 14:23     ` Glynn Clements
2005-05-16  4:00       ` gumbold
2005-05-16 15:28         ` Glynn Clements
2005-05-16 18:31           ` gumbold
2005-05-17  7:36             ` Glynn Clements
2005-05-16 19:07           ` gumbold

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 NNTP newsgroup(s).