Reference: "Embedded Linux, C-language programming practice"
1. The use of program memory area
Storage area is divided into: static and dynamic
Static: read-only data area (RO DATA), has been initialized to read and write data area (RW DATA), an uninitialized read-write data area (BSS:Block Started by Symbol)
dynamic: heap Memory (heap) and stack memory (stack)
Heap memory is from low address to high address allocation, stack memory is from high address to low address allocation
2.C program stack space usage
In the C language program, the stack space is managed by the compiler, can be reflected in the program stack space using the example of parameter passing, return values, and automatic variable is the use of space.
3.C program heap space usage
In the C language program, the heap memory allocation and release is done by calling the library function, and their use need to include standard library files:
# include
There are four functions to achieve the heap memory allocation and release:
void * malloc (size_t size) to allocate memory space
void free (void * ptr) free memory space
void * calloc (size_t nmemb, size_t size) to allocate memory space
void * realloc (void * ptr, size_t size) to re-allocate memory space
4. Heap memory and stack memory usage comparison
The swap of the most classic examples:
void swap (int a, int b)
(
int tmp;
tmp = b;
b = a;
a = tmp;
return;
)
This swap can not be achieved, because, swap function is called when you create a new stack space, when the stack upon return, the stack area has been released. Therefore, we must use the pointer.
I seriously appreciate the two examples:
typedef struct _S100
(
char string [100];
) S100;
void fun_para1 (S100 a)
(
printf ( "From para:% s \ n", a.string);
strcpy (a.string ,"-----");
printf ( "fun_para1 change:% s \ n", a.string);
return;
)
void test_fun_para1 (void)
(
S100 a;
strcpy (a.string, "+++++")
fun_para1 (a);
printf ( "after fun_para1 return:% s \ n", a.string);
return;
)
Running Results:
From para :+++++
fun_para1 change: -----
after fun_para1 return :+++++
void fun_para3 (char a [100])
(
strcpy (a ,"-----");
printf ( "fun_para1 change:% s \ n", a);
retrun;
)
void test_fun_para3 (void)
(
char a [100];
strcpy (a ,"+++++");
fun_para3 (a);
printf ( "after fun_para3 return:% s \ n", a);
return;
)
Running Results:
from para :+++++
fun_para1 change :-----
after fun_para3 return :-----
From the run results, such a conclusion can be drawn:
1. The use of program memory area
Storage area is divided into: static and dynamic
Static: read-only data area (RO DATA), has been initialized to read and write data area (RW DATA), an uninitialized read-write data area (BSS:Block Started by Symbol)
dynamic: heap Memory (heap) and stack memory (stack)
Heap memory is from low address to high address allocation, stack memory is from high address to low address allocation
2.C program stack space usage
In the C language program, the stack space is managed by the compiler, can be reflected in the program stack space using the example of parameter passing, return values, and automatic variable is the use of space.
3.C program heap space usage
In the C language program, the heap memory allocation and release is done by calling the library function, and their use need to include standard library files:
# include
There are four functions to achieve the heap memory allocation and release:
void * malloc (size_t size) to allocate memory space
void free (void * ptr) free memory space
void * calloc (size_t nmemb, size_t size) to allocate memory space
void * realloc (void * ptr, size_t size) to re-allocate memory space
4. Heap memory and stack memory usage comparison
The swap of the most classic examples:
void swap (int a, int b)
(
int tmp;
tmp = b;
b = a;
a = tmp;
return;
)
This swap can not be achieved, because, swap function is called when you create a new stack space, when the stack upon return, the stack area has been released. Therefore, we must use the pointer.
I seriously appreciate the two examples:
typedef struct _S100
(
char string [100];
) S100;
void fun_para1 (S100 a)
(
printf ( "From para:% s \ n", a.string);
strcpy (a.string ,"-----");
printf ( "fun_para1 change:% s \ n", a.string);
return;
)
void test_fun_para1 (void)
(
S100 a;
strcpy (a.string, "+++++")
fun_para1 (a);
printf ( "after fun_para1 return:% s \ n", a.string);
return;
)
Running Results:
From para :+++++
fun_para1 change: -----
after fun_para1 return :+++++
void fun_para3 (char a [100])
(
strcpy (a ,"-----");
printf ( "fun_para1 change:% s \ n", a);
retrun;
)
void test_fun_para3 (void)
(
char a [100];
strcpy (a ,"+++++");
fun_para3 (a);
printf ( "after fun_para3 return:% s \ n", a);
return;
)
Running Results:
from para :+++++
fun_para1 change :-----
after fun_para3 return :-----
From the run results, such a conclusion can be drawn:
when structure being used as parameters in function, the entire structure has been pushed onto the stack memory, just like a variable.
when array being used as a parameter in function, it will be handled as a pointer
when array being used as a parameter in function, it will be handled as a pointer
沒有留言:
張貼留言