ITeye博客

c++ 动态内存分配

aircoder · 2013-08-28 22:35 · c语言 · c++ · 编程语言 · Markdown

先看一段代码:

view plaincopy

#include<iostream>  
using namespace std;  
void GetMe(char *p,int num)  
{  
    p=(char*)malloc(sizeof(char)*num);  
      
}  
int main()  
{  
    char *str=NULL;  
    GetMe(str,100);  
    strcpy(str,"Hello!");  
    cout<<str<<endl;  
    return 0;  
}  

该代码运行崩溃,原因是str并没有实际获得指针p开辟的内存空间。在函数GetMe中,p实际上是主函数中str的一个副本,p申请了内存,只是把p指向的内存地址改变,而str并没有改变,所以str依然没有获得内存,在执行字符串复制时就会出错。而且每次p申请的内存都不会得到释放,最终会造成内存泄露。

我们可以更改以上代码如下:

view plaincopy

#include<iostream>  
using namespace std;  
char* GetMe(char *p,int num)  
{  
    p=(char*)malloc(sizeof(char)*num);  
    return p;  
}  
int main()  
{  
    char *str=NULL;  
    str=GetMe(str,100);  
    strcpy(str,"Hello!");  
    cout<<str<<endl;  

view plaincopy

<span style="white-space:pre">  </span>delete str;  
    return 0;  
}  

在该代码中我们为p申请了空间,并将该空间的地址作为返回值传给str,这样str就指向了p申请的内存空间。还有一种修改方法是:传递str的指针:

view plaincopy

#include<iostream>  
using namespace std;  
void GetMe(char **p,int num)  
{  
    *p=(char*)malloc(sizeof(char)*num);  
      
}  
int main()  
{  
    char *str=NULL;  
    GetMe(&str,100);  
    strcpy(str,"Hello!");  
    cout<<str<<endl;  
    delete str;  
    return 0;  
}  

在该代码中传递了str的指针给函数GetMe(),那么p就是str的地址的副本,地址的副本可定就是该地址了,所以该函数是为str地址指向的str开辟空间。所以能正确输出结果。

1 #include<stdio.h> 2 #include<stdlib.h> 3 4 union myunion{ 5 char charvar; 6 int intvar; 7 float floatvar; 8 }; 9 void swap(int ** p1, int **p2) 10 { 11 //会产生临时变量,所以使用二级指针 12 int *tmp; 13 14 tmp = *p1; 15 *p1 = *p2; 16 *p2 = tmp; 17 } 18 19 20 int main() 21 { 22 23 union myunion *uni = malloc(sizeof(float)); 24 25 int a= 10, c= 20; 26 int *aa = &a; 27 int *cc = &c; 28 fprintf(stdout,"aa is:%d\t cc is: %d\n", *aa, *cc); 29 swap(&aa,&cc); 30 printf("swap ..........."); 31 fprintf(stdout,"aa is:%d\t cc is: %d\n", *aa, *cc); 32 fprintf(stdout,"a is:%d\t b is: %d\n", a, c);// 不变,变的只是指针本身指向的地址而已 33 34 printf("---------------\n"); 35 36 free(uni); 37 38 return 0; 39 }

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。