Topic: Effect of volatile on struct members?


Author: k04jg02@gmail.com
Date: Fri, 1 Dec 2006 10:31:50 CST
Raw View
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                      ]





Author: Seungbeom Kim <musiphil@bawi.org>
Date: Fri, 1 Dec 2006 15:59:46 CST
Raw View
k04jg02@gmail.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.

> 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                      ]





Author: "James Kanze" <james.kanze@gmail.com>
Date: Fri, 1 Dec 2006 17:19:13 CST
Raw View
Seungbeom Kim wrote:
> k04jg02@gmail.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@gmail.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                      ]