I have been working in some C language function that is going to be called from a Progress 4GL application and I found myself with the following doubts:
The C function uses malloc to dynamically allocate an array of chars and the idea is that the pointer that allocates that memory will be returned to the Progress 4GL process which originally sent a MEMPTR data type to hold the result of the C method.
Basically the MEMPTR from Progress must be sent to the C function to "receive" the C generated array and use it when scope returns开发者_如何学Go to it from de C function (a pass by reference between both languages where the C method "fills" the MEMPTR progress variable).
My questions is:
When the Progress process finishes and the MEMPTR variable is "freed", does it free the malloc allocated memory in the C function as well?
and
If I free the malloc allocated memory in the C function I'm guessing the Progress process will receive garbage data, Is this correct?
Thanks for your time and help.
Greetings.
4GL/ABL: How to Call WIN32 API Function: GetLongPathName
Progress 4GL doesn't manage that memory for you. You need to provide a method to deallocate/free that memory.
Yes, if you ever access free()'d memory, access to it becomes undefined - it may crash, it may give you garbage data, or your program could continue chugging away without error.
Disclaimer: I work for Progress.
Generally in any cross-language programming environment, it is a Bad Idea to have or expect one language to deallocate memory allocated by the other. The language that allocates resources (like memory) should have a corresponding call to deallocate it when the client is done with them.
C in particular does not have garbage collection, so any memory allocated by it must be manually deallocated.
There is one exception. On most (non-embedded) platforms, all allocated resources will be reclaimed when the application terminates. So if the allocation is a one-time startup only type thing (rather than being done in a loop or something), then it is generally OK to just let the OS reclaim it when the program exits, rather than worrying about doing it manually.
精彩评论