C programming language equivalent for structure initialization in C++

Viewed 103

Assuming list is some structure

list *temp;
temp = new list;

the above is in c++, does this have an equivalent in C?

1 Answers
list *temp;
temp = malloc(sizeof(list));

[...]

free(temp);

C doesn't have the new keyword - you've got to allocate and free the memory you'd like to use manually. new also does some work behind the scenes, like calling constructors and returning the proper pointer type - malloc doesn't cover that, and has to be handled by the programmer.

Related