Look at the way the definition of an array of static allocation and dynamic allocation:
Statically allocated array definitions:
const int N=100;
typedef struct{
int data[N];
int length;
}sqlist;//顺序表类型
Dynamically allocated array definitions:
typedef struct{
int *data;
int length;
}sqlist;//顺序表类型
M address space assigned byte length of a sequence table (dynamic):
sqlist L;
L.data=(ElemType*)malloc(sizeof(ElemType)*n);
//简单的写:
L.data=(int*)malloc(sizeof(int)*n);
. 1, the malloc (m) function : called dynamic memory allocation. In C language
It opens m represents a length byte address space, and returns the first address of this space, m is an integer.
(int *) represents a cast, because the malloc function returns a pointer viod * type, pointer type is undetermined, the sizeof (int) is a function malloc argument specifies the size of an int type memory; n-presentation element the number , size of each element is occupied bytes (int), int example, 8 bytes, if the elements 20, accounted for 120 bytes, the foregoing (int *) is expressed behind you want 120 the contents of bytes into what type, and then we get the base address L.data
2, the sizeof (x) calculation : calculating the length of the variable x.
. 3, Free (P) function : releases memory pointer variable, i.e. completely remove a variable. One using malloc. p must be a pointer variable.
Using the function to be loaded <stdlib.h> Standard Libraries
4, new and malloc :
Dynamic memory allocation in C ++:
To create a space to store, use new to dynamically allocate space, delete to free memory space, both with use.
For example, the definition:
指针变量=new 数据类型
int *p=new int;
int *p=new int[MAXSIZE];