Assume that I have a structure as below:
struct line {
int length;
char contents[];
};
struct line *thisline = (struct line *) malloc (sizeof (struct line) + this_length);
thisline->length = this_length;
Where is the allocated space for contents? In heap or in the coming address after length?
Both. It’s in the heap, since
thislinepoints to an allocated buffer in the heap. The extra size you’ve requested in themalloc()call serves as the allocated area forthisline->contents. Thus,thisline->contentsbegins right afterthisline->length.