Adjust structure size C

I want the size of the C structure to be a multiple of 16 bytes (16B / 32B / 48B / ..). It doesn't matter what size it fits, it should only be a multiple of 16B. How can I get the compiler to do this? Thanks.

+6
source share
4 answers

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); // use the pStruct for whatever free(pStruct); } 

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.

+5
source

For Microsoft Visual C ++:

 #pragma pack(push, 16) struct _some_struct { ... } #pragma pack(pop) 

For GCC:

 struct _some_struct { ... } __attribute__ ((aligned (16))); 

Example:

 #include <stdio.h> struct test_t { int x; int y; } __attribute__((aligned(16))); int main() { printf("%lu\n", sizeof(struct test_t)); return 0; } 

compiled with gcc -o main main.c , will output 16 . The same goes for other compilers.

+11
source

This is entirely up to the compiler and other tools, since alignment is not specified deeply in the ISO C standard (it indicates that alignment can occur at the request of compilers, but not go into details regarding its enforcement).

You will need implementation specifics for your compiler toolchain. It can provide #pragma pack (or align or some other stuff) that you can add to the structure definition.

It can also provide this as an extension of the language. For example, gcc allows you to add attributes to a definition, one of which controls alignment:

 struct mystruct { int val[7]; } __attribute__ ((aligned (16))); 
+5
source

Perhaps you can make a double structure by wrapping your actual structure in a second, which can add an addition:

 struct payload { int a; /*Your actual fields. */ float b; char c; double d; }; struct payload_padded { struct payload p; char padding[16 * ((sizeof (struct payload) + 15) / 16)]; }; 

Then you can work with the augmented structure:

 struct payload_padded a; apd = 43.3; 

Of course, you can use the fact that the first member of the structure starts 0 bytes from the beginning of the structure and processes the pointer to struct payload_padded , as if it were a pointer to struct payload (because it is):

 float d_plus_2(const struct payload *p) { return p->d + 2; } /* ... */ struct payload_padded b; const double dp2 = d_plus_2((struct payload *) &b); 
+3
source

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


All Articles