从函数返回的堆分配

发布于 2024-12-10 04:02:43 字数 279 浏览 1 评论 0原文

The function has a problem: 

void myAllo(int mySize, char *myChar )
{
    myChar = new char[mySize];
}

堆分配会从 myAllo() 返回吗? 我不这么认为,因为 myChar 指向的内存会 当 myAllo 返回时被释放。

正确的 ?

欢迎任何评论。

谢谢

The function has a problem: 

void myAllo(int mySize, char *myChar )
{
    myChar = new char[mySize];
}

Will the heap allocation be returned from myAllo() ?
I do not think so, because memory pointed by myChar will
be deallocated when myAllo returns.

Right ?

Any comments are welcome.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

夜夜流光相皎洁 2024-12-17 04:02:43

<块引用>

堆分配会从 myAllo() 返回吗?

不会。参数 myChar 是按值传递给 myAllo() 的,因此您只需修改指针的本地副本,而不是修改指针中的指针值。调用者的上下文。

<块引用>

当 myAllo 返回时,myChar 指向的内存将被释放。

又不行了。函数退出时会泄漏分配的内存。要取消分配内存,您需要对 new[] 返回的指针调用 delete[],但当函数退出时,该指针将超出范围。


有多种方法可以解决您的问题。

让调用者传入指向已分配内存的指针地址,现在您可以修改调用者指针指向的内容。

void myAllo(int mySize, char **myChar )
{
    *myChar = new char[mySize];
}

类似地,您的函数可以接受对指针的可变引用

void myAllo(int mySize, char *& myChar )
{
    myChar = new char[mySize];
}

返回指向已分配内存的指针

char *myAllo( int mySize )
{
    return new char[mySize];
}

比上述所有解决方案更好,不是返回原始指针,而是返回智能指针

std::unique_ptr<char[]> myAllo( int mySize )
{
    return std::unique_ptr<char[]>( new char[mySize] );
}

Will the heap allocation be returned from myAllo() ?

No. The argument myChar is being passed by value to myAllo(), so you're simply modifying a local copy of the pointer, not the value of the pointer in the caller's context.

memory pointed by myChar will be deallocated when myAllo returns.

No again. The function leaks the allocated memory when it exits. To de-allocate memory you need to call delete[] on the pointer returned by new[] but this pointer goes out of scope when the function exits.


There are several ways to fix your problem.

Have the caller pass in the address of the pointer which will point to the allocated memory, now you can modify what the caller's pointer points to.

void myAllo(int mySize, char **myChar )
{
    *myChar = new char[mySize];
}

Similarly, your function could accept a mutable reference to the pointer

void myAllo(int mySize, char *& myChar )
{
    myChar = new char[mySize];
}

Return a pointer to the allocated memory

char *myAllo( int mySize )
{
    return new char[mySize];
}

Better than all of the above solutions, instead of returning raw pointers, return a smart pointer

std::unique_ptr<char[]> myAllo( int mySize )
{
    return std::unique_ptr<char[]>( new char[mySize] );
}
风筝在阴天搁浅。 2024-12-17 04:02:43

堆分配会从 myAllo() 返回吗?

不。

当 myAllo 返回时,myChar 指向的内存将被释放。

不,这是内存泄漏。

Will the heap allocation be returned from myAllo() ?

No.

memory pointed by myChar will be deallocated when myAllo returns.

No, this is a memory leak.

久伴你 2024-12-17 04:02:43

为了获得分配的内存,您需要将函数编写为

void myAllo(int mySize, char **myChar )
{
    *myChar = new char[mySize];
}

并调用它

char* pointer;
myAllo(size, &pointer);

,或者编写为

char* myAllo(int mySize )
{
    return new char[mySize];
}

并调用它

char* pointer = myAllo(size);

In order to get the memory you allocated you need to write the function as

void myAllo(int mySize, char **myChar )
{
    *myChar = new char[mySize];
}

and call it

char* pointer;
myAllo(size, &pointer);

or as

char* myAllo(int mySize )
{
    return new char[mySize];
}

and call it

char* pointer = myAllo(size);
樱娆 2024-12-17 04:02:43
void myAllo(int mySize, char *myChar ) 
{   //note: myChar is a COPY of someone else's pointer 
    myChar = new char[mySize];
    //nobody outside of the function will ever see this change
}

动态内存(新)仅在您显式释放时才会释放。
myChar 超出范围并被自动清理,但指针的“清理”不会释放它指向的任何内存。这允许我有多个指针指向相同的数据。

void myAllo(int mySize, char *&myChar )
{  //note: myChar is a REFERENCE of someone else's pointer 
    myChar = new char[mySize];
}

或者更好的是,使用向量:

std::vector<char> myAllo(int mySize)
{
    return std::vector<char>(mySize);
}
void myAllo(int mySize, char *myChar ) 
{   //note: myChar is a COPY of someone else's pointer 
    myChar = new char[mySize];
    //nobody outside of the function will ever see this change
}

Dynamic memory (new) is only released when you explicitly release it.
myChar goes out of scope and is automatically cleaned up, but a pointer's "clean up" does not deallocate any memory it points at. This allows me to have multiple pointers point at the same data.

void myAllo(int mySize, char *&myChar )
{  //note: myChar is a REFERENCE of someone else's pointer 
    myChar = new char[mySize];
}

Or better yet, use a vector:

std::vector<char> myAllo(int mySize)
{
    return std::vector<char>(mySize);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文