Created
February 3, 2014 01:51
-
-
Save drewfish/8777824 to your computer and use it in GitHub Desktop.
demonstrate memory alignment on the arduino
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
#include <Arduino.h> | |
#include <stddef.h> | |
#define PAD0 | |
#define PAD1 char pad0; | |
#define PAD2 char pad0; char pad1; | |
#define PAD4 char pad0; char pad1; char pad2; char pad3; | |
#define PAD8 char pad0; char pad1; char pad2; char pad3; char pad4; char pad5; char pad6; char pad7; | |
#define TEST_PAD(type, name, pad) \ | |
{ \ | |
struct Test ## pad ## _ ## name { PAD ## pad; type target; }; \ | |
Test ## pad ## _ ## name t ## pad; \ | |
Serial.print(F(#type " -- pad " #pad " -- size ")); \ | |
Serial.print(sizeof(t ## pad)); \ | |
Serial.print(F(" -- offset ")); \ | |
Serial.println( offsetof(Test ## pad ## _ ## name, target) ); \ | |
} | |
#define TEST(type) \ | |
TEST_PAD(type, type, 0); \ | |
TEST_PAD(type, type, 1); \ | |
TEST_PAD(type, type, 2); \ | |
TEST_PAD(type, type, 4); \ | |
TEST_PAD(type, type, 8); | |
#define TESTN(type, name) \ | |
TEST_PAD(type, name, 0); \ | |
TEST_PAD(type, name, 1); \ | |
TEST_PAD(type, name, 2); \ | |
TEST_PAD(type, name, 4); \ | |
TEST_PAD(type, name, 8); | |
void f() {}; | |
class C_simple { | |
public: | |
C_simple() {}; | |
void f() {}; | |
}; | |
class C_vtable { | |
public: | |
C_vtable() {}; | |
virtual void f() {}; | |
virtual ~C_vtable() {}; | |
}; | |
typedef void (*pfunc)(); | |
typedef void (C_simple::* pmf_C_simple)(); | |
typedef void (C_vtable::* pmf_C_vtable)(); | |
void setup() { | |
const __FlashStringHelper *blank = F(""); | |
Serial.begin(57699); | |
Serial.println(F("----------------------- BOOTED -----------------------")); | |
TEST(uint8_t); Serial.println(blank); | |
TEST(uint16_t); Serial.println(blank); | |
TEST(uint32_t); Serial.println(blank); | |
TEST(uint64_t); Serial.println(blank); | |
TEST(float); Serial.println(blank); | |
TEST(double); Serial.println(blank); | |
TEST(bool); Serial.println(blank); | |
TEST(char); Serial.println(blank); | |
TEST(byte); Serial.println(blank); | |
TEST(word); Serial.println(blank); | |
TEST(int); Serial.println(blank); | |
TEST(short); Serial.println(blank); | |
TEST(long); Serial.println(blank); | |
TESTN(long long, longlong); Serial.println(blank); | |
TESTN(int*, pointer); Serial.println(blank); | |
TEST(size_t); Serial.println(blank); | |
TEST(pfunc); Serial.println(blank); | |
TEST(pmf_C_simple); Serial.println(blank); | |
TEST(pmf_C_vtable); Serial.println(blank); | |
} | |
void loop() { | |
// everything interesting has already been done | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment