By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,820 Members | 1,189 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,820 IT Pros & Developers. It's quick & easy.

Q: memcpy str to struct

P: n/a
LO everybody,

I have a string (defined as a character array), and a structure which
comprises of unsigned chars, chars or char arrays. I want to copy the
'string' into the structure, in one action, using memcpy

Am I correct in assuming that I can't (or shouldn't) because of
structure padding? Lets say I've got a struct of

{
char a[6];
char b;
char c[3];
}

Could I expect a compiler to align 'b' to the next boundary?

PS my struct will only ever be made up of char types.
Thanks.
Jun 7 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Nope said:
LO everybody,

I have a string (defined as a character array), and a structure which
comprises of unsigned chars, chars or char arrays. I want to copy the
'string' into the structure, in one action, using memcpy

Am I correct in assuming that I can't (or shouldn't) because of
structure padding? Lets say I've got a struct of

{
char a[6];
char b;
char c[3];
}

Could I expect a compiler to align 'b' to the next boundary?
It's allowed to, but not required to. Some do, and some don't.

Do the copying field-by-field, and all will be well. For example,
assuming you have at least 10 bytes in your string...

start = 0;

memcpy(s.a, str + start, sizeof s.a);
start += sizeof s.a;
s.b = str[start];
start += sizeof s.b;
memcpy(s.c, str + start, sizeof s.c);
start += sizeof s.c;

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Jun 7 '07 #2

P: n/a
In article <4b********************************@4ax.com>,
Nope <no**@nopeynope.comwrote:
>I have a string (defined as a character array), and a structure which
comprises of unsigned chars, chars or char arrays. I want to copy the
'string' into the structure, in one action, using memcpy
>Am I correct in assuming that I can't (or shouldn't) because of
structure padding? Lets say I've got a struct of
>{
char a[6];
char b;
char c[3];
}
>Could I expect a compiler to align 'b' to the next boundary?
>PS my struct will only ever be made up of char types.
You are correct to be concerned. Compilers are allowed to add padding
between structure elements, and they don't have to tell you why.

Theoretically it could even be because they noticed that b was
being accessed a lot and so they padded to align b in such a way that
they could use a faster instruction series. For example, a compiler
could choose to store b and c within the same 64 bit word aligned
on a 64 bit boundary, with b occupying the numeric bottom byte,
and then to featch b by doing a 64 bit fetch and bitwise and-ing
the result with 0xff; on some systems that might be faster than
using a load-byte instruction (which might disrupt cache prefetch
operations.)

That said, I haven't -personally- encountered a compiler that would
not store the characters all in consequative memory positions
for arrays that small, but leaving two characters of padding between
a and b is -plausible- (so a could be all fetched at once in a single
64 bit fetch instruction.) And I seem to recall that with larger array
sizes and optimization cranked up to 11 on some of the compilers I've
used, that even character arrays might get aligned to take into
account cache line clashes.
--
"law -- it's a commodity"
-- Andrew Ryan (The Globe and Mail, 2005/11/26)
Jun 7 '07 #3

P: n/a
Thanks gents.
Jun 9 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.