Created
March 13, 2021 06:18
-
-
Save nyrahul/d3df672c7dd7153da69d7fe35d1af1d6 to your computer and use it in GitHub Desktop.
Compile time check to validate structure size
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#define BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2 * !!(condition)])) | |
int main(void) | |
{ | |
struct t { | |
int x; | |
int y; | |
int z; | |
}; | |
BUILD_BUG_ON(sizeof(struct t) < 10); // This passes compilation | |
BUILD_BUG_ON(sizeof(struct t) < 20); // This fails compilation | |
printf("ok\n"); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment