sizeof(struct) returns unexpected value

This should be simple but I have no clue where to look for the issue:

I have a struct:

struct region
{
public:
    long long int x;
    long long int y;
    long long int width;
    long long int height;
    unsigned char scale;
};

When I do sizeof(region) it gives me 40 when I am expecting 33.

Any ideas?

(mingw gcc, win x64 os)

It's padding the struct to fit an 8-byte boundary. So it actually is taking 40 bytes in memory - sizeof is returning the correct value.

If you want it to only take 33 bytes then specify the packed attribute:

struct region
{
public:
    long long int x;
    long long int y;
    long long int width;
    long long int height;
    unsigned char scale;
} __attribute__ ((packed));

c++ - sizeof(struct) returns unexpected value -, it's padding struct fit 8-byte boundary. taking 40 bytes in memory - sizeof returning correct value. if want take 33 bytes specify packed attribute: struct region  So it actually is taking 40 bytes in memory - sizeof is returning the correct value. If you want it to only take 33 bytes then specify the packed attribute: struct region { public: long long int x; long long int y; long long int width; long long int height; unsigned char scale; } __attribute__ ((packed));

long long int values are 8 bytes each. scale is only 1 byte but is padded for alignments, so it effectively takes up 8 bytes too. 5*8 = 40.

[Resolved] sizeof( long ) returns unexpected value, is this a bug , Peter. PS. for instance the following structure should be 8 bytes in size, assuming a packing of 1: struct {. long l;. char c;. short s;. }; sizeof. The code calls sizeof() on a malloced pointer type, which always returns the wordsize/8. This can produce an unexpected result if the programmer intended to determine how much memory has been allocated.

As others said, structs are padded for alignments, and such padding not only depends on the type of the members, but also on the order of the members in which they're defined.

For example, consider these two structs A and B as defined below. Both structs are identical in terms of members and types; the only difference is that the order in which members are defined isn't same:

struct A
{
    int i;
    int j;
    char c;
    char d;
};

struct B
{
    int i;
    char c;
    int j;
    char d;
};

Would the sizeof(A) be equal to sizeof(B) just because they've same number of members of same type? No. Try printing the size of each:

cout << "sizeof(A) = "<< sizeof(A) << endl;
cout << "sizeof(B) = "<< sizeof(B) << endl;

Output:

sizeof(A) = 12
sizeof(B) = 16

Surprised? See the output yourself : http://ideone.com/yCX4S

gcc sizeof(struct) giving unexpected value - gcc - html, I have a struct, Pe_SymbolHeader. It looks like this: typedef struct _Pe_SymbolHeader { char Name[8]; // 8 uint32_t Value; // 12 uint16_t SectionNumber; // 14  unexpected value for nargout when returning a Learn more about classdef, nargout, return value, handle, oop MATLAB

Data structure alignment, Data structure alignment refers to the way data is arranged and accessed in computer memory. For example, on a 32-bit machine, a data structure containing a 16-bit value followed by a mod align)) mod align will return zero, therefore the original value is left unchanged. The size of this structure would be 6 bytes. Unchecked return value leads to resultant integer overflow and code execution. CVE-2006-4447 Program does not check return value when invoking functions to drop privileges, which could leave users with higher privileges than expected by forcing those functions to fail.

Types, To handle any unexpected values, you should use the revert function to revert the whole transaction, or return a tuple with a second bool value denoting success. This restriction is necessary, as the size of the struct has to be finite. Note how  Note that a.fctA().fctB also returns 1. I have entered a bug report for the crash with a nested function but I would not expect that code to be legal MATLAB code in the foreseeable future. Information stated here is not an official guarantee of support or statement of policy from MathWorks.

dbghelp2/pdb.c at master · vtjnash/dbghelp2 · GitHub, reader->u.jg.header = PRD(0, sizeof(struct PDB_JG_HEADER)); if (*pdw++ != 0). {. printf("unexpected value\n");. return -1;. } for (i = 0; i < count; i++). The reason is that when i call the a::clone() from inside the instance structure it successfully creates the new a, returns the pointer to ret but then the function returns some random gibberish pointer and not ret. Wth is going on here?

Comments
  • @sth: What was the point of those edits?
  • @eriktous: I liked the question and tried to increase the signal-to-noise ratio by removing the text that didn't add anything to the question.
  • Note that using an array of packed structures can severely hamper performance due to elements of the struct being unaligned.
  • Is there a way to get some kind of packed_sizeof(MyStruct)? This way, one could use the struct as unpacked and get good performance, but you can use the real size of the struct for reading from the file
  • @codymanix: how are you saving the struct to a file? if you have an array of unpacked structs, say 10 of them, then it'll save 400 bytes to the file. you'll want to read 400 bytes back in order to get 10 properly-formed structs. reading 330 bytes will leave you short a few. but also you shouldn't save and load to a file dependent on the struct padding/alignment since different compilers might do things differently. pack it manually to the file and read it back manually.
  • @codymanix: You assume that the padding is always at the end. It's not true, padding can occur anywhere in the middle, see the post from Nawaz. Padding and unpadding is typically tedious boilerplate code, just like file I/O in general. Boost.Serialization helps a bit, but it isn't completely automatic either. There's no C# [Serializable] equivalent in C++.
  • brilliant, I didn't know that... any way of avoiding it? want to read the struct from file and need to get the exact size
  • @George: it usually pays off to store data as plain text instead of in binary formats. Check out the Power of Plain Text section in the Pragmatic Programmer book.
  • @George, most compilers have an attribute syntax which can specify the packing on a structure. In gcc I think it is __attribute__((packed)), look that up. Note of course your binary data is totally non-portable.
  • @George: You may be asking for worse trouble by reading binary data from a file straight into a struct, because of byte ordering. You may be better with your struct having padding and reading fields separately, writing them to elements of the struct.
  • @George, certain architectures don't like misaligned data types -- though most will allow it. Different compilers will also do different things, but there is likely a way in all of them to get the packing right. Endianess may be an issue between chipsets. And yes of course, the size, use the specifically sized types like uint32_t, since the size of those types varies between chips and even OSs.