How it works? (About the memory which allocate by function)
-
We often can see that some function will return a pointer to a structure or a buffer, it must be aollocated by function itself. The question is how the memory is freed? and when it is freed? Can I aollocate some memory in a function and hope the caller will clean up the mass? I am using C, so the C++ destruction function is not what I want. Thanks
-
We often can see that some function will return a pointer to a structure or a buffer, it must be aollocated by function itself. The question is how the memory is freed? and when it is freed? Can I aollocate some memory in a function and hope the caller will clean up the mass? I am using C, so the C++ destruction function is not what I want. Thanks
As long as you tell the programmer calling your function that he/she has to free memory that you are allocating, there's nothing wrong with what you describe. Also, you should tell the caller which function to use when freeing the memory (free, delete, delete [] , GlobalFree, etc.). I do it all the time... -c
-
We often can see that some function will return a pointer to a structure or a buffer, it must be aollocated by function itself. The question is how the memory is freed? and when it is freed? Can I aollocate some memory in a function and hope the caller will clean up the mass? I am using C, so the C++ destruction function is not what I want. Thanks
You often see this? You shouldn't. That's very poor programming style. There are a few cases in the C Runtime library where you see this, but what happens here is that the C runtime library allocates the memory and cleans it up when the program exits. Generally, memory should be copied, or passed to a function through a pointer or reference, and that memory should be used by the function. Additionally, COM utilizes a mechanism where the Caller has to clean up memory when using SysAllocString and IMalloc. These rules are clearly documented though. you can't just assume people will clean up your allocations.
-
You often see this? You shouldn't. That's very poor programming style. There are a few cases in the C Runtime library where you see this, but what happens here is that the C runtime library allocates the memory and cleans it up when the program exits. Generally, memory should be copied, or passed to a function through a pointer or reference, and that memory should be used by the function. Additionally, COM utilizes a mechanism where the Caller has to clean up memory when using SysAllocString and IMalloc. These rules are clearly documented though. you can't just assume people will clean up your allocations.
First, I would like to thank both of you for your help. I am programming a set of netbios support function, one of which need return a structure pointer. and I need call it several times, yesterday i find a way to do this, that is use the 'static' modifier. then there will be only one copy of the structure and it will initialize when the program start and clean up when the program exit. although it will keep it's content through function calls, but that is not a matter. It's the solution that the C library used? by the way, I am sorry about my poor english, I am still a student. mask