Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ Struct - Define Minimum Size

Is there a C++ (or MSVC) method of automatically padding a struct to a minimum size? For example, imagine the following pseudo-code:

#pragma pad(256) // bytes
struct SETUPDATA {
  int var1;
  double var2;
};

where sizeof(SETUPDATA) = 256 bytes

The goal here being, during development this struct's members can change without changing the footprint size at runtime.

like image 934
Nicholas Avatar asked Dec 12 '22 14:12

Nicholas


1 Answers

You can use a union

struct SETUPDATA {
    union { struct your_data; char [256]; }
}

or something like this. This ensures it's at least 256 but only as long as your_data is not larger.

You can also add a simple assert after that just does a compiler check assert(sizeof(struct SETUPDATA) == 256)

like image 127
Jesus Ramos Avatar answered Dec 14 '22 04:12

Jesus Ramos