Sizeof returns different values ​​for the structure when compiling different programs

I have 2 programs that share a header file. This header file defines a multi-member structure. Then there is a #define: #define STRUCTURE_SIZE sizeof(OUR_STRUCTURE).

This structure is then used in shared memory with STRUCTURE_SIZE, which is used for the argument sizeto shmget().

Unfortunately, STRUCTURE_SIZE is 20758 for one program and 20764 for another. Therefore, when the second program tries to get a common mem, shmget()returns EINVAL.

uname -a:

Linux machine 2.6.30.10-105.2.23.fc11.i686.PAE #1 SMP Thu Feb 11 07:05:37 UTC 2010 i686 i686 i386 GNU/Linux

g++ --version:

g++ (GCC) 4.4.1 20090725 (Red Hat 4.4.1-2)

+3
source share
4 answers

A few possibilities:

  • / .
  • , , #pragma pack
  • , , -

(: , .)

+13

. , , .

+2

:

  • , , , ,
  • ( , )
+2

You could gain some insight by writing some code to check the field offsets in OUR_STRUCTUREand print them using two compilations in turn. Divide the total size of the structure to determine the size caused by each of its fields.

struct OUR_STRUCTURE
{
  double d;
  other_structure other;
  bool flag;
};

OUR_STRUCTURE ours;
cout << &ours.d - &ours << endl;
cout << &ours.other - &ours << endl;
cout << &ours.flag - &ours << endl;
cout << &ours + sizeof(OUR_STRUCTURE) - &ours.flag << endl;
+2
source

Source: https://habr.com/ru/post/1773234/


All Articles