Differences between C++ 'new' operator and 'malloc()' (NOT a C/C++ question)

Claude SIMON sc.cygwin.com@zeusw.org
Wed Jul 4 09:45:00 GMT 2012


Hello everybody !

I have some C++ code that I want to use in a native Java component
compiled with Cygwin g++.

I also use exactly the same C++ code in a command-line tool ('.exe' binary).

When I compile the tool with Visual C++, it works. When I compile the tool
with g++, it works.

When I compile the component with Visual C++, it works. When I compile the
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++ 'new'
operator, the component compiled with Cygwin g++ doesn't crash anymore.

To summarize, having some code written in C++ :
	- used in a command line tool :
		- using 'malloc' :
			- compiled with Visual C++ : works.
			- compiled with Cygwin g++ : works.
	- used in a Java native component :
		- using 'malloc' :
			- compiled with Visual C++ : works.
			- compiled with Cygwin g++ : CRASHES.
		- using the C++ 'new' operator :
			- compiled with Visual C++ : works.
			- compiled with Cygwin g++ : works.

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).

If it can be of some use, the component/tool can be found at
http://zeusw.org/intl/expp . The g++ version is 4.5.3.

Thanks !

--
Claude SIMON (sc.CYGWIN;COM@zeusw.org)
(Sorry for my poor English ; I'm French)



--
Problem reports:       http://cygwin.com/problems.html
FAQ:                   http://cygwin.com/faq/
Documentation:         http://cygwin.com/docs.html
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple



More information about the Cygwin mailing list