Ryan Johnson wrote:
On 04/07/2012 5:45 AM, Claude SIMON wrote:
When I compile the component with Visual C++, it works. When I compile
component with g++... it crashes.
With 'gdb', I found that the problem happens when calling the 'malloc'
function (as soon as the function is called, NOT when the returned
allocated memory is used). When I replace the 'malloc' by a the C++
operator, the component compiled with Cygwin g++ doesn't crash anymore.
I thought that the C++ 'new' operator calls the 'malloc' function, but
this seems not to be the case. As I want to use 'malloc'-like functions
rather than the C++ 'new' operator, I wonder which functions are used in
the C++ 'new' operator to allocate memory (and naturally which functions
are used in the C++ 'delete' operator to free the memory).
Operator new() and malloc() are explicitly *not* interchangeable (for
many reasons, not least of which that the Standard says so). If you were
to free new'ed memory, or delete malloc'ed memory, the resulting heap
corruption could easily manifest as a crash the next time you tried to
allocate something... or it might just silently clobber data and lead to
"spooky action at a distance."
Thank you for the answer, but I am aware of this and my problem has
nothing to do with it, nor, as stated in the subject, with having some
lacuna in C/C++ programming.
Let's try to be a little more explicit despite my poor English.
Let's consider a Java native component which only calls a 'malloc(1)'. It
doesn't even test the returned value (it is usually not a good idea, but
it doesn't matter here).
This component :
- compiled with g++ under Linux : works,
- compiled with g++ under Mac OS : works,
- compiled with Visual C++ under Windows : works,
- compiled with g++ under Cygwin : CRASHES !
It crashes as soon the 'malloc(1)' function is called. You don't even have
the opportunity to test the returned value, nor to use it. It's perhaps a
Cygwin bug, or perhaps a JVM/JRE/JDK bug ; I don't know and I don't bother
(but if someone will make some investigation about that, I'm ready to help
him or her if I can).
When you replace the 'malloc()' by the 'new' operator, then the component
compiled with g++ under Cygwin works too.
The 'new' operator, among other things, allocates memory, as 'malloc()'
does, but obviously it doesn't use 'malloc()' as it doesn't crash. So,
because I can't use 'malloc()' in my Java native components, and because I
doesn't want to use the 'new' operator, I wish to know which functions the
'new' operator uses to allocate memory, so I can use them in my Java
native component so they would no more crash when compiled with g++ under