Q: memcpy str to struct

N

Nope

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.
 
R

Richard Heathfield

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;
 
W

Walter Roberson

Nope said:
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.
 

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

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top