这是在使用 C struct hack 时如何使用分配的“额外”内存吗?
问题:
我在下面有一个 C struct hack 实现。我的问题是如何使用我分配给黑客的“额外”内存。有人可以给我一个使用额外内存的例子吗?
#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';
}