Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to understand the parameters of C language functions: pointers

2025-03-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces "how to understand C language function parameter: pointer pointer". In daily operation, I believe many people have doubts about how to understand C language function parameter: pointer pointer problem. The editor consulted all kinds of data and sorted out a simple and easy-to-use operation method. I hope it will be helpful for you to answer the doubt of "how to understand C language function parameter: pointer pointer"! Next, please follow the editor to study!

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 intention of the code is that the do_work () function requests size bytes of space from the system heap space and then returns the pData pointer in the main function. However, an error was reported during execution: Segmentation fault (core dumped).

Analyze the reasons

We can think of a pointer of type char* as a remote control. If we 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.

Execute char * pData = 0

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

Execute do_work (pData, 128)

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

Execute p = (char *) malloc (size + 1)

The purpose 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 piece of space in memory, which is equivalent to a p, the remote control is bound to a device, and the device can be controlled, as shown below:

So far you have seen the cause of the program crash: although the pointer p is assigned, the content in the argument pData is always empty, so after returning from the do_malloc function, pData is still a null pointer, so it crashes. 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 that space to pData. In the do_malloc function, the first address of the allocated space is returned after a successful call to the system function malloc. The key is to send the first address to the pData pointer, that is, to make the value in the pData pointer variable equal to the first address of the heap space.

So how to do this through a function in the middle, as shown in 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;}

Execute char * pData = 0

This sentence has not changed.

Execute do_malloc (& pData, 128)

Pass the address of the pData pointer as an argument, because pData itself is a pointer, plus the address character &, is the pointer of the pointer (secondary pointer), so the first parameter of the do_malloc function is defined as a char** type, as shown in the figure:

P is a second-level pointer at this time. After the parameter is assigned, the content of p becomes the address of the pointer variable pData, which means that p points to the variable pData.

Execute * p = (char *) malloc (size + 1)

This sentence starts with figuring out what * p means. As I just said, p is a pointer that points to the variable pData. Then adding the value operator * before p is equivalent to taking out the value in the pointer p, and the value in it is pData! Therefore, the heap space header address returned by the malloc function is equivalent to an assignment to pData, as shown in the following figure:

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

At this point, the study on "how to understand C language function parameters: pointer pointer" is over. I hope to be able to solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report