Free-ing pointers returned from shared libraries compiled with different compiler

Jan 28, 2017 at 5:54am
Hi, I am new to C++ and UNIX programming!

I have a question. Is it safe for me to free a pointer returned by a library compiled using a different compiler? If so, why is the case?
My system is x86_64 Ubuntu 16.04, and the library was compiled using GCC 6, whereas my program is compiled using clang 3.8.

P.S.: the library is XCB (X11 C Binding). It returned pointers to data structures, but expect the app to free the pointers. The problem is that the library doesn't provide any 'free' function.
Jan 29, 2017 at 5:03pm
What about "new"/"delete"? Does the actual implementation differs between compilers?
Jan 30, 2017 at 8:34am
It happens sometimes that memory allocations and frees of different compilers are incompatible, but on Linux there should be no such problems, especially between gcc and clang.
Last edited on Jan 30, 2017 at 8:37am
Feb 1, 2017 at 10:18am
Is it safe for me to free a pointer returned by a library compiled using a different compiler?

Probably not.

What about "new"/"delete"?

new/delete do their thing, i.e. allocate/release memory and call constructor/destructor. That isn't the problem.

The problem is making sure they're using the same heap.

Typically, this can be resolve using methods in the library, like create/release. I posted an example in this forum some time back. I'll see if I can find it.

EDIT:
http://www.cplusplus.com/forum/general/33662/

You need a release() method with these examples.
http://www.cplusplus.com/forum/general/35380/
http://www.cplusplus.com/forum/unices/184342/
Last edited on Feb 1, 2017 at 10:33am
Topic archived. No new replies allowed.