gpt4 book ai didi

c - asprintf();导致内存问题

转载 作者:太空宇宙 更新时间:2023-11-04 08:37:13 28 4
gpt4 key购买 nike

我正在使用 asprintf(&somestring,"%s%s",stringone,stringtwo) 来连接字符串。如果我调用一次然后调用 free(somestring) 一次,它工作正常。但是,如果我多次调用 asprintf() 但在递归函数中只调用一次 free(somestring) ,我就会遇到问题。是否需要为每个 asprintf() 调用 free()

最佳答案

Does free() need to be called for every asprintf()?

是的。根据其 documentation :

The functions asprintf() and vasprintf() are analogs of sprintf(3) and vsprintf(3), except that they allocate a string large enough to hold the output including the terminating null byte, and return a pointer to it via the first argument. This pointer should be passed to free(3) to release the allocated storage when it is no longer needed.

关于c - asprintf();导致内存问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25714332/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com