Let's say I have a struct that is a list such as
typedef struct mylist {
int head;
int tail;
int size;
buckets_t *bucketslots;
} mylist_t;
and then
void create_list(mylist_t* list, int size) {
list->head = 0;
list->tail = 0;
list->size = size;
list->bucketslots = malloc(sizeof(bucket_t) * size);
}
Now, in my main method, if I perform:
mylist_t list1;
create_list(&list1, 1000);
Does the second to last line automatically malloc space for list1? Or, do I need to explicitly malloc space through
malloc(sizeof(mylist_t))
?
What I'd like to really know is if this is a valid procedure to make a struct of type mylist and start using it in main.
EDIT
It appears that C puts list1 on the stack when I declare the second to last line, but it does not go on the heap. If I want it on the heap, should I do the following? :
mylist_t* create_list(int size) {
mylist_t list1 = malloc(sizeof(mylist_t));
list->head = 0;
list->tail = 0;
list->size = size;
list->bucketslots = malloc(sizeof(bucket_t) * size);
return list1;
}
And then for calling the function:
create_list(1000);
With this, will I have access to list1 throughout the program?
mylist_t list1;, when in block scope, creates an object of typemylist_twith automatic storage duration. What is unclear about that?list1was allocated when you declared it! Its not a pointer variable which needs to be assigned memory.