Skip to content

Instantly share code, notes, and snippets.

@Giacom
Created December 17, 2019 16:39
Show Gist options
  • Select an option

  • Save Giacom/1a9cffb86eaeadd6e032feda92df260b to your computer and use it in GitHub Desktop.

Select an option

Save Giacom/1a9cffb86eaeadd6e032feda92df260b to your computer and use it in GitHub Desktop.
// Type your code here, or load an example.
#include <cstddef>
#include <cstdlib>
#include <cstdio>
typedef unsigned long buf_t;
struct T {
unsigned long size;
T* next;
buf_t data[0];
};
static buf_t buffer[200];
constexpr auto buffer_size = sizeof(buffer) / sizeof(buffer[0]);
static unsigned int head = 4;
int main() {
unsigned char* data = (unsigned char*)buffer;
for (int i = 0; i < buffer_size; i++) {
buffer[i] = (buf_t)-1;
}
T* t = (T*)(data + head);
const int length = 5;
t->size = length;
const int byte_size_a = (offsetof(T, next) * 2) + (t->size * sizeof(buf_t));
//printf("%d\n", byte_size);
//T* t = (T*)malloc(byte_size);
for (unsigned int i = 0; i < t->size; i++) {
t->data[i] = (buf_t)i + 1;
}
head += byte_size_a;
T* tt = (T*)(data + head);
t->next = (T*)0xA1A2A3A4A5A6A7A8;
tt->next = (T*)-1;
tt->size = length + 5;
for (unsigned int i = 0; i < tt->size; i++) {
tt->data[i] = (buf_t)i * 2;
}
for (unsigned int i = 0; i < buffer_size * 2; i++) {
printf("[%d- %d]: \t%u\n", i, i * 4, data[i]);
}
//return sizeof(T);
return offsetof(T, data[0]);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment