hidden padding and memory size

hi, I have question here about hidden padding

if I make an aligned structure like this

struct __align__(16)  MyStruct {

	float u, v, w;

}

there will be 4bytes hidden padding.

So later if i want to store an array of MyStruct in cuda using cudaMalloc or cudaHostAlloc, I wonder how large the memory needed.

is it

sizeof(float) * 3 *size_of_array

or

sizeof(float) * 4 *size_of_array

? any clue?

thank you

I’d use simply:
sizeof(MyStruct)*size_of_array

nice trick, thanks…

but still I wanna know the effect of hidden padding, so i’m waiting for a more fundamental answer

In order to meet the alignment requirement, sizeof(MyStruct) has to equal a multiple of 16 bytes, so if sizeof(float) = 4 and you have 3 fields in the struct, then sizeof(MyStruct) = 16. What else could it be?

Use sizeof(MyStruct) to get the size of the structure.

Use sizeof(member) to get the size of a member in the structure.

Use offsetof(struct, member) to get the offset in bytes from the start of the structure.

If you allocate an array

MyStruct s[4] the compiler will correctly add the padding.

If you want to malloc an array of MyStruct then make sure to use sizeof(MyStruct) when calculating the size. You should never use the sum of the size of the children to determine the size of a structure.