malloc的内存的指针的指针 [英] malloc memory to a pointer to pointer

查看:115
本文介绍了malloc的内存的指针的指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我也碰到这个问题,使用指针时,指针为char传来:

 无效setmemory(字符** P,INT NUM)
{
    * P =(字符*)malloc的(NUM);
}    无效测试(无效)
    {
        字符*海峡= NULL;
        setmemory(安培;海峡,100);
        的strcpy(STR,你好);
        的printf(STR);
    }诠释的main()
{
    测试();
    返回0;
}

在code以上是正确的,但为什么使用指针的指针的char ** P在这里我无法弄清楚?为什么只使用一个指针为char呢?所以我改变这个片段到下面,发现它不工作,谁能告诉我为什么吗?谢谢!

 无效setmemory(字符* P,INT NUM)
{
    P =(字符*)malloc的(NUM);
}无效测试(无效)
{
    字符*海峡= NULL;
    setmemory(STR,100);
    的strcpy(STR,你好);
    的printf(STR);
}诠释的main()
{
    测试();
    返回0;
}


解决方案

这里有一点要注意的是 - 当我们说指针,我们一般倾向于认为在按引用传递方面,但也不一定。即使指针可按值传递

的char * str中是本地的测试的char * P 是本地的 setmemory 。所以,你在做的修改 setmemory 将不会在可见测试如果你不发送指针的指针。

您可以把它像这样的单一指针

工作

 的char * setmemory(字符* P,INT NUM)// p是一个新的指针,但分在同一
                                    //位置STR
{
    P =(字符*)malloc的(NUM); //现在,'P'开始在不同的位置比STR指点
    的strcpy(P,你好); //一些数据复制到locnP是指向
    回磷; //哎呀。可怜的`str`仍然是指向以NULL :(
              //发送他的新分配区的地址
}无效测试(无效)
{
    字符*海峡= NULL;
    海峡= setmemory(STR,100); //我们传递它指向NULL指针    的printf(STR); //现在海峡指向alloced记忆和快乐:)
}诠释的main()
{
    测试();
    返回0;
}

请注意,在 setmemory 我们正在返回本地指针,但它不是一个问题上堆,而不是(无悬摆指针问题),因为该指针指向的位置堆栈

I have came across this problem when using pointer to pointer to a char:

void setmemory(char** p, int num)
{
    *p=(char*)malloc(num);
}

    void test(void)
    {
        char* str=NULL;
        setmemory(&str,100);
        strcpy(str,"hello");
        printf(str);
    }

int main()
{
    test();
    return 0;
}

The code above is correct,but I can't figure it out why using a pointer to a pointer char** p here? Why just using a pointer to a char instead? so I change this snippet into below and found it not working ,can anyone tell me why? thanks!

void setmemory(char* p, int num)
{
    p=(char*)malloc(num);
}

void test(void)
{
    char* str=NULL;
    setmemory(str,100);
    strcpy(str,"hello");
    printf(str);
}

int main()
{
    test();
    return 0;
}

解决方案

One thing to note here is - When we say pointers, we generally tend to think in terms of pass by reference but not necessarily. Even pointers can be passed by value

char* str is local to test and char* p is local to setmemory . So the changes you do in setmemory will not be visible in test if you dont send a pointer to a pointer.

You can make it work with a single pointer like this

 char * setmemory(char* p, int num) // p is a new pointer but points at the same
                                    // location as str
{
    p=(char*)malloc(num); // Now, 'p' starts pointing at a different location than 'str'
    strcpy(p ,"hello");  // Copy some data to the locn 'p' is pointing to
    return p; // Oops. The poor `str` is still pointing at NULL :( 
              // Send him the address of the newly allocated area
}

void test(void)
{
    char* str=NULL;
    str=setmemory(str,100); // We are passing a pointer which is pointing to NULL

    printf(str); //Now str points to the alloced memory and is happy :)
}

int main()
{
    test();
    return 0;
}

Note that in setmemory we are returning a local pointer, but it is not a problem ( no dangling pointer problems ) as this pointer points to a location on heap and not on stack

这篇关于malloc的内存的指针的指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