使用 SUN RPC 释放内存
全部。当我使用 静态 char* 结果 = malloc(1000*sizeof(char));
基于ONC+ SUN RPC的服务器程序的一程序。但这个字符串是应该返回的。
如果是静态的,我认为没有必要释放它。每次,字符串结果都会分配相同的地址,不会引起内存冲突。
我说得对吗?或者我该怎么办?这是返回值,如何释放呢? 谢谢
all. When I usestatic char* result = malloc(1000*sizeof(char));
in one procedure of the server program based on ONC+ SUN RPC. But this string is the what should be returned.
If it is static, I think there is no need to free it. For every time, string result will be allocated the same address, not cause memory conflicts.
Am I right? Or what should I do? This is the return value, how to free it?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果我理解您的问题,那么您正在返回类型
char*
的函数调用中使用初始化程序。这种情况下使用静态存储类就没有问题了。result
将在程序的整个生命周期中保留其值(您分配的内存地址)。但是,在这种情况下,malloc 不会被多次调用。仅在第一次执行您的功能时。此外,内存仍然分配在堆上,因此完成后仍然需要释放它。但是,此释放将在程序的 exit() 上执行,因此可以忽略。
If I understand your question, you are using the initializer in a function call that returns type
char*
. In that case, there is no problem with using the static storage class.result
will keep its value (the memory address you allocated) throughout the life of your program.However, malloc will not be called multiple times in this instance. Only the first time through your function. Also, the memory is still allocated on the heap, thus you still need to free it when you are done. However, this freeing will be performed on your program's exit(), so that could be ignored.