Skip to content

Instantly share code, notes, and snippets.

@JLospinoso
Created June 14, 2016 20:10
Show Gist options
  • Save JLospinoso/1abf58847c41b908764568a477256f46 to your computer and use it in GitHub Desktop.
Save JLospinoso/1abf58847c41b908764568a477256f46 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <memory>
#include "MemoryPrinter.h"
#define MEM_SIZE 32
#define HEADER "\x01\x02\x03\x04\x05\x06\x07\x08\x09"
#define HEADER_LENGTH 9
#define ALIGNMENT 8
#define BUFFER "\x10\x11\x12\x13\x14\x15\x16\x17"
#define BUFFER_LENGTH 8
void print(void *start, int length) {
int index = 0;
char *startAsChar = (char*)start;
printf("%u bytes starting at %x:\n", length, start);
while (index < length){
printf("%03u: ", index);
for (int column = 0; index < length && column < ALIGNMENT; column++, index++){
printf("0x%02x ", *(startAsChar + index));
}
printf("\n");
}
}
int main() {
char data[MEM_SIZE] = { 0 };
memcpy(data, HEADER, HEADER_LENGTH);
print(data, MEM_SIZE);
void *endOfHeader = &(data[0]) + HEADER_LENGTH;
std::size_t space = MEM_SIZE - HEADER_LENGTH;
void *nextAvailableByte = std::align(ALIGNMENT, BUFFER_LENGTH, endOfHeader, space);
memcpy(nextAvailableByte, BUFFER, BUFFER_LENGTH);
print(data, MEM_SIZE);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment