mus*_*bar 0 c++ pointers memory-leaks
我的同事告诉我,void foo(int** p)它在C#中像out参数一样使用.有人可以准确解释如何?
我明白了,但是有些东西不见了.我知道如果我们将指针p本身传递给foo(*p)函数体p = new int(),我们可能会有一个悬空修改器!但是如何foo(**p)阻止这样的事情发生呢?
aJ.*_*aJ. 10
void foo(int** p)
{
*p = new int;
}
void main()
{
int *p = NULL;
foo(&p);
//unless you pass the pointer to p, the memory allocated in the
//function foo is not available here.
foo(p); // lets assume foo is foo(int* p)
//when you pass only pointer p to `foo()` then the value of the pointer will be
//passed ( pass by value) to foo() and hence, any modification to pointer p ( in
// the form of allocating memory also) will not be available after the
// control returns from foo()
// The p will still points to NULL here, not the memory allocated in foo().
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1104 次 |
| 最近记录: |