I am curious to know if keeping the metadata header around the string will be safe and not implementation dependent?
I am not sure that the following works will work on different platforms either if there is something that can lead to incorrect fields sizeor totalor if there is a problem with reallocthe buffer
#include <stdlib.h>
#include <stdio.h>
typedef struct{
int size;
int total;
char buf[];
} String;
int get_str_size(char *str){
String *pointer = (void*)(str-sizeof (String));
return pointer->size;
}
int get_str_total(char *str){
String *pointer = (void*)(str-sizeof (String));
return pointer->total;
}
char *init_string(int sz){
size_t header_size = 1 + sz + sizeof(String);
String *pointer = calloc(header_size, 1);
pointer->total = 0;
pointer->size = sz;
return pointer->buf;
}
char *realloc_string(char *str, int sz){
int old = get_str_size(str);
int new = old + sz;
String *pointer1 = (void*)(str-sizeof (String));
size_t header_size = 1 + new + sizeof(String);
String *pointer2 = realloc(pointer1, header_size);
return pointer2->buf;
}
int main(void){
char *str = NULL;
str = init_string(10);
printf("Length of str:%d\n", get_str_size(str));
printf("Total malloc'd:%d\n", get_str_total(str));
free(str - sizeof (String));
return 0;
}
source
share