The size of structure C will depend on the members of the structure, their types and their number. There is really no standard way to force the compiler to make structures more concise. Some compilers provide a pragma that allows you to set the alignment border, but thatβs a different matter. And there may be those who have such a setting or provide such a pragma.
However, if you insist that this method should perform memory allocation in the structure and force memory allocation to the next size of 16 bytes.
So, if you had such a structure.
struct _simpleStruct { int iValueA; int iValueB; };
Then you can do something like the following.
{ struct _simpleStruct *pStruct = 0; pStruct = malloc ((sizeof(*pStruct)/16 + 1)*16);
What would do is increase the size to 16 bytes in size. However, what the memory allocator does may or may not give you a block that is actually this size. A memory block may actually be larger than your request.
If you are going to do something special with this, for example, say that you are going to write this structure to a file and you want to know the size of the block, then you will need to do the same calculation that is used in malloc () instead of using the sizeof operator () to calculate the size of the structure.
So, the next would be to write your own sizeof () operator using a macro, for example.
#define SIZEOF16(x) ((sizeof(x)/16 + 1) * 16)
As far as I know, there is no reliable method for pulling the size of the selected block from the pointer. Typically, the pointer will have a memory allocation block that is used by the memory heap management functions that will contain memory management information, such as the size of the allocated block, which may actually be larger than the required amount of memory. However, the format of this block and its location relative to the actual memory address will depend on the execution time of the C compiler.