Sizeof(Struct) Returns Unexpected Value

sizeof(struct) returns unexpected value

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));

sizeof(struct) returns a SMALLER than expected value?

Why do you think it should be bigger?

It contains 3 uint32 with no virtual methods, that's exactly 12 bytes.

The union doesn't count, since it's a type, it contains no member of that type.

If you want your class to contain a single instance of the union, which contains a single instance of the struct, you should write:

struct Packet
{
uint32 MessageCount;
uint32 Length;
uint32 FieldValue;

union
{
uint32 typeInfo;
struct
{
uint8 MagicNumber[3];
uint8 Version;
} /* MagicVersion */;
};
} /* PacketHeader */;

The names within /**/ are optional, you can either specify them or not. If you do, you have to access their members using the name of the union/struct, otherwise you'll have a flat Packet struct.

sizeof giving unexpected result for my structure

The data members of struct are being aligned by default. There might be padding between these data members as well as padding after the last data member. In your case the padding will be most likely at the end.

The first data member is a pointer, which in your case requires 4 bytes of memory. Then although the other member is a char that requires only 1 byte of memory, there is a padding up to the multiple of 4, but the reason is not because "32 bits is what most computers are most comfortable with" as you say, but because 4 is the size of the largest data member.

Usually there is a pragma directive allowing you to specify custom alignment available. In Visual Studio, there is #pragma pack, that might help you in this case. Just make sure you know what you are doing. Although you will minimize the memory usage, it might negatively affect the performance of your code.

For more information have a look at related questions:

How to minimize the memory usage of a struct-type?

How does sizeof calculate the size of structures

Is the size of a struct required to be an exact multiple of the alignment of that struct?

or even Determining the alignment of C/C++ structures in relation to its members

Marshal.Sizeof() returning unexpected value

Add this to the structure:

[StructLayout(LayoutKind.Sequential, Pack = 1)]
internal class PROXY_HDR
{
public ushort pad;
public ushort label;
public byte flags;
public ushort length;
[MarshalAs(UnmanagedType.ByValArray,
SizeConst = 4, ArraySubType = UnmanagedType.U1)]
public byte[] ip;
public ushort port;
}

This will tell the compiler to treat it as a typical C-style array, not a pointer. This should be byte, as an IP address is an unsigned char array. char typically isn't used in these types of headers, it's usually byte (i.e: unsigned char)

Python struct.Struct.size returning unexpected value

Unless you specify any character for byte order, alignment, struct use native byte order, alignment(@); which cause padding.

By explicitly specifying byte order, you can get what you want:

>>> struct.Struct('!Bffffff').size  # network byte order
25
>>> struct.Struct('=Bffffff').size # native byte order, no alignment.
25
>>> struct.Struct('>Bffffff').size # big endian
25
>>> struct.Struct('<Bffffff').size # little endian
25
>>> struct.Struct('@Bffffff').size # native byte order, alignment. (+ native size)
28

Unexpected value returned from sizeof

sizeof is not a magical "how much space did I malloc into this pointer" function. The idiom you're using only works with real arrays, not pointers being dereferenced as arrays. Here, it means sizeof(int*)/sizeof(int), which is clearly not useful.

sizeof pointer to structure surprise

All the comments are hugely instructive. Thank you. User Dxiv gave the response I was looking for.
The "correct" syntax for what I was trying to do was:

sizeof(*pfoo);

or

sizeof(footype);

Credit to user Yunnosch for pointing out my question phrasing was off. I hope I am forgiven since my knowledge at the time of the post lacked what it now has after asking the question. The question should have been "how can I determine the size of the datatype pointed to by pfoo?"

Function returning unexpected struct values

You have to assign the returned value to the structure object in main

s1 = func(s1,x,y);

Inside the body the function deals with a copy of the original object. It does not change the original object because it is passed by value.

Another approach is to pass the structure by reference

void func( num &x, int a, int b) {
x.n[0] = a+b;
x.n[1] = a*b;
}

In this case in main you could just write

func(s1,x,y);

Or you could use even so-called C approach of passing by reference

void func( num *x, int a, int b) {
x->n[0] = a+b;
x->n[1] = a*b;
}

and call it like

func( &s1, x, y );

As for this statement

cout << func(s1,x,y).n[0] << "\n" << func(s1,x,y).n[1];  

then you access data members of two temporary objects returned by the two calls of the function. After executing this statement these temporary objects will be deleted.



Related Topics



Leave a reply



Submit