Is this how one can use the the "extra" memory allocated while using the C struct hack?
Questions:
I have a C struct hack implementation below. My question is how can I use the "extra" memory that I have allocated with the hack. Can someone please give me an example on using that extra memory ?
#include<stdio.h>
#include<stdlib.h>
int main()
{
struct mystruct {
int len;
char chararray[1];
};
struct mystruct *ptr = malloc(sizeof(struct mystruct) + 10 - 1);
ptr->len=10;
ptr->chararray[0] = 'a';
ptr->chararray[1] = 'b';
ptr->chararray[2] = 'c';
ptr->chararray[3] = 'd';
ptr->chararray[4] = 'e';
ptr->chararray[5] = 'f';
ptr->chararray[6] = 'g';
ptr->chararray[7] = 'h';
ptr->chararray[8] = 'i';
ptr->chararray[9] = 'j';
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…