Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Padding and alignment

Reply
Thread Tools

Padding and alignment

 
 
Spoon
Guest
Posts: n/a
 
      02-12-2007
Hello everyone,

I had expected my C++ compiler to add padding within classes and structs
to align fields to their "natural" boundary. This seems not to be true.

$ cat align.cxx
#include <cstdio>
struct foo
{
virtual void f() const { }
long long x;
};
int main()
{
foo bar;
printf("%p %p\n", (void *)&bar, (void *)&bar.x);
return 0;
}

$ ./a.out
0xbfd9da00 0xbfd9da04

On my platform (x86 Linux g++) long long is 64-bits wide.
(I suppose the first 4 bytes store the v-pointer.)
Why didn't the compiler add padding to align x to a 64-bit boundary?

Regards.
 
Reply With Quote
 
 
 
 
red floyd
Guest
Posts: n/a
 
      02-12-2007
Spoon wrote:
> Hello everyone,
>
> I had expected my C++ compiler to add padding within classes and structs
> to align fields to their "natural" boundary. This seems not to be true.
>
> $ cat align.cxx
> #include <cstdio>
> struct foo
> {
> virtual void f() const { }
> long long x;
> };
> int main()
> {
> foo bar;
> printf("%p %p\n", (void *)&bar, (void *)&bar.x);
> return 0;
> }
>
> $ ./a.out
> 0xbfd9da00 0xbfd9da04
>
> On my platform (x86 Linux g++) long long is 64-bits wide.
> (I suppose the first 4 bytes store the v-pointer.)
> Why didn't the compiler add padding to align x to a 64-bit boundary?
>
> Regards.


That's an implementation specific issue, having nothing to do with the
language itself, and is therefore OT in comp.lang.c++. May I suggest
asking in gnu.g++.help?

Followups set to gnu.g++.help

 
Reply With Quote
 
 
 
 
Rolf Magnus
Guest
Posts: n/a
 
      02-12-2007
Spoon wrote:

> Hello everyone,
>
> I had expected my C++ compiler to add padding within classes and structs
> to align fields to their "natural" boundary. This seems not to be true.
>
> $ cat align.cxx
> #include <cstdio>
> struct foo
> {
> virtual void f() const { }
> long long x;
> };
> int main()
> {
> foo bar;
> printf("%p %p\n", (void *)&bar, (void *)&bar.x);
> return 0;
> }
>
> $ ./a.out
> 0xbfd9da00 0xbfd9da04
>
> On my platform (x86 Linux g++) long long is 64-bits wide.
> (I suppose the first 4 bytes store the v-pointer.)
> Why didn't the compiler add padding to align x to a 64-bit boundary?


What makes you think it should? Generally, C++ doesn't dictate any
alingment. On some 32 bit systems, the maximum needed alignment is 32 bits,
so why waste memory by using an alignment greater than needed if you don't
gain anything from it?
I'm writing C++ code for an 8 bit platform which doesn't have any specific
alignment reqirements, so every type is byte aligned.


 
Reply With Quote
 
Grizlyk
Guest
Posts: n/a
 
      02-12-2007

Spoon wrote:
>
> struct foo
> {
> virtual void f() const { }
> long long x;
> };
>
> int main()
> {
> foo bar;
> printf("%p %p\n", (void *)&bar, (void *)&bar.x);
> return 0;
> }
>
> $ ./a.out
> 0xbfd9da00 0xbfd9da04
>
> On my platform (x86 Linux g++) long long is 64-bits wide.
> (I suppose the first 4 bytes store the v-pointer.)


Wait.
1. What is v-pointer?
2. Why member foo: has nonzero offset from foo memory start?

--
Maksim A. Polyanin

"In thi world of fairy tales rolls are liked olso"
/Gnume/


 
Reply With Quote
 
Pan
Guest
Posts: n/a
 
      02-12-2007
Rolf Magnus wrote:

> > On my platform (x86 Linux g++)

> I'm writing C++ code for an 8 bit platform which doesn't have any specific
> alignment reqirements, so every type is byte aligned.


BTW, x86 architecture don't have specific alignment requirements, too.

--
Marco
 
Reply With Quote
 
red floyd
Guest
Posts: n/a
 
      02-12-2007
Grizlyk wrote:


DISCLAIMER: THIS IS OFF-TOPIC, AS THE STANDARD DOES NOT DISCUSS
VPTR/VTBL MECHANISMS.

However, as most, if not all, implementations use it....

