all. When I use
static char* result = m开发者_开发技巧alloc(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
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.
精彩评论