The difference between c/c++ function parameters char*a and char*&a

The author focuses on the field of Android security. Welcome to pay attention to my personal WeChat public account "Android Security Engineering" (click to scan the code to follow). The personal WeChat public account mainly focuses on the security protection and reverse analysis of Android applications, sharing various security attack and defense methods, Hook technology, ARM compilation and other Android-related knowledge.

char* a is a pointer, pointing to a char type variable or a piece of char type memory space. In the parameter list of the function, char* a indicates that a pointer to char type is passed to the function as a parameter.

char*& a is a pointer reference to a pointer of type char. In the parameter list of the function, char*& a means to pass a reference to a char type pointer as a parameter to the function.

When using char* a as a function parameter, the function can modify the content of the memory space pointed to by a, but cannot modify the address pointed to by a; while using char*& a as a function parameter, the function can modify the address pointed to by a, thereby changing The location and content of the memory space pointed to by a.

Here is an example using char* a and char*& a:

void foo(char* a)
{
    
    
    // 修改 a 所指向的内存空间的内容
    *a = 'A';
}

void bar(char*& a)
{
    
    
    // 修改 a 指向的地址
    a = new char[2];
    // 修改 a 所指向的内存空间的内容
    *a = 'B';
}

int main()
{
    
    
    char c = 'a';
    char* ptr = &c;
    // 使用 char* a 调用函数 foo
    foo(ptr);
    // c 的值被修改为 'A'
    std::cout << c << std::endl;

    char* ptr2 = &c;
    // 使用 char*& a 调用函数 bar
    bar(ptr2);
    // ptr2 指向新的内存空间,其中第一个元素的值为 'B'
    std::cout << *ptr2 << std::endl;

    // 必须释放动态分配的内存
    delete[] ptr2;
    return 0;
}

char* a and char*& a are used differently:

  1. When the function needs to modify the content of the memory space pointed to by the passed pointer, but does not need to modify the pointer itself, char* a should be used.

  2. When the function needs to modify the content of the memory space pointed to by the passed pointer and needs to use the new memory space pointed to by the pointer outside the function, char*& a can be used. In this case, the new operator should be used inside the function to allocate new memory space, and the caller is responsible for freeing this memory space when appropriate.

  3. When the function needs to modify the address pointed to by the passed pointer, char*& a must be used. In this case, the function may change the location of the memory space pointed to by the pointer, which means that the caller needs to pay attention to whether it needs to reallocate the memory space and release the old memory space.

Using char* a means that the function will not change the value of the pointer itself, but may modify the content of the memory space pointed to by the pointer. And using char*& a indicates that the function may change the value of the pointer itself, and may allocate new memory space inside the function to store the modified content.

Why can't the address pointed to by char* a be modified after being passed in?
When passing char* a, what is actually passed is the address pointing to a memory space. The function can access and modify the content of this memory space through this address, but cannot modify the address itself.

This is because parameters are passed by value when the function is called. That is, what is passed to the function is a copy of the pointer, not the pointer itself. When the function modifies the address pointed to by char* a, it actually just modifies the copy of the parameter without changing the value of the original pointer. Therefore, modifying the address pointed to by char* a has no effect on the original pointer.

If you need to modify the value of the pointer itself, you can use char*& a. At this time, what is passed is a reference to a pointer, and the function can modify the value of the pointer itself through the reference, thereby changing the address pointed to by the pointer.

Guess you like

Origin blog.csdn.net/HongHua_bai/article/details/130278707