Structures and Memory Allocation in C
Memory Allocation for Structures
There are several ways to allocate memory for structures, depending on the specific requirements of your program.
Here are a few ways to allocate memory for structures in C:
Stack allocation
If you know the size of the structure at compile time, you can allocate it on the stack using the declaration syntax.
For example:
#include <stdio.h>
// define a structure named 'person'
struct person
{
int income;
int age;
};
int main()
{
// declare a variable 'p' of type 'person',
// which allocates enough memory on
// the stack to hold the entire structure
struct person p;
// set the values of the 'name' and 'age'
// members of the 'p' variable
p.income = 10000;
p.age = 30;
// print out the income and age of the
//person using the 'printf' function
printf("Income: %d\n", p.income);
printf("Age: %d\n", p.age);
return 0;
}
Output:
Explanation
- In this program, we define a person structure with two members:
name
andage
. - We then declare a variable
p
of typeperson
, which allocates enough memory on the stack to hold the entire structure.
Heap allocation
If you don't know the size of the structure at compile time, you can allocate it on the heap using the malloc
function.
For example:
#include <stdio.h>
#include <stdlib.h>
// define a structure named 'person'
struct person
{
char name[50];
int age;
};
int main()
{
// dynamically allocate memory on the heap
// for a 'person' structure, and
// assign a pointer to the structure to the 'p' variable
struct person *p = (struct person *)malloc(sizeof(struct person));
// check if the memory allocation was successful
if (p == NULL)
{
printf("Memory allocation failed\n");
return 1;
}
// set the values of the 'name' and 'age'
// members of the 'p' structure using
// the 'strcpy' and 'sprintf' functions
strcpy(p->name, "John Doe");
sprintf(&p->age, "%d", 30);
// print out the name and age of the person
// using the 'printf' function
printf("Name: %d\n", p->name);
printf("Age: %d\n", p->age);
// free the memory allocated for the 'p'
// structure using the 'free' function
free(p);
return 0;
}
Output:
Explanation
- In this program, we define a person structure with two members:
name
andage
. - We then dynamically allocate memory on the heap for a
person
structure using the malloc function, and assign a pointer to the structure to thep
variable.
Struct initialization
If you know the initial values of the structure members, you can initialize the structure using a struct initialization itself.
For example:
#include <stdio.h>
// define a structure named 'person'
struct person
{
char name[50];
int age;
};
int main()
{
// initialize a 'person' structure using a struct initializer
struct person p = {"John Doe", 30};
// print out the name and age of the person using the 'printf' function
printf("Name: %s\n", p.name);
printf("Age: %d\n", p.age);
return 0;
}
Output:
Explanation
- In this program, we define a person structure with two members:
name
andage
. - We then initialize a person structure using a struct initializer, which sets the
name
member to "John Doe" and theage
member to 30.