Alignment wastes no memory now

This commit is contained in:
2020-06-15 17:38:15 +02:00
parent ebb7c6eeee
commit c7feb38582
3 changed files with 13 additions and 7 deletions

View File

@ -1,4 +1,5 @@
#!/bin/sh
rm -rf build
cmake .
make
./build/test.out

View File

@ -3,13 +3,17 @@
#include <stdlib.h>
union arraylist_meta {
max_align_t dummy_align;
double dummy_double;
long double dummy_long_double;
long long dummy_long_long;
void *dummy_ptr;
void (*dummy_func_ptr)(void);
struct {
size_t len;
size_t cap;
size_t sizeof_one_element;
size_t len;
size_t cap;
size_t sizeof_one_element;
};
};
};
void* arraylist_create(size_t array_size, size_t sizeof_one_element) {
union arraylist_meta *arraylist_new = malloc(array_size * sizeof_one_element + sizeof*arraylist_new);

View File

@ -1,10 +1,11 @@
#include <stdio.h>
#include "array.h"
#include <stddef.h>
int main() {
int *int_array = arraylist_create(2, sizeof (int));
int_array[0] = 28;
int_array[1] = 20;
for(int i = 0; i < 100; ++i) int_array = arraylist_append(int_array, &i);
for(int i = 0; i < 102; ++i) printf("Index: %d Value: %d\n", i, int_array[i]);
for(int i = 0; i < 10; ++i) arraylist_append(int_array, &i);
for(int i = 0; i < 12; ++i) printf("Index: %d Value: %d\n", i, int_array[i]);
}