Hello, I have a doubt about allocating memory related to a previous allocated memory; sorry if for such bad explanations (ok, and sorry for the title, I have no idea on how to resume that question), that's what I mean:

Code:
//compiled as C with DevCpp/MingW
#include <conio.h>
#include <stdio.h>

struct _SAMPLE
{
int *list;
};

int main()
{
int q,MAXL;
struct _SAMPLE *sample;

MAXL=4;
sample=(struct _SAMPLE*)malloc(sizeof(struct _SAMPLE));
if(!sample) {return 0;}
sample->list=(int*)malloc(MAXL*sizeof(int));
if(!sample->list) {free(sample);return 0;}

for(q=0;q<MAXL;q++)
    {
    sample->list[q]=q;
    }
free(sample->list);
free(sample);

getch();
return 0;
}
First I allocate memory for the 'sample' variable that's a type of 'struct _SAMPLE'. Inside 'sample' there's another variable that needs dynamic memory allocation; that previous code is right? I mean, ¿isthat the correct way to do it? Or should I have to allocate memory for all the data in the structure, something like

Code:
MAXL=4;
sample=(struct _SAMPLE*)malloc(sizeof(struct _SAMPLE)+(MAXL*sizeof(int)));
if(!sample) {return 0;}
sample->list=(int*)malloc(MAXL*sizeof(int));
if(!sample->list) {free(sample);return 0;}
That's because I have changed from C++ to C and I need the C++ vectors, so I have started with lists and b-trees; all the samples I have seen about that uses 'non mallocated variables' (sorry for that definition ) in structs like

Code:
...
struct _SAMPLE
{
int dontneedmalloc[4];
};
...
struct _SAMPLE *sample;
sample=(struct _SAMPLE*)malloc(sizeof(struct _SAMPLE));
sample->dontneedmalloc[0]=0;
...
Here I ask for a size of memory that's the size of the empty struct plus the size of the future memory allocation of the inner variable 'list'.

So there isn't necessary to allocate memory for the inner variable to work with it because is included in the 'sizeof(struct _SAMPLE)'. But when I have to allocate memory for the inner variable, should I have to allocate memory for it also when allocate mem for the container structure? (And reallocating it when I reallocate the inner var?)

Thank's in advance
Niara