I this helloworld portable? I am vaguely aware of something called
"structure padding" and wonder if it could affect this program since the
struct only contains chars.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main (void)
{
struct
{
unsigned char var1[5];
unsigned char var2[6];
unsigned char var3[2];
} var =
{
{'H', 'e', 'l', 'l', 'o'},
{' ', 'W', 'o', 'r', 'l', 'd'},
{'!', '\n'}
};
char varcopy[sizeof(var)];
size_t i;
memcpy (varcopy, &var, sizeof(varcopy));
for (i = 0; i < sizeof(varcopy); i++)
putchar (varcopy[i]);
return EXIT_SUCCESS;
}
|
|
0
|
|
|
|
Reply
|
oompah62 (8)
|
5/20/2005 2:17:46 PM |
|
Stephen Mayes wrote:
> I this helloworld portable? I am vaguely aware of something called
> "structure padding" and wonder if it could affect this program since the
> struct only contains chars.
>
> #include <stdio.h>
> #include <stdlib.h>
> #include <string.h>
>
> int main (void)
> {
> struct
> {
> unsigned char var1[5];
> unsigned char var2[6];
> unsigned char var3[2];
> } var =
> {
> {'H', 'e', 'l', 'l', 'o'},
> {' ', 'W', 'o', 'r', 'l', 'd'},
> {'!', '\n'}
> };
> char varcopy[sizeof(var)];
> size_t i;
>
> memcpy (varcopy, &var, sizeof(varcopy));
>
> for (i = 0; i < sizeof(varcopy); i++)
> putchar (varcopy[i]);
>
> return EXIT_SUCCESS;
> }
>
>
The code is portable. However, the output of
your program may differ depending on platforms
and compiler versions.
The compiler is allowed to insert padding bytes
between members of a class, union or structure.
The value of the padding bytes is undefined.
--
Thomas Matthews
C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.comeaucomputing.com/learn/faq/
Other sites:
http://www.josuttis.com -- C++ STL Library book
http://www.sgi.com/tech/stl -- Standard Template Library
|
|
0
|
|
|
|
Reply
|
Thomas_MatthewsSpamBotsSuck (408)
|
5/20/2005 2:33:26 PM
|
|
"Stephen Mayes" <oompah62@hotmail.com> wrote:
# I this helloworld portable? I am vaguely aware of something called
# "structure padding" and wonder if it could affect this program since the
# struct only contains chars.
Some CPU prefer to load and store some types of operands are aligned to
particular byte boundaries. For example ints are often stored word boundaries,
at addresses that are a multiple of 4. Since struct fields are required to
be stored in order, packing them in tightly can put a field at an awkard
boundary. So the compiler can insert unused bytes between fields to
move a later field back to the next desired boundary.
The code will work in the sense that sizeof accounts for padding in the
struct, and memcpy copies padding bytes in sequence with field bytes.
Whether this struct has padding is up to the compiler. It's possible
that it would have 3 padding bytes at the end to bring the size to 16.
# #include <stdio.h>
# #include <stdlib.h>
# #include <string.h>
#
# int main (void)
# {
# struct
# {
# unsigned char var1[5];
# unsigned char var2[6];
# unsigned char var3[2];
# } var =
# {
# {'H', 'e', 'l', 'l', 'o'},
# {' ', 'W', 'o', 'r', 'l', 'd'},
# {'!', '\n'}
# };
# char varcopy[sizeof(var)];
# size_t i;
#
# memcpy (varcopy, &var, sizeof(varcopy));
#
# for (i = 0; i < sizeof(varcopy); i++)
# putchar (varcopy[i]);
#
# return EXIT_SUCCESS;
# }
#
--
SM Ryan http://www.rawbw.com/~wyrmwif/
So basically, you just trace.
|
|
0
|
|
|
|
Reply
|
wyrmwif (945)
|
5/20/2005 3:02:39 PM
|
|
The following article about structure alignment should help:
http://www.eventhelix.com/RealtimeMantra/ByteAlignmentAndOrdering.htm
Deepa
--
EventStudio 2.5 - http://www.EventHelix.com/EventStudio
Generate Sequence diagrams from a simple declarative language
|
|
0
|
|
|
|
Reply
|
eventhelix (355)
|
5/23/2005 11:13:57 AM
|
|
EventHelix.com wrote:
> The following article about structure alignment should help:
>
> http://www.eventhelix.com/RealtimeMantra/ByteAlignmentAndOrdering.htm
Note that the article is not about C, but about "C as
it might be implemented on a certain class of processors."
C is most certainly implemented on processors that do not
fit the article's model, so be sure to read the article as
an example, not as a definition.
Also, the example titled "Actual Structure Definition
Used By the Compiler" is wrong: the `pad3' field is larger
than the article's assumptions say it should be.
--
Eric.Sosman@sun.com
|
|
0
|
|
|
|
Reply
|
Eric.Sosman (4228)
|
5/23/2005 2:29:35 PM
|
|
On Fri, 20 May 2005 10:17:46 -0400, "Stephen Mayes"
<oompah62@hotmail.com> wrote:
> I this helloworld portable? I am vaguely aware of something called
> "structure padding" and wonder if it could affect this program since the
> struct only contains chars.
<snip: struct var containing several initialized char[] fields>
> char varcopy[sizeof(var)];
> size_t i;
>
> memcpy (varcopy, &var, sizeof(varcopy));
>
> for (i = 0; i < sizeof(varcopy); i++)
> putchar (varcopy[i]);
In practice portable but not quite in theory.
An implementation (compiler) is allowed to add padding after any
member of a struct for any reason, including "today's the third
Tuesday". In practice the only _good_ reason is for alignment, and no
char type in C can require nontrivial alignment, since a pointer to
(flavored) char must be able to access any byte of usable memory.
In the unlikely event there is padding, it is safely copied by memcpy
which accesses memory as unsigned char's. But the implementation
chooses whether to treat plain char like signed or unsigned, and if it
chooses signed that is permitted to have trap representations and the
padding could be such, and if so fetching it (for output) is Undefined
Behavior; but again there is probably no good reason any actual
implementation will screw up.
BTW you could instead just do one call:
fwrite (stdout, varcopy, sizeof varcopy, 1);
Or if you prefer ... varcopy, 1, sizeof varcopy.
Or indeed fwrite from var without copying at all.
- David.Thompson1 at worldnet.att.net
|
|
0
|
|
|
|
Reply
|
david.thompson1 (1042)
|
5/31/2005 3:49:12 AM
|
|
|
5 Replies
35 Views
(page loaded in 0.192 seconds)
|