Problems with operator new and constructor execution

Roger Jonsson K (QTX) roger.k.jonsson@etx.ericsson.se
Mon Nov 15 04:56:00 GMT 1999


Hello,

I found a bug with the operator new in gcc (version, platform and makefile options at the end of the mail).

I wrote my own new operator, it returns NULL when out of memory. In the case when new returns NULL the constructor of the object is still executed, which of course results in a core dump (crash) since I do not have any this pointer at this point (this = NULL).

To test this, simply write a operator new that returns NULL. When you try to create a new object with new the program will crash. In the minor code example below the program will crash in the empty constructor.

void* operator new (size_t s)
{
	return NULL;
}

CExample::CExample ()
{
	// Empty
}

CExample*
CExample::instance()
{
	if (!m_pInstance)
	{
		m_pInstance = new CExample();
	}
	return m_pInstance;
}




GCC versions and Platforms
-------------------------------------------------------------------------------------------------------------------------------------
gcc version 2.8.1,                     sparc-sun-solaris 2.6
gcc version 2.95.1 19990816 (release), sparc-sun-solaris 2.7


Make options
-------------------------------------------------------------------------------------------------------------------------------------
#############################################################################
# 2  COMPILER [CC]
#############################################################################
# Compiler&Linker
CC		= /usr/local/bin/gcc
LD		= $(CC)

# Preprocessor defines
OSEDEFINE	= -D_REENTRANT -DOS_DEBUG -DOSE_DELTA \
		  -DUNIX -DTEMPCONFIGFIX -D_POSIX_C_SOURCE=199506
SNMPDEFINE	= -D__TYPES_HAVE_boolean_t_
SOCKETDEFINE= -D__SOLARIS__PROC
DEFINE	= $(OSEDEFINE) $(SNMPDEFINE) $(SOCKETDEFINE)

# Compiler flags
CFLAGS	= -c -g $(DEFINE) $(INCLUDE)

# Linker flags
LIBS		= -linets -lsksnmpagent -lsnmpaux \
		  -lsocket -lnsl -lpthread

LDFLAGS	= $(OBJECTS) $(OSELIB) $(DBGSRVLIB) \
		  $(TOSVLIB) $(H323LIB) $(LIBS) \
		  /opt/GCC281/lib/libstdc++.a \
		  $(OSSLIB_API) -L$(ROME)/Inet -L$(SKSNMPAGENT)
-------------------------------------------------------------------------------------------------------------------------------------

Best regards,
Roger Jonsson
roger.k.jonsson@etx.ericsson.se




More information about the Gcc-bugs mailing list