> 1. What is v-pointer?

Pointer to the virtual function table for the class (not required by the
standard, but most known implementations do this).

> 2. Why member foo: has nonzero offset from foo memory start?

For the vptr. Each object with virtual functions in g++ -- again, not
required by the STandard -- has a pointer to a per-class table which
contains the addresses of the virtual functions for that class. It
generally lives at the start of the class, hence the rationale behind
foo: not having 0 offset.

 
Reply With Quote
 
Grizlyk
Guest
Posts: n/a
 
      02-12-2007

red floyd wrote:
>
> However, as most, if not all, implementations use it....
>
>> 1. What is v-pointer?

> Pointer to the virtual function table for the class (not required by the
> standard, but most known implementations do this).
>
>> 2. Why member foo: has nonzero offset from foo memory start?

> For the vptr. Each object with virtual functions in g++ -- again, not
> required by the STandard -- has a pointer to a per-class table which
> contains the addresses of the virtual functions for that class. It
> generally lives at the start of the class, hence the rationale behind
> foo: not having 0 offset.


For the first, there are no virtual functions declared in the class, so no
vtable exist.

For the second, I have read many times, that C++ classes have designed to be
similar to C structures, so C++ data is very like to C-structure, but
C-structure has zero offset for first member and has well-defined theorder
of the members.

I do not think, that the equality is important or needed, but to make it,
for C++ extra hidden data, as pointer to vtable and so on, because of sizeof
of all visible data is known at compile time, all extra hidden data can be
placed _after_ all. For inherited classes new data can be placed after all
data of base class. But may be pointer to vtable befor all data is more
regular than pointer to vtable mixed with data.

--
Maksim A. Polyanin

"In thi world of fairy tales rolls are liked olso"
/Gnume/




 
Reply With Quote
 
Robert Mabee
Guest
Posts: n/a
 
      02-12-2007
Rolf Magnus wrote:
> Generally, C++ doesn't dictate any
> alingment. On some 32 bit systems, the maximum needed alignment is 32 bits,
> so why waste memory by using an alignment greater than needed if you don't
> gain anything from it?
> I'm writing C++ code for an 8 bit platform which doesn't have any specific
> alignment reqirements, so every type is byte aligned.


It's often recommended to align primitive types same as their size (if
not silly like 80 bit floating point). This comes from sad experience
with CPUs that evolve to wider busses, making the former two-word type
either inefficient or fatal to access on the "wrong" alignment. An
otherwise compatible CPU requires painful coding to use such things as
structures saved on disk.

Sounds like a short-sighted mistake in adding 64 bit types to the x86
compiler. Too late to fix it. It is now up to the user to craft any
structure that might be permanent or exported so that it has explicit
padding to this "natural" alignment. Probably it makes no sense to
export an object with a vtable pointer in a raw form so its effect on
alignment can be ignored.
 
Reply With Quote
 
red floyd
Guest
Posts: n/a
 
      02-12-2007
Grizlyk wrote:
> red floyd wrote:
>
>
> For the first, there are no virtual functions declared in the class, so no
> vtable exist.
>


Really? From the OP:

> struct foo
> {
> virtual void f() const { }
> long long x;
> };



> For the second, I have read many times, that C++ classes have designed to be
> similar to C structures, so C++ data is very like to C-structure, but
> C-structure has zero offset for first member and has well-defined theorder
> of the members.
>


Only for POD.
 
Reply With Quote
 
red floyd
Guest
Posts: n/a
 
      02-12-2007
Grizlyk wrote:

> For the first, there are no virtual functions declared in the class, so no
> vtable exist.


Really? From the OP:

> struct foo
> {
> virtual void f() const { }
> long long x;
> };
>
> For the second, I have read many times, that C++ classes have designed to be
> similar to C structures, so C++ data is very like to C-structure, but
> C-structure has zero offset for first member and has well-defined theorder
> of the members.
>


Only for POD.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
std::vector padding behavior and alignment Andrew Tomazos C++ 7 06-24-2009 11:17 AM
Memory Padding and alignment Paul_Huang C++ 2 09-21-2004 06:05 AM
Allocation schema w.r.t. padding and alignment... SenderX C++ 2 05-13-2004 08:51 PM
memcpy problem with padding for word alignment!!! very urgent Ninan Thomas C++ 3 08-22-2003 06:19 AM
memcpy problem with padding for word alignment!!! very urgent Ninan Thomas C Programming 3 08-22-2003 06:19 AM



Advertisments