getaddrinfo addrinfo 结果在堆栈或堆中

发布于 2024-10-26 11:33:41 字数 781 浏览 3 评论 0原文

至少我有点困惑。 getaddrinfo() 调用“更新”指向 addrinfo 结构的指针,当我要在同一范围(该函数)中使用 addrinfo 时一切都很好,但是如果我将该结构复制到另一个结构(通过分配它)会发生什么。

请帮助我了解正在进行的基础知识(不寻求替代方法的建议)。

如果我错了请纠正我: a) getaddrinfo() 需要一个指向 addrinfo 的结构指针的指针。 b) getaddrinfo 在当前函数作用域中创建一个 addrinfo 结构并更新 a) 中所需的指针

现在我真正的问题:我想将该 addrinfo 存储在其他地方。使用分配给其他指针不会执行深层复制,并且在该函数之后所有指针都将变得无效?

最好给出一个极其简化的示例:

void GetAddrInfo(struct addrinfo *update)
{
    struct addrinfo *res;
    getaddrinfo(xx,xx,xx,&res);

    //is this save? After this 'scope' ends all pointed fields are invalid?
    //this doesn't copy the linked list ai_next.
    *update=*res; 
}

直接在 getaddrinfo 上使用 &update 似乎不起作用,因为问题仍然存在:原始结构在函数作用域结束后被销毁。

任何人都可以在这里给我更多的见解(请解释什么在哪里创建,在哪里销毁,堆栈,堆所有信息都是受欢迎的)

I am a bit confused at least.
getaddrinfo() call 'updates' a pointer to a addrinfo struct, all is well when I am going to use the addrinfo in the same scope (that function) but what happens if I copy the struct to another one (by assigning it).

Please help me understand the undergoing basics (not seeking advice for alternative approaches).

Correct me if I am wrong:
a) getaddrinfo() requires a pointer to struct-pointer to addrinfo.
b) getaddrinfo creates a addrinfo struct in the current function scope and updates the pointer required in a)

Now my real question: i would like to store that addrinfo somewhere else. Using an assigning to an other pointer does not do a deep copy, and after the function all the pointers become invalid?

Better give an extremely simplified example:

void GetAddrInfo(struct addrinfo *update)
{
    struct addrinfo *res;
    getaddrinfo(xx,xx,xx,&res);

    //is this save? After this 'scope' ends all pointed fields are invalid?
    //this doesn't copy the linked list ai_next.
    *update=*res; 
}

Directly using &update on getaddrinfo seems to not work because the problem remains: the original struct get destroyed after the function scope ends.

Anyone able to give me more insight here (please explain what gets created where and destroyed where, stack, heap all info is welcome)

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

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

发布评论

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

评论(2

站稳脚跟 2024-11-02 11:33:41

函数作用域结束后原始结构体被销毁

否,结构体的指针被销毁。其余数据仍在堆中。如果您在完成结果后不调用 freeaddrinfo(),这将导致内存泄漏。

我想将该地址信息存储在其他地方

由于数据仍然存在,请随意复制指针;不需要深拷贝。从您的示例来看:

void GetAddrInfo(struct addrinfo **update)  /* pointer to pointer */
{
    struct addrinfo *res;
    getaddrinfo(xx,xx,xx,&res);
    *update=res;  /* save the pointer to the struct */
}

您只需使用以下命令调用此函数:

struct addrinfo *mycopy;
GetAddrInfo(&mycopy);

the original struct get destroyed after the function scope ends

No, the struct's pointer is destroyed. The rest of the data is still in the heap. This would be a memory leak if you don't call freeaddrinfo() when you're finished with the result.

i would like to store that addrinfo somewhere else

Since the data still exists, feel free to copy the pointer; no need for a deep copy. From your example:

void GetAddrInfo(struct addrinfo **update)  /* pointer to pointer */
{
    struct addrinfo *res;
    getaddrinfo(xx,xx,xx,&res);
    *update=res;  /* save the pointer to the struct */
}

You would simply call this function with:

struct addrinfo *mycopy;
GetAddrInfo(&mycopy);
微凉徒眸意 2024-11-02 11:33:41

getaddrinfo 分配一个 addrinfo 结构列表,并为您提供指向列表头部的指针。使用完后,可以通过将此指针传递给 freeaddrinfo 来释放所有已分配的内存。

你所做的事情足够安全,但会泄漏内存。

void GetAddrInfo(struct addrinfo **update)
{
    getaddrinfo(xx,xx,xx,update);
}


addrinfo * myai;
GetAddrInfo(&myai);
freeaddrinfo(myai)

这种方法不会泄漏内存 - 您只是检索指向 addrinfo 列表头的指针。

getaddrinfo allocates a list of addrinfo structures, and gives you the pointer to the head of the list. You release all allocated memory by passing this pointer to freeaddrinfo when you're done with it.

What you're doing is safe enough, but leaks memory.

void GetAddrInfo(struct addrinfo **update)
{
    getaddrinfo(xx,xx,xx,update);
}


addrinfo * myai;
GetAddrInfo(&myai);
freeaddrinfo(myai)

This approach will not leak memory - you're simply retrieving a pointer to the addrinfo list head.

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