【转】【c++】指针参数是如何传递内存的

参数策略

如果函数的参数是一个指针,不要指望用该指针去动态申请内存。如下:

void GetMemory(char *p, int num)
{
    p = (char *)malloc(sizeof(char) * num);
}
void Test(void)
{
    char *str = NULL;
    GetMemory(str, 100);     //str仍未NULL
    strcpy(str, "hello");    //运行错误
}

原因是编译器总是为每个参数制作临时副本。指针参数p, 其副本为_p,使_p=p。如果改变了_p所指的内容,相应的p所指的内容也跟着改变(毕竟指向同样的地方)。但是在GetMemory中动态分配内存空间,改变了_p的内容。在调用函数中的p还是指向NULL。再者,因为函数GetMemory中动态分配了空间,但是没释放,这样调用一次函数,就泄露了一次内存。图示:

如果非得用指针参数申请内存,可以用指针的指针作为参数申请内存

void GetMemory(char **p, int num)
{
    *p = (char *)malloc(sizeof(char) * num);
}
void Test(void)
{
    char *str = NULL;
    GetMemory(&str, 100);     //记得加地址符
    strcpy(str, "hello");
    free(str)
 }

原理是一样的,比较难理解,图示表示:

比较好的方法是传指针的引用

#include <iostream>
#include <string>
#include <cstring>
#include <cstdlib>
using namespace std;
void GetMemory(char *&p, int num)
{
    p = (char *)malloc(sizeof(char) * num);
}

void Test(void)
{
    char *str = NULL;
    GetMemory(str, 100);
    strcpy(str, "hello");
    cout << str << endl;
    free(str);
}
int main()
{
    Test();
}

这里注意指针的引用 为char* &a,要是不好理解可以这样:

typedef char* pchar;
pchar &a

返回值策略

可以用函数返回值来传递动态内存。这中方法比“指针的指针”简单多了

char *GetMemory(int num)
{
     char *p = (char *)malloc(sizeof(char) * num);
     return p;
}
void Test(void)
{
    char *str = NULL;
    str = GetMemory(100);  //str指向了动态分配的空间
    strcpy(str, "hello");
    free(str)
 }

在使用返回值时,千万别返回指向“栈内存”的指针、引用,因为该内存在函数结束时自动消亡了,返回的指针是个野指针了。例如

char *GetString()
{
     char p[] = "hello world"; //数组内容存储在栈区,函数结束时,会释放掉
     return p;
}
void Test(void)
{
    char *str = NULL;
    str = GetString();      //因为非配的内存早已释放掉,此时的str是个野指针,内容是垃圾
   cout << str << endl;
 }

在函数中不定义数组,定义指针,示例:

char *GetString()
{
     char *p = "hello world"; //数组内容存储在静态区,函数结束时,不会释放掉
     return p;
}
void Test(void)
{
    char *str = NULL;
    str = GetString();
    cout << str << endl;
 }

此时的程序是正确的,但是有一点,此时分配的内存处于静态区,是只可以读取但是不可以修改的。

原文路径:http://www.cnblogs.com/kaituorensheng/p/3246900.html

时间: 2024-08-06 20:04:36

【转】【c++】指针参数是如何传递内存的的相关文章

指针参数是如何传递内存的

1.1.5 指针参数是如何传递内存的? 如果函数的参数是一个指针,不要指望用该指针去申请动态内存.如下示例中,Test函数的语句GetMemory(str, 200)并没有使str获得期望的内存,str依旧是NULL,为什么? void GetMemory(char *p, int num) { p = (char *)malloc(sizeof(char) * num); } void Test(void) { char *str = NULL; GetMemory(str, 100); //

指针参数是如何传递内存的?

如果函数的参数是一个指针,不要指望用该指针去申请动态内存.示例7-4-1中,Test函数的语句GetMemory(str, 200)并没有使str获得期望的内存,str依旧是NULL,为什么? void GetMemory(char *p, int num){    p = (char *)malloc(sizeof(char) * num);}void Test(void){    char *str = NULL;    GetMemory(str, 100);    // str 仍然为 

指针参数是如何传递内存的(转载)

参数策略 如果函数的参数是一个指针,不要指望用该指针去动态申请内存.如下:   void GetMemory(char *p, int num) { p = (char *)malloc(sizeof(char) * num); } void Test(void) { char *str = NULL; GetMemory(str, 100); //str仍未NULL strcpy(str, "hello"); //运行错误 }   原因是编译器总是为每个参数制作临时副本.指针参数p,

二级指针和函数参数——指针参数是如何传递内存的?

1:如果函数的参数是一个指针,不要指望用该指针去申请动态内存.Test 函数的语句 GetMemory(str, 200)并没有使 str 获得期望的内存,str 依旧是 NULL,为什么? void GetMemory(char *p, int num) { p = (char *)malloc(sizeof(char) * num); } void Test(void) { char *str = NULL; GetMemory(str, 100); // str 仍然为 NULL strc

指针参数的传递(节选 C++/C 高质量编程 林锐)

指针参数是如何传递内存的 如果函数的参数是一个指针,不要指望用该指针去申请动态内存.示例7-4-1中,Test函数的语句GetMemory(str, 200)并没有使str获得期望的内存,str依旧是NULL,为什么? void GetMemory(char *p, int num) { p = (char *)malloc(sizeof(char) * num); } void Test(void) { char *str = NULL; GetMemory(str, 100);    //

指针参数--指针做参数时应该注意的地方

转自:http://blog.csdn.net/sszgg2006/article/details/9037675 首先看以下程序: [cpp] view plain copy #include <stdio.h> int *swap(int *px, int *py){ int temp; temp = *px; *px = *py; *py = temp; return px; } int main(void){ int i = 10, j = 20; int *p = swap(&

C语言中结构体参数变量的传递

c语言  结构体  函数  参数  传递 一.前言 本文中的程序实现对员工信息结构体字段赋值并打印出来的功能.该结构体的定义如下: // 员工信息结构体 typedef struct {        INT8       szEmployeeName[100];     // 员工姓名        UINT16  iEmployeeAge;                    // 员工年龄        UINT32  iEmployeeNo;                      

数组与指针——参数

引入: #include <stdio.h> #include <stdlib.h> void fun(char **q) { exit; } void main() { char **p,a[6][8]; p = a; fun(a); } 编译不能通过! ERROR:不能将"char (*)[8]"类型的值分配到"char **"类型的实体. ERROR:"char (*)[8]"类型和"char **&quo

函数与指针参数——关于两道题目的个人理解

题目1,下面的代码的输出结果是什么 1 #include<stdio.h> 2 3 void get(char *p) 4 { 5 p="hello!"; 6 } 7 8 int main(int argc, char const *argv[]) 9 { 10 char *str=NULL; 11 str = get(); 12 13 printf("%s\n",str); 14 15 return 0; 16 } 读者也可以自己思考一下答案是多少,当