调用 C 函数的 Progress 4GL
我一直在研究将从 Progress 4GL 应用程序调用的一些 C 语言函数,我发现自己有以下疑问:
C 函数使用 malloc 动态分配字符数组,其想法是分配的指针该内存将返回给 Progress 4GL 进程,该进程最初发送 MEMPTR 数据类型来保存 C 方法的结果。
基本上,Progress 中的 MEMPTR 必须发送到 C 函数,以“接收”C 生成的数组,并在范围从 de C 函数返回时使用它(在两种语言之间通过引用传递,其中 C 方法“填充”MEMPTR 进度多变的)。
我的问题是:
当 Progress 进程完成并且 MEMPTR 变量被“释放”时,它是否也会释放 C 函数中 malloc 分配的内存?
如果
我释放 C 函数中 malloc 分配的内存,我猜测 Progress 进程将收到垃圾数据,这是正确的吗?
感谢您的时间和帮助。
问候。
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 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
4GL/ABL:如何调用WIN32 API函数:GetLongPathName
Progress 4GL 不会为您管理该内存。您需要提供一种方法来解除分配/释放该内存。
是的,如果您曾经访问过 free()d 内存,则对它的访问将变得不确定 - 它可能会崩溃,可能会给您带来垃圾数据,或者您的程序可能会继续运行而不会出现错误。
免责声明:我为进步工作。
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.
通常,在任何跨语言编程环境中,让或期望一种语言释放另一种语言分配的内存都是一个坏主意。分配资源(如内存)的语言应该有一个相应的调用,以便在客户端使用完资源后释放资源。
C 特别没有垃圾回收功能,因此必须手动释放由它分配的任何内存。
有一个例外。在大多数(非嵌入式)平台上,应用程序终止时将回收所有分配的资源。因此,如果分配是一次性启动类型的事情(而不是在循环或其他方式中完成),那么通常可以让操作系统在程序退出时回收它,而不用担心手动执行此操作。
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.