One, dynamic storage allocation
In the array chapter, it was introduced that the length of several groups was predefined and fixed throughout the program. Dynamic array types are not allowed in C language.
For example:
int n;
scanf ("%d", &n);
int a[n];
It is wrong to represent the length with a variable, to animate the size of the array. However, in actual programming, this is often the case, that is, the required memory space depends on the actual input data, and can not be predetermined. For this kind of problem, the method of using array is difficult to solve. To address the above problems, C language provides some memory management functions that can dynamically allocate memory space as needed, or reclaim unused space for reuse, providing a means to effectively utilize memory resources.
The following three memory management functions are commonly used:
1. Allocating memory space functions malloc
Call form:
(type specifier *) malloc (size)
Function: Allocates a contiguous area in the dynamic storage area of memory with a length of "size" byte. The return value of the function is the first address of the range.
The type specifier indicates what data type is used for the zone.
(type specifier *) indicates that the return value is cast to the type pointer.
' Size ' is an unsigned number.
For example:
pc= (char *) malloc (100);
Represents a 100-byte allocated memory space and is cast to a character array type, and the return value of the function is a pointer to the character array, which is assigned to the pointer variable PC.
2. Allocating memory space functions Calloc
Calloc is also used to allocate memory space.
Call form:
(type specifier *) calloc (n,size)
Function: Allocates a contiguous region of N block Length "size" in the memory dynamic storage area. The return value of the function is the first address of the range.
(Type descriptor *) is used to force type conversions.
The difference between the Calloc function and the malloc function is only that you can allocate n chunks at once.
For example:
ps= (Struet stu*) calloc (2,sizeof (struct stu));
The sizeof (struct Stu) is the length of the STU structure. So the statement means: Allocate 2 contiguous areas by Stu length, cast to Stu type, and assign the first address to the pointer variable PS.
Release memory space Function free
Call form:
Free (VOID*PTR);
Function: Frees a block of memory that PTR points to, and PTR is a pointer variable of any type that points to the first address of the freed area. The freed zone should be an area allocated by the malloc or Calloc function.
"Example 1" allocates an area and enters a student data.
Main ()
{
struct STU
{
int num;
Char *name;
char sex;
Float score;
} *ps;
ps= (struct stu*) malloc (sizeof (struct stu));
ps->num=102;
Ps->name= "Zhang Ping";
ps->sex= ' M ';
ps->score=62.5;
printf ("number=%d name=%s", ps->num,ps->name);
printf ("Sex=%c score=%f", Ps->sex,ps->score);
Free (PS);
}
In this example, the structure Stu is defined and the STU type pointer variable PS is defined. It then allocates a stu large memory area and assigns the first address to PS so that the PS points to the area. Each member is assigned a value using PS as the pointer variable pointing to the structure, and the member values are output by printf. Finally, use the free function to release the memory space that the PS points to. The whole program includes three steps to apply for memory space, use memory space, free memory space, and realize dynamic allocation of storage space.
Free () Only frees heap memory, which is the malloc () created
Some time ago, look at someone's code and see
Free (q);
Q->LRU = *blk_head_lru.next;
So, it feels a little strange, why a pointer free, but also can assign value, and the result is correct.
After the baffled.
After Google, found free only to make a mark, tell the system this block of memory, once invoked, that address is not protected, that is, other variables may occupy that address at any time.
After the thought if free, then malloc a new variable then that value is overwritten?
Wrote a code, tried it.
#include <stdio.h>
#include <malloc.h>
int main ()
{
int *a = (int *) malloc (sizeof (int));
*a = 5;
Free (a);
printf ("A =%d\n", *a);
*a = 4;
printf ("A =%d\n", *a);
int *b;
b = (int *) malloc (sizeof (int));
printf ("A =%d\n", *a);
return 0;
}
VS2005 compiled through.
If you have free
The result is
A =-572662307
A = 4
A =-842150451
If you comment out free
A = 5
A = 4
A = 4
This is why.