[C++] __builtin_new vs operator new

Benjamin Kosnik bkoz@cygnus.com
Mon Apr 24 12:48:00 GMT 2000


Check this out:

void test03()
{
  using namespace std;

  bool test = false;
  (void) set_new_handler(NULL);
  
  try {
      void* p = operator new(INT_MAX);
  } 
  catch (bad_alloc& ba) {
    test = true;
  }
}

This does not exhibit standard-conformant behavior. I would expect
that operator new would be called, that attempts to allocate this
ammount of memory will fail, and as set_new_handler is set to NULL,
operator new would return std::bad_alloc.

This does not happen. In particular, operator new (from
gcc/cp/new1.cc), which looks correct, does not get called. The
generated asm (g++ -S) is:

	.file	"3.cc"
	.version	"01.01"
gcc2_compiled.:
.globl __rethrow
.text
	.align 16
.globl test03__Fv
	.type	 test03__Fv,@function
test03__Fv:
.LFB1:
	pushl	%ebp
.LCFI0:
	movl	%esp, %ebp
.LCFI1:
	subl	$24, %esp
.LCFI2:
	movb	$0, -1(%ebp)
	subl	$12, %esp
	pushl	$0
.LCFI3:
	call	set_new_handler__3stdPFv_v
	addl	$16, %esp
.LEHB3:
	subl	$12, %esp
	pushl	$2147483647
	call	__builtin_new <--------------here
	addl	$16, %esp
	movl	%eax, -8(%ebp)
.LEHE3:
	jmp	.L9
	.p2align 4,,7
.L3:
.LCFI4:
	call	__start_cp_handler
	movl	%eax, -8(%ebp)
	movl	$0, -12(%ebp)
	movl	-8(%ebp), %eax
	movl	8(%eax), %eax
	movl	%eax, -12(%ebp)
	movb	$1, -1(%ebp)
	subl	$12, %esp
	pushl	-8(%ebp)
.LCFI5:
	call	__cp_pop_exception
	addl	$16, %esp
.L9:
	movl	%ebp, %esp
	popl	%ebp
	ret


Is there a way to force standard-conformant behavior? (And why is it
not the default?)

-benjamin



More information about the Gcc-bugs mailing list