Last active
September 5, 2017 02:28
-
-
Save roosemberth/ff04d40d738d68b48726 to your computer and use it in GitHub Desktop.
Effects of padding on variable declaration:
This file contains hidden or 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
/* Roosembert Palacios © 2015, Released under GPLv3 License: http://www.gnu.org/licenses/gpl-3.0.fr.html | |
* This piece of code is intended to show the effects of memory padding and the importance of the variable | |
* Allocation order. Please note that compiler warning "-Wpadded" is intentional and can be disabled by | |
* commenting line (10: #pragma GCC diagnostic warning "-Wpadded"). | |
*/ | |
#include <stdio.h> | |
#pragma GCC diagnostic ignored "-Wpointer-to-int-cast" | |
#pragma GCC diagnostic ignored "-Wint-to-pointer-cast" | |
#pragma GCC diagnostic ignored "-Wformat" | |
#pragma GCC diagnostic warning "-Wpadded" | |
struct S1{ | |
double a; | |
double b; | |
int c; | |
short d; | |
char e; | |
}; | |
struct S2{ | |
char a; | |
double b; | |
int c; | |
double d; | |
short e; | |
}; | |
struct S3{ | |
char a; | |
short b; | |
int c; | |
double d; | |
double e; | |
}; | |
const char structDefinitions[]="\ | |
struct S1{ struct S2{ struct S3{ \n\ | |
double a; char a; char a; \n\ | |
double b; double b; short b; \n\ | |
int c; int c; int c; \n\ | |
short d; double d; double d; \n\ | |
char e; short e; double e; \n\ | |
}; }; }; \n\ | |
"; | |
struct S1 s1; | |
struct S2 s2; | |
struct S3 s3; | |
void main(void){ | |
printf("This program is intended to show the effects of compiler padding and variable order on struct size\n\ | |
based in conjectures on Alignment in C seminar by Sven-Hendrik Haase. Online at http://goo.gl/jXDdMX\n\ | |
©2015 - Roosembert Palacios, Released under GPLv3 License: http://www.gnu.org/licenses/gpl-3.0.fr.html\n\ | |
\n"); | |
printf("Struct definitions:\n%s",structDefinitions); | |
printf("============================================================\n"); | |
printf("s1.member:address(size)\ts2.member:address(size)\ts3.member:address(size)\n"); | |
printf("------------------------------------------------------------\n"); | |
printf("s1->a:0x%x:(%d)\ts2->a:0x%x:(%d)\ts3->a:0x%x:(%d)\n", &s1.a, (int)(((int)&s1.b)-((int)&s1.a)), &s2.a, (int)(((int)&s2.b)-((int)&s2.a)), &s3.a, (int)(((int)&s3.b)-((int)&s3.a))); | |
printf("s1->c:0x%x:(%d)\ts2->c:0x%x:(%d)\ts3->c:0x%x:(%d)\n", &s1.b, (int)(((int)&s1.c)-((int)&s1.b)), &s2.b, (int)(((int)&s2.c)-((int)&s2.b)), &s3.b, (int)(((int)&s3.c)-((int)&s3.b))); | |
printf("s1->c:0x%x:(%d)\ts2->c:0x%x:(%d)\ts3->c:0x%x:(%d)\n", &s1.c, (int)(((int)&s1.d)-((int)&s1.c)), &s2.c, (int)(((int)&s2.d)-((int)&s2.c)), &s3.c, (int)(((int)&s3.d)-((int)&s3.c))); | |
printf("s1->d:0x%x:(%d)\ts2->d:0x%x:(%d)\ts3->d:0x%x:(%d)\n", &s1.d, (int)(((int)&s1.e)-((int)&s1.d)), &s2.d, (int)(((int)&s2.e)-((int)&s2.d)), &s3.d, (int)(((int)&s3.e)-((int)&s3.d))); | |
printf("s1->e:0x%x:(%d)\ts2->e:0x%x:(%d)\ts3->e:0x%x:(%d)\n", &s1.e, (int)(((int)&s1.a+sizeof(s1))-((int)&s1.e)), &s2.e, (int)(((int)&s2.a+sizeof(s2))-((int)&s2.e)), &s3.e, (int)(((int)&s3.a+sizeof(s3))-((int)&s3.e))); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment