473,396 Members | 1,996 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Padding and alignment

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.
Feb 12 '07 #1
12 6374
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

Feb 12 '07 #2
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.
Feb 12 '07 #3

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::x has nonzero offset from foo memory start?

--
Maksim A. Polyanin

"In thi world of fairy tales rolls are liked olso"
/Gnume/
Feb 12 '07 #4
Pan
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
Feb 12 '07 #5
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::x 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::x not having 0 offset.

Feb 12 '07 #6
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.
Feb 12 '07 #7

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::x 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::x 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/


Feb 12 '07 #8
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.
Feb 12 '07 #9
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.
Feb 12 '07 #10

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;
};
Hmm? I did not see. If virtual exist vtable must be.

--
Maksim A. Polyanin

"In thi world of fairy tales rolls are liked olso"
/Gnume/
Feb 12 '07 #11
Pan wrote:
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.
It depends. Some instructions are slower when used on misaligned data, some
won't work at all.

Feb 12 '07 #12
Grizlyk wrote:
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.
POD ("plain old data") classes in C++ have that, too. Basically, a
simplified definition of "POD" is "similar to what C would do". Adding a
virtual member function makes your class non-POD.

Feb 13 '07 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: SenderX | last post by:
I am thinking of porting a library from C to C++, and was tinkering with ways to ensure proper struct padding and alignment. The library is very hardware specific, and will simply crash and/or...
2
by: Paul_Huang | last post by:
OK, I tried it with a piece of sample code to test the memory padding and alignment and get some weird results. I would appreciate if anybody can help to give a explain. Below is my sample code:...
10
by: Roman Mashak | last post by:
Hello, All! Could you please explain me how is the padding of 'structure' fields is made? For example: struct { char a1; int a2;
20
by: Lalatendu Das | last post by:
hi let's say i have a structure struct test { int A; char B; int C; }; this above structure defination always going to take 16 byte in memeory in whatever manner we align the member variables...
11
by: simonp | last post by:
I'm taking an intro course on C++, and our teacher is not being clear on how stuct memory padding is determined. If the memory used by all components defined inside a struct falls between a...
1
by: fdmfdmfdm | last post by:
code like: struct pid_tag{ unsigned int inactive : 1; unsigned int : 1; /* 1 bit of padding */ unsigned int refcount : 6; unsigned int : 0; /* pad to next word boundary...
3
by: abhivg | last post by:
Hi, I am trying to port a 32 bit Unix application to 64 bit Windows. While compiling on Windows I am getting a number of warnings related to structure padding. More specifically "warning C4820:...
24
by: karthikbalaguru | last post by:
Hi, I find that the structure padding is not being taken into account while using 'new' operator. Is there a way to enable it ? struct Dataunit { char dataid; int keyid;
12
by: Kislay | last post by:
case 1 : struct s { char c1; // 8 double d; // 8 int i1; // 4 char c2; // 4 int i2; // 4 };
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.