C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Effect of volatile on struct members?

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language, library and standards
View previous topic :: View next topic  
Author Message
Guest






PostPosted: Fri Dec 01, 2006 4:31 pm    Post subject: Effect of volatile on struct members? Reply with quote



I was tinkering with the idea that it'd be nice if you could define a
struct that specified the layout of a file, and then be able to 'load'
that file by simply pointing a struct pointer to the beginning of the
file. You can't usually do this because of padding bytes inserted by
the compiler for efficient access. So I thought that maybe if I did
this:

struct example {
volatile char a;
volatile int x;
volatile double z;
};

That 'volatile' would prevent the compiler from adding those padding
bytes, because I've just told it I'm expecting the struct to be layed
out in a specific way by using that keyword. But in my tests, no matter
how I arrange it, I end up with a struct with the same memory layout as
one with all non-volatile members -- sizeof(example) is always 16
regardless of whether the struct members are volatile, even though the
sum of the members is only 13. I find this a little surprising, since
I'm compiling on an x86 machine so AFAIK unaligned accesses are allowed
although slow. Compiling with g++ 4.1.2 (Ubuntu). Is the compiler
ignoring the volatile keyword, or am I misinterpreting what its effect
should be?

---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
Back to top
Seungbeom Kim
Guest





PostPosted: Fri Dec 01, 2006 9:59 pm    Post subject: Re: Effect of volatile on struct members? Reply with quote



k04jg02 (AT) gmail (DOT) com wrote:
Quote:
I was tinkering with the idea that it'd be nice if you could define a
struct that specified the layout of a file, and then be able to 'load'
that file by simply pointing a struct pointer to the beginning of the
file. You can't usually do this because of padding bytes inserted by
the compiler for efficient access.

Padding is not the only thing that keeps you from doing that;
you may also have different byte orders ("endian") as well.

Quote:
That 'volatile' would prevent the compiler from adding those padding
bytes, because I've just told it I'm expecting the struct to be layed
out in a specific way by using that keyword.

Where do you get that idea from? 'volatile' has nothing to do with
memory layout. Rather, the standard specifies in 3.9.3/1: "The
cv-qualified or cv-unqualified versions [...] shall have the same
representation and alignment requirements." Therefore, if an int
has an alignment requirement of 4 bytes, then so does a volatile int.

--
Seungbeom Kim

---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
Back to top
James Kanze
Guest





PostPosted: Fri Dec 01, 2006 11:19 pm    Post subject: Re: Effect of volatile on struct members? Reply with quote



Seungbeom Kim wrote:
Quote:
k04jg02 (AT) gmail (DOT) com wrote:
I was tinkering with the idea that it'd be nice if you could define a
struct that specified the layout of a file, and then be able to 'load'
that file by simply pointing a struct pointer to the beginning of the
file. You can't usually do this because of padding bytes inserted by
the compiler for efficient access.

Padding is not the only thing that keeps you from doing that;
you may also have different byte orders ("endian") as well.

And other representation features may vary. For integer types,
I only know of one modern machine that doesn't use 2's
complement, but that's a fairly recent development. And for
floating point types, IBM mainframes don't use IEEE, and they're
not exactly rare.

And of course, even on everyday machines, the size of long and
long double varies---on some machines, it even varies depending
on whether you boot the machine as Linux or as Windows.

--
James Kanze (Gabi Software) email: james.kanze (AT) gmail (DOT) com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34


---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language, library and standards All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.