C Dynamic Struct Allocation
In this page:
struct StructName *pointer_name = malloc(sizeof(struct StructName));
pointer_name->member = value;
free(pointer_name);
Dynamically Allocating a Struct
A struct can be dynamically allocated on the heap using malloc(sizeof(struct StructName)), which reserves exactly enough memory to hold one instance of that struct, accessed through a pointer.
Example: Dynamically Allocating a Struct
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Include general utilities (malloc, free, exit)
#include <stdlib.h>
// Define a structure type named Point
struct Point { int x; };
// Program execution starts in main()
int main() {
// Allocate memory on the heap
struct Point *p = malloc(sizeof(struct Point));
p->x = 5;
// Print formatted text to the screen
printf("%d", p->x);
// Release the heap memory so it can be reused
free(p);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Accessing Members with ->
The -> operator accesses a member of a struct through a pointer in a single step, combining what would otherwise be a dereference followed by the dot operator, written as (*p).member.
Example: Accessing Members with ->
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Include general utilities (malloc, free, exit)
#include <stdlib.h>
// Define a structure type named Point
struct Point { int x; };
// Program execution starts in main()
int main() {
// Allocate memory on the heap
struct Point *p = malloc(sizeof(struct Point));
p->x = 10;
// Print formatted text to the screen
printf("%d", p->x);
// Release the heap memory so it can be reused
free(p);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Allocating an Array of Structs
An entire array of structs can be dynamically allocated at once by multiplying sizeof(struct StructName) by the desired element count, and each struct in the array is then accessed with regular indexing.
Example: Allocating an Array of Structs
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Include general utilities (malloc, free, exit)
#include <stdlib.h>
// Define a structure type named Point
struct Point { int x; };
// Program execution starts in main()
int main() {
// Allocate memory on the heap
struct Point *points = malloc(3 * sizeof(struct Point));
points[1].x = 7;
// Print formatted text to the screen
printf("%d", points[1].x);
// Release the heap memory so it can be reused
free(points);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Freeing a Dynamically Allocated Struct
A dynamically allocated struct, whether a single instance or an array of them, is released with a single call to free, exactly as with any other block of memory obtained from malloc.
Example: Freeing a Dynamically Allocated Struct
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Include general utilities (malloc, free, exit)
#include <stdlib.h>
// Define a structure type named Point
struct Point { int x; };
// Program execution starts in main()
int main() {
// Allocate memory on the heap
struct Point *p = malloc(sizeof(struct Point));
p->x = 1;
// Release the heap memory so it can be reused
free(p);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Structs with Dynamically Allocated Members
When a struct contains a pointer member that itself points to separately allocated memory, such as a dynamically sized name string, that inner allocation must be freed first, followed by the struct itself, to avoid leaking the inner memory.
Example: Structs with Dynamically Allocated Members
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Include general utilities (malloc, free, exit)
#include <stdlib.h>
// Define a structure type named Person
struct Person { char *name; };
// Program execution starts in main()
int main() {
// Allocate memory on the heap
struct Person *p = malloc(sizeof(struct Person));
// Allocate memory on the heap
p->name = malloc(10);
// Release the heap memory so it can be reused
free(p->name);
// Release the heap memory so it can be reused
free(p);
printf("Freed inner then outer");
return 0;
}
Login to try C/C++/Java/PHP code in the editor
- Using
.instead of->on a pointer to a struct, which does not compile. - Not checking whether
mallocreturnedNULLbefore using the struct. - Freeing the struct but not the memory its own pointer members point to, causing a leak.
Chapter Quiz — Complete all 8 topics to unlock
0/8 topics done
Complete these topics first: