Re: Array assignment via struct
Lawrence Kirby wrote:[color=blue]
> On Sat, 06 Aug 2005 08:02:38 -0400, Joe Wright wrote:
>
>[color=green]
>>Jack Klein wrote:
>>[color=darkred]
>>>On Fri, 05 Aug 2005 16:24:25 -0400, Joe Wright <jwright@comcas t.net>
>>>wrote in comp.lang.c:
>>>
>>>
>>>
>>>>Lawrence Kirby wrote:[/color][/color]
>
>
> ...
>
>[color=green][color=darkred]
>>>> *(S*)A = sa;
>>>
>>>
>>>Here is where you invoke undefined behavior, since A isn't dynamically
>>>allocated. There is no guarantee that A meets the alignment
>>>requiremen ts for an S. The compiler might generate code that assumes
>>>that A is, causing some sort of trap on some platforms, or possible
>>>misaligned data or overwriting the destination array.
>>>[/color]
>>
>>All of S is an array of char. What alignment requirements might there be
>>for an S? None. Structures don't have alignment requirements, their
>>members do. What are the alignment requirements of a char array?[/color]
>
>
> Any object type can have alignment requirements. A structure's alignment
> requirements must meet the requirements of all of its members, but there's
> nothing to stop it being stricter. The reason for doing this is speed,
> word aligned access can be faster even for smaller objects. Consider for
> example optimised strcpy() memcpy() etc. code that operates a word at a
> time.
>
>
>[color=green][color=darkred]
>>>I strongly dislike people who write code like this. Especially if I
>>>have to clean up after the 'clever' programmer. It would never pass a
>>>code inspection at any shop with decent standards. Shops that don't
>>>do code inspections don't have decent standards by definition.
>>>[/color]
>>
>>You 'strongly dislike people' who try to get 'clever' with C in a
>>newsgroup posting? Boy, are you tough.[/color]
>
>
> When the "clever" method is obscure and possibly wrong (or not easy to
> prove correct) and there is "dumb" simple, clear and correct method
> available I'd have to agree.
>
>[color=green]
>>I thought you'd get me for not checking the malloc() returns and not
>>free()ing ps and pa before exit. You never know your luck.[/color]
>
>
> There's that too. :-)
>
> Lawrence[/color]
Ok, I give up.
Too clever I suppose. Except for this thread, I don't think I've ever
done that: disguise an array as a struct so that it can be assigned to
or used as a value to be assigned to a struct.
Kieth, Stan, Netocrat and Tim notwithstanding , when Chris Torek and
Lawrence Kirby tell me I'm all wet, I'm wet.
It's not just C, I love this group too.
--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Lawrence Kirby wrote:[color=blue]
> On Sat, 06 Aug 2005 08:02:38 -0400, Joe Wright wrote:
>
>[color=green]
>>Jack Klein wrote:
>>[color=darkred]
>>>On Fri, 05 Aug 2005 16:24:25 -0400, Joe Wright <jwright@comcas t.net>
>>>wrote in comp.lang.c:
>>>
>>>
>>>
>>>>Lawrence Kirby wrote:[/color][/color]
>
>
> ...
>
>[color=green][color=darkred]
>>>> *(S*)A = sa;
>>>
>>>
>>>Here is where you invoke undefined behavior, since A isn't dynamically
>>>allocated. There is no guarantee that A meets the alignment
>>>requiremen ts for an S. The compiler might generate code that assumes
>>>that A is, causing some sort of trap on some platforms, or possible
>>>misaligned data or overwriting the destination array.
>>>[/color]
>>
>>All of S is an array of char. What alignment requirements might there be
>>for an S? None. Structures don't have alignment requirements, their
>>members do. What are the alignment requirements of a char array?[/color]
>
>
> Any object type can have alignment requirements. A structure's alignment
> requirements must meet the requirements of all of its members, but there's
> nothing to stop it being stricter. The reason for doing this is speed,
> word aligned access can be faster even for smaller objects. Consider for
> example optimised strcpy() memcpy() etc. code that operates a word at a
> time.
>
>
>[color=green][color=darkred]
>>>I strongly dislike people who write code like this. Especially if I
>>>have to clean up after the 'clever' programmer. It would never pass a
>>>code inspection at any shop with decent standards. Shops that don't
>>>do code inspections don't have decent standards by definition.
>>>[/color]
>>
>>You 'strongly dislike people' who try to get 'clever' with C in a
>>newsgroup posting? Boy, are you tough.[/color]
>
>
> When the "clever" method is obscure and possibly wrong (or not easy to
> prove correct) and there is "dumb" simple, clear and correct method
> available I'd have to agree.
>
>[color=green]
>>I thought you'd get me for not checking the malloc() returns and not
>>free()ing ps and pa before exit. You never know your luck.[/color]
>
>
> There's that too. :-)
>
> Lawrence[/color]
Ok, I give up.
Too clever I suppose. Except for this thread, I don't think I've ever
done that: disguise an array as a struct so that it can be assigned to
or used as a value to be assigned to a struct.
Kieth, Stan, Netocrat and Tim notwithstanding , when Chris Torek and
Lawrence Kirby tell me I'm all wet, I'm wet.
It's not just C, I love this group too.
--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Comment