1、内存分配方式有三种
(1)从静态存储区域分配。内存在程序编译的时候就已经分配好,这块内存在程序的整个运行期间都存在。例如全局变量,static变量。
(2)在栈上创建。在执行函数时,函数内局部变量的存储单元都可以在栈上创建,函数执行结束时这些存储单元自动被释放。栈内存分配运算内置于处理器的指 令集中,效率很高,但是分配的内存容量有限。
2、使用指针做函数返回值
错误1:使用栈内存返回指针。因为栈内存将在调用结束后自动释放,从而主函数使用该地址空间将很危险。例如:指针pcStr将指向一个未知的内存内容。
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
static char *GetWord()
{
char pcWord[32] = "hello word!";
return pcWord;
}
int main(int argc, char *argv[])
{
char *pcStr = NULL;
pcStr = GetWord();//出错! 得到一块已释放的内存
printf("pcStr = %s\n", pcStr);
return 0;
}
正确的使用方法是使用堆内存返回指针,但是要注意防止内存泄露,该内存使用完毕后要记得释放
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
static char *GetWord()
{
char Word[32] = "hello word!";
char *pcWord = NULL;
pcWord = malloc(32 * sizeof(char));
if(NULL == pcWord)
{
printf("pcWord malloc memeory failed.\n");
return NULL;
}
strcpy(pcWord, Word);
return pcWord;
}
int main(int argc, char *argv[])
{
char *pcStr = NULL;
pcStr = GetWord();
printf("pcStr = %s\n", pcStr);
free(pcStr);//记得释放内存 防止内存泄露
pcStr = NULL;//避免形成野指针
return 0;
}
3、使用指针作函数参数
在C语言中,形参只是继承了实参的值,是另外一个量(返回值也是同理,传递了一个地址值(指针)或实数值),形参的改变并不能引起实参的改变。错误1:直接使用形参分配内存,因为实参的值并不会改变,如下则实参一直为NULL
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
static void MallocWordMem(char *pcWord)
{
pcWord = malloc(32 * sizeof(char));
if(NULL == pcWord)
{
printf("pcWord malloc memeory failed.\n");
return ;
}
}
int main(int argc, char *argv[])
{
char *pcStr = NULL;
char cWord[32] = "hello word!";
MallocWordMem(pcStr);
strcpy(pcStr, cWord);
printf("pcStr = %s\n", pcStr);
free(pcStr);//记得释放内存 防止内存泄露
pcStr = NULL;//避免形成野指针
return 0;
}
程序运行提示段错误。
2、通过指针是可以传值的,因为此时该指针的地址是在主函数中申请的栈内存,我们通过指针对该栈内存进行操作,从而改变了实参的值。(这个就不举例说明了。)
3、可以采用指向指针的指针来进行在调用函数中申请内存
#include <stdio.h>#include <string.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
char c = 'A';
char *pc = &c;
char **ppc = &pc;
printf("c addr = %p, c = %c\n", &c, c);
printf("pc addr = %p, pc = %p, *pc = %c\n", &pc, pc, *pc);
printf("ppc addr = %p, ppc = %p, *ppc = %p, **ppc = %c\n", &ppc, ppc, *ppc, **ppc);
return 0;
}
执行结果如下:
xzg@byxc-PDSML:~$ ./1
c addr = 0xbfc19adf, c = A
pc addr = 0xbfc19ad4, pc = 0xbfc19adf, *pc = A
ppc addr = 0xbfc19ad8, ppc = 0xbfc19ad4, *ppc = 0xbfc19adf, **ppc = A
变量c的地址为0xbfc19adf,,内容时‘A’ ,
变量pc的地址是0xbfc19ad4,内容为变量c的地址0xbfc19adf,内容地址指向的数据是‘A’
变量ppc的地址是0xbfc19ad8,内容是变量pc的地址,内容地址指向的数据是变量c的地址,内容指向的地址的地址的内容是‘A’
#include <string.h>
#include <stdlib.h>
static void MallocWordMem(char **pcWord)
{
*pcWord = malloc(32 * sizeof(char));
if(NULL == pcWord)
{
printf("pcWord malloc memeory failed.\n");
return ;
}
}
int main(int argc, char *argv[])
{
char c = 'A';
char *pc = NULL;
char **ppc = &pc;
pc = &c;
printf("c addr = %p, c = %c\n", &c, c);
printf("pc addr = %p, pc = %p, *pc = %c\n", &pc, pc, *pc);
printf("ppc addr = %p, ppc = %p, *ppc = %p, **ppc = %c\n", &ppc, ppc, *ppc, **ppc);
MallocWordMem(ppc);
strcpy(pc, "abcd");
printf("pc = %s\n", pc);
return 0;
}
程序运行结果如下:
xzg@byxc-PDSML:~$ ./1
c addr = 0xbfac126f, c = A
pc addr = 0xbfac1264, pc = 0xbfac126f, *pc = A
ppc addr = 0xbfac1268, ppc = 0xbfac1264, *ppc = 0xbfac126f, **ppc = A
pc = abcd