简体   繁体   English

它是通过指针?

[英]Is it passing by pointer?

void func(char* buf) { buf++;}

Should I call it passing by pointer or just passing by value(with the value being pointer type)? 我应该通过指针调用它还是只传递值(值为指针类型)? Would the original pointer passed in be altered in this case? 在这种情况下,传入的原始指针会被改变吗?

This is passing by value. 这是值得传递的。

void func( char * b )
{
    b = new char[4];
}

int main()
{
    char* buf = 0;
    func( buf );
    delete buf;
    return 0;
}

buf will still be 0 after the call to func and the newed memory will leak. 调用func后,buf仍为0,新内存将泄漏。

When you pass a pointer by value you can alter what the pointer points to not the pointer itself. 按值传递指针时,可以更改指针指向的内容而不是指针本身。

The right way to do the above stuff would be 做上述事情的正确方法是

ALTERNATIVE 1 替代方案1

void func( char *& b )
{
    b = new char[4];
}

int main()
{
    char* buf = 0;
    func( buf );
    delete buf;
    return 0;
}

Notice the pointer is passed by reference and not value. 请注意,指针是通过引用而不是值传递的。

ALTERNATIVE 2 替代方案2

Another alternative is to pass a pointer to a pointer like 另一种方法是将指针传递给指针

void func( char ** b )
{
    *b = new char[4];
}

int main()
{
    char* buf = 0;
    func( &buf );
    delete buf;
    return 0;
}

Please note I am not in any way advocating the use of naked pointers and manual memory management like above but merely illustrating passing pointer. 请注意我并不以任何方式提倡使用如上所述的裸指针和手动内存管理,而只是说明传递指针。 The C++ way would be to use a std::string or std::vector<char> instead. C ++的方式是使用std::stringstd::vector<char>

The pointer will not be altered. 指针不会改变。 Pass by pointer means pass an address. 传递指针意味着传递一个地址。 If you want the pointer altered, you have to pass a double pointer a deference it once. 如果你想改变指针,你必须传递一个双指针。


foo( char **b)
{
  *b = NULL;
}

The pointer itself is being passed by value (the memory being pointed at is being passed by pointer). 指针本身正在通过值传递(指向的内存正由指针传递)。 Changing the parameter inside the function will not affect the pointer that was passed in. 更改函数内的参数不会影响传入的指针。

To implement reference semantics via "passing a pointer", two things must happen: The caller must take the "address-of" the thing to be passed, and the callee must dereference a pointer. 要通过“传递指针”来实现引用语义,必须发生两件事:调用者必须接受要传递的“address-of”,并且被调用者必须取消引用指针。

Parts of this can be obscured by the use of arrays, which decay to a pointer to the first element - in that sense, the array content is always "passed by reference". 使用数组可以掩盖部分内容,数组会衰减到指向第一个元素的指针 - 在这个意义上,数组内容总是“通过引用传递”。 You can use an "array-of-one" to obscure the dereferencing, though. 但是,您可以使用“数组之一”来模糊解除引用。

This is the straight-forward apporoach: 这是直截了当的apporoach:

void increment_me(int * n) { ++*n; }    // note the dereference

int main() { int n; increment_me(&n); } // note the address-of

Here's the same in disguise: 这是伪装的相同:

void increment_me(int n[]) { ++n[0]; }     // no visible *  
int main() { int n[1]; increment_me(n); }  // or &

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM