C language function passing parameters: pointer to pointer

Preface

Today a colleague asked a question: Passing pointers in function parameters is a very common scenario, re-organize and record it here, if you have similar questions in the future, just post this small summary.

Code: Version 1

void do_malloc(char *p, int size)
{
    p = (char *)malloc(size + 1);
    memset(p, 0, size + 1);
}


int main(int argc, char *argv[])
{
    char *pData = 0;
    do_malloc(pData, 128);
    sprintf(pData, "%s", "abc");
    printf(pData);
    return 0;
}

The original intention of the code is: The do_work() function applies for size bytes of the system heap space, and then returns the pData pointer in the main function .
However, when the execution of Segmentation fault (core dumped)error: .

Analyze the reasons

We can regard a pointer of char* type as a remote control . If you assign a value to this pointer, it is equivalent to binding the remote control to a device , which can be controlled by the remote control.

carried outchar *pData = 0;

The content of pData is empty, which is equivalent to that the remote control is not bound to any device , as shown in the figure below:

carried outdo_work(pData, 128);

The parameter passed here is pData itself, so void do_work(char *p, int size)after entering the function, the content of the actual parameter pData is assigned to the formal parameter p, so the content of the pointer p is also empty , that is, the remote control p is not bound to any device. As shown below:

carried outp = (char *)malloc(size + 1);

The function of this sentence is to assign the first address of the applied heap space to p. That is to say: now p points to a space in the memory, which is equivalent to binding the remote controller of p to a device, and you can control this device , as shown in the following figure:

So far we have seen the cause of the program crash: although the pointer p is assigned, the content of the actual parameter pData is always empty , so after returning from the do_malloc function, pData is still a null pointer, so it crashed. Of course, the heap space pointed to by p is also leaked.

Code: Version 2

The original intention of the code is to apply for heap space in the do_malloc function, and then assign the first address of this space to pData. In the do_malloc function, calling the system function malloc successfully returns the first address of the allocated space. The key is to send this first address to the pData pointer, which means that the value in the pData pointer variable is equal to the first address of the heap space.

So how to accomplish this function through a function in the middle, the following code:

void do_malloc(char **p, int size)
{
    *p = (char *)malloc(size + 1);
    memset(*p, 0, size + 1);
}


int main(int argc, char *argv[])
{
    char *pData = 0;
    do_malloc(&pData, 128);
    sprintf(pData, "%s", "abc");
    printf(pData);
    return 0;
}

carried outchar *pData = 0;

There is no change in this sentence.

carried outdo_malloc(&pData, 128);

The address of the pData pointer is passed as the actual parameter, because pData itself is a pointer, plus the address character &, it is the pointer of the pointer (second-level pointer), so the first parameter of the do_malloc function must be defined as char** Type, as shown in the figure at this time:

p is a secondary pointer at this time. After the parameter is assigned , the content in p becomes the address of the pointer variable pData , that is to say, p points to the variable pData.

carried out*p = (char *)malloc(size + 1);

First understand *pwhat this sentence means. As I said earlier, p is a pointer, which points to the variable pData. Then adding the value operator * in front of p is equivalent to taking out the value in pointer p, and the value in it is pData!
Therefore, the first address of the heap space returned by the malloc function is equivalent to being assigned to pData, as shown in the following figure:

At this point, the remote control pData is bound to the allocated heap space, and then there is no problem in operating pData.


[Original Statement]

> OF: Columbia Road (public No.: The IOT of IOT town )
> know almost: Columbia Road
> B station: Columbia Road Share
> Nuggets: Columbia Road Share
> CSDN: Columbia Road Share

If you think the article is good, please forward it and share it with your friends.


I will summarize and share the actual combat experience of projects in embedded development for more than ten years , I believe you will not be disappointed!

Press and hold the QR code in the picture to follow, each article has dry goods.


Reprint: Welcome to reprint, but without the consent of the author, this statement must be retained, and the original link must be given in the article.




Recommended reading

[1] The underlying debugging principle of gdb is so simple
[2] Double buffering technology in producer and consumer mode
[3] In- depth LUA scripting language, so that you can thoroughly understand the principle of debugging
[4] Step by step analysis-how to implement it in C Object-oriented programming
[5] those things about encryption and certificates

Guess you like

Origin blog.csdn.net/u012296253/article/details/112553964