Today, I am free to implement the sequence table in the data structure, and encountered such a problem during the initial creation of the table.
#include<stdlib.h>#include<stdio.h>#define MaxSize 1000#define ElemType inttypedef struct {ElemType data[MaxSize];int length; }SqList;int isListEmpty(SqList *L);int *p;void CreateList(SqList *LTMP,ElemType a[],int n){printf("%p ",LTMP);LTMP=(SqList *)malloc(sizeof(SqList));printf("%p ",LTMP);p=(int *)LTMP;printf("%p\n",p); for(int i=0;i<n;i++)(*LTMP).data[i]=a[i];(*LTMP).length=n;}void dispList(SqList *L){if(isListEmpty(L))return;for(int i=0;i<L->length;i++){printf("%d ",L->data[i]);} printf("\n");}int isListEmpty(SqList *L){return(L->length==0);}int main(){int a[3]={1,2,3};SqList * L=NULL;printf("%p ",L);CreateList(L,a,3);printf("%d %d %d \n",*p,*(p+1),*(p+2));printf("%p\n",L); //dispList(L);}
If you do not add the & (reference character) to L in createlist (sqlist * l, elemtype A [], int N), The displist function reports an error, the reason is that l in the createlist function passes the form parameter, and the L pointer of the createlist function is a temporary pointer, which is set to ltmp. Even if it is a pointer without a quote, it is passed as a form parameter, and L is two pointers.
The first address indicates the address of the real parameter L. Because l is null, the address is 0. The second form parameter passed to the createlist function with the address L. ltmp points to null and 0.
The third address is allocated a new memory space for the ltmp, pointing to a dynamic memory opened by malloc. The first address is 000... 00337000. The fourth address is the address where the global variable pointer P points to ltmp, which is also 000... 00337000. However, when the function stops running, the call function stack is empty, and the temporary variables are released. The memory is not released.
* P, * (p + 1), * (p + 2) output, 3, that is, the value assigned in createlist. The memory has never been released, the data stored in it has not changed. If there is no memory, it will only leak memory.
The address of L is output later. Because it is a value transfer, L is not changed, and it is still 0.
Let's take a look at it with reference transfer.
#include<stdlib.h>#include<stdio.h>#define MaxSize 1000#define ElemType inttypedef struct {ElemType data[MaxSize];int length; }SqList;int isListEmpty(SqList *L);int *p;void CreateList(SqList *&L,ElemType a[],int n){printf("%p ",L);L=(SqList *)malloc(sizeof(SqList));printf("%p ",L);p=(int *)L;printf("%p\n",p); for(int i=0;i<n;i++)L->data[i]=a[i];L->length=n;}void dispList(SqList *L){if(isListEmpty(L))return;for(int i=0;i<L->length;i++){printf("%d ",L->data[i]);} printf("\n");}int isListEmpty(SqList *L){return(L->length==0);}int main(){int a[3]={1,2,3};SqList * L=NULL;printf("%p ",L);CreateList(L,a,3);printf("%d %d %d \n",*p,*(p+1),*(p+2));printf("%p\n",L); dispList(L);}
The first and second addresses are all l addresses, which are unchanged, so they are still 0 addresses. Later, the malloc function is used to pass the first address of the dynamically allocated memory, and L is changed due to the passing of the real parameter l, so the three or five addresses are the same as the newly allocated address. The displist function can run normally.
Function parameters and real parameters