#include<stdio.h> #include<stdlib.h> voidfunc1(int * str1){ int i; puts("starting malloc str1 with 10 * int ..."); str1 = (int*)malloc(sizeof(int)*10); printf("In func1, after malloc, the value of str1 is %d\n", str1 ); for ( i = 0 ; i < 10 ; i++ ) str1[i] = i; puts("In func1, the value of str1[] is"); for ( i = 0 ; i < 10 ; i++ ) printf("%d ",str1[i]); putchar('\n'); } intmain(){ int * str, i; puts("Before func1, the value of array str is"); for ( i = 0 ; i < 10 ; i++ ) printf("%d ", str[i] ); putchar('\n'); printf("In main function, str is %d\n", str ); func1(str); printf("After func1, str is %d\n", str ); puts("After func1, the value of array str is"); for ( i = 0 ; i < 10 ; i++ ) printf("%d ", str[i] ); putchar('\n'); return0; }
执行上面的代码,运行结果如下
1 2 3 4 5 6 7 8 9 10
Before func1, the value of array str is 10 -6499098613276400 -64990985732764 -64990984132764 In main function, str is -649916608 starting malloc str1 with 10 * int ... In func1, after malloc, the value of str1 is 30027792 In func1, the value of str1[] is 0123456789 After func1, str is -649916608 After func1, the value of array str is 10 -6499098613276400 -64990985732764 -64990984132764
voidfunc1(int ** str1){ /* 要为int *类型赋值,传入int** */ int i; puts("starting malloc str1 with 10 * int ..."); *str1 = (int*)malloc(sizeof(int)*10); printf("In func1, after malloc, the value of str1 is %d\n", *str1 ); for ( i = 0 ; i < 10 ; i++ ) (*str1)[i] = i; puts("In func1, the value of str1[] is"); for ( i = 0 ; i < 10 ; i++ ) printf("%d ",(*str1)[i]); putchar('\n'); } intmain(){ int * str, i; ...... func1(&str); /* 传入指针str所在的地址 */ ...... return0; }
上面代码和之前代码的不同之处在于,func1的接收参数变为了 int ** 类型,是一个指向int型指针的指针,因此向func1传入str的地址,就可以在func1里修改str的值。在func1中,要修改main中str的值,只需要对( * str1)进行操作。运行结果如下。
1 2 3 4 5 6 7 8 9 10
Before func1, the value of array str is 10 -12425027573276700 -124250275332767 -124250273732767 In main function, str is -1242510416 starting malloc str1 with 10 * int ... In func1, after malloc, the value of str1 is 28885008 In func1, the value of str1[] is 0123456789 After func1, str is 28885008 After func1, the value of array str is 0123456789
可行做法2
1 2 3 4 5 6 7 8 9 10 11
int * func1( int * str ){ ...... returnstr; } int main(){ int * str, i; ...... str = func1( str ); ...... return0; }
#include<stdio.h> #include<stdlib.h> voidfunc2(int ** str2){ str2 = (int**)malloc(sizeof(int*)*10); printf("In func2, the value of str2 is %d\n",str2); int i, j; for ( i = 0; i < 10; i++){ str2[i] = (int *)malloc(sizeof(int)*10); printf(" The value of str2[%d] is %d\n", i, str2[i]); printf("And the value of the array is "); for ( j = 0 ; j < 10 ; j++ ){ str2[i][j] = i * 10 + j; printf("%d ", str2[i][j]); } putchar('\n'); } } intmain(){ int ** str2; /* str2 is stored in stack, which was not initialized */ int i, j ; printf("Before run func2, the str2 is %d\n", str2); func2(str2); printf("After run func2, return to main, the str2 is %d\n", str2); for ( i = 0 ; i < 10 ; i++ ) printf("str2[i] is %d\n", str2[i]); puts("The value of total str2 is :"); for ( i = 0 ; i < 10 ; i++ ){ for ( j = 0 ; j < 10 ; j++ ) printf("%d ", str2[i][j]); putchar('\n'); } return0; }
Before run func2, the str2 is -1427732048 In func2, the value of str2 is 10940432 The value of str2[0] is 10940528 And the value of the array is 0123456789 The value of str2[1] is 10940576 And the value of the array is 10111213141516171819 The value of str2[2] is 10940624 And the value of the array is 20212223242526272829 The value of str2[3] is 10940672 And the value of the array is 30313233343536373839 The value of str2[4] is 10940720 And the value of the array is 40414243444546474849 The value of str2[5] is 10940768 And the value of the array is 50515253545556575859 The value of str2[6] is 10940816 And the value of the array is 60616263646566676869 The value of str2[7] is 10940864 And the value of the array is 70717273747576777879 The value of str2[8] is 10940912 And the value of the array is 80818283848586878889 The value of str2[9] is 10940960 And the value of the array is 90919293949596979899 After run func2, return to main, the str2 is -1427732048 str2[i] is 1 str2[i] is -1427727973 str2[i] is 0 str2[i] is -1427727969 str2[i] is -1427727953 str2[i] is -1427727942 str2[i] is -1427727925 str2[i] is -1427727831 str2[i] is -1427727796 str2[i] is -1427727780 The value of total str2 is : 段错误 (核心已转储)
voidfunc2(int *** str2){ *str2 = (int**)malloc(sizeof(int*)*10); printf("In func2, the value of str2 is %d\n",*str2); int i, j; for ( i = 0; i < 10; i++){ (*str2)[i] = (int *)malloc(sizeof(int)*10); printf(" The value of str2[%d] is %d\n", i, (*str2)[i]); printf("And the value of the array is "); for ( j = 0 ; j < 10 ; j++ ){ (*str2)[i][j] = i * 10 + j; printf("%d ", (*str2)[i][j]); } putchar('\n'); } }
intmain(){ int ** str2, i, j; ...... func2(&str2); ...... return0; }
正确做法2
1 2 3 4
int ** func2(int ** str2){ ...... return str2; }
野指针
对malloc分配的堆空间,free对应的首地址后一定要将指针变量赋为NULL。如p = ( int * )malloc(sizeof(int) * 10),若free(p),则一定要 p = NULL,否则p指向的内存空间已经释放,而p指向了垃圾内存,成为野指针。再次调用p会引发不允许的内存访问。