the problem with multi inherited struct's memory-layout on linux

At the beginning, my problem is here:
http://www.cplusplus.com/forum/general/187028/

But now, I found something newly;
(And like preview problem, I will take the result both on Window and Linux, maybe in this topic, the result on Windows will be unuseful, I still put them )

Code1:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include "stdio.h"
#pragma  pack(4)

struct A	{ int a1;  char a2; };
struct B : public A	{ char b1; };

int main()
{
#define p_addr(var) printf("%-6s: %4u: %llu \n", #var, sizeof(var), &var)
	B s;
	printf("struct C: sizeof(c) = %u \n", sizeof(s));
	p_addr(s.a1);
	p_addr(s.a2);
	p_addr(s.b1);

	return 0;
}


here are the results:
Window 7, 64 bit, vs2012:
struct sizeof(s) = 12
s.a1  :    4: 3864736
s.a2  :    1: 3864740
s.b1  :    1: 3864744


(Linux) Centos 7.0, 64bit, gcc 4.9.1:
struct sizeof(s) = 12
s.a1  :    4: 140736078922720
s.a2  :    1: 140736078922724
s.b1  :    1: 140736078922728



Code 2:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include "stdio.h"
#pragma  pack(4)

struct A			{ int a1;  char a2; };
struct B : public A	{ int b1;  char b2; };
struct C : public B	{ char c1; };

int main()
{
#define p_addr(var) printf("%-6s: %4u: %llu \n", #var, sizeof(var), &var)
	C s;
	printf("struct sizeof(s) = %u \n", sizeof(s));
	p_addr(s.a1);
	p_addr(s.a2);
	p_addr(s.b1);
	p_addr(s.b2);
	p_addr(s.c1);

	return 0;
}


The result will be:
Window 7, 64 bit, vs2012:
struct sizeof(s) = 20
s.a1  :    4: 3733360
s.a2  :    1: 3733364
s.b1  :    4: 3733368
s.b2  :    1: 3733372
s.c1  :    1: 3733376


(Linux) Centos 7.0, 64 bit, gcc 4.9.1:
struct sizeof(s) = 16
s.a1  :    4: 140720557799712
s.a2  :    1: 140720557799716
s.b1  :    4: 140720557799720
s.b2  :    1: 140720557799724
s.c1  :    1: 140720557799725



Maybe you have found the problem:
If the struct just inherit once, the child memory a new 4 byte for the char-type;
And if the struct inherit twice or more, the grand-child share it's char-type with the char-type in the child struct on Linux.

What make this result?

Thanks for TheIdeasMan, and I add some question.
I want to know what policies gcc(Linux) and Windows are used for the (struct) member location, and how to make them same.

I write a program with net, the Server use Linux and Client use Windows. The Server and Client use the same header file (.h) for struct definition. To the multi inherited structs, the lengths and member addresses will be different between Server and Client. So when Client processes the data from Server, something will be wrong.
Last edited on
Moschops already answered your question in the previous thread.
What make this result?


Perhaps a more important question: Why are you worried about it?

People have asked before, because they want to write to a binary file. But then they discover it doesn't matter because they have to write each member individually, the sizeof the whole struct is irrelevant in that situation.

Maybe you have another reason to ask?
Topic archived. No new replies allowed.