Subject: Spurious g++ warning? inlining new and delete
To: None <tech-userlevel@NetBSD.ORG>
From: Brian C. Grayson <bgrayson@ece.utexas.edu>
List: tech-userlevel
Date: 12/10/1997 12:31:07
  Yes, this is slightly off topic, but it's a userlevel question,
and my ultimate concern is a change to gcc/g++ in the source
tree...

  Somewhere I got the impression that it was acceptable for a
user program to redefine the ``global'' new and delete
operators.  For example, in my research project, we redefine
them, in a header file so they get inlined, to use the BSD
allocator directly.  

  I'm not sure how much performance it gives us, but it makes
it easy to do things like ``if DEBUG is defined, overwrite a
freed object with 0xcccccccc (my favorite) or 0xdeadbeef'', for
easy allocation-bug tracking.

  However, when /usr/bin/gcc sees my inline definitions, it complains:

../../MI/fastalloc.h:52: warning: `void operator delete(void *)'
	  was used before it was declared inline
<internal>:52: warning: previous non-inline declaration here
../../MI/fastalloc.h: In function `void operator delete [](void *)':
../../MI/fastalloc.h:57: warning: `void operator delete [](void *)'
	  was used before it was declared inline
<internal>:57: warning: previous non-inline declaration here
../../MI/fastalloc.h: In function `void * operator new(unsigned int)':
../../MI/fastalloc.h:63: warning: `void * operator new(unsigned int)'
	  was used before it was declared inline
<internal>:63: warning: previous non-inline declaration here
../../MI/fastalloc.h: In function `void * operator new [](unsigned int)':
../../MI/fastalloc.h:68: warning: `void * operator new [](unsigned int)'
	  was used before it was declared inline
<internal>:68: warning: previous non-inline declaration here


  We've already hacked our local gcc to not print this warning
for redefinitions of inline funtions that are defined
<internal>ly, but before I send-pr it, I'd like to know if this
violates some C++ standard.  I thought I had gotten the idea from
Stroustrup's book.  I'm pretty sure the only thing the compiler
is unhappy about is the inlining, not the redefinition.  (And
yes, we include the appropriate header (fastalloc.h) before ANY
code in ALL files, so we know it is not actually being used,
contrary to the warning message).

  I don't know enough gcc internals to do a patch that also
checks whether or not it really _was_ used before the
redefinition.

  Before I had tracked down the exact location, I sent in a bug
report to the GCC folks (about a year or two ago), and they
gave me a patch that didn't fix the problem, and they didn't
seem too interested in solving the problem.

  For an example, try compiling the following one-liner as a .cc file:
inline void operator delete (void* p) { do_free(p); }

  If you remove the inline keyword, it compiles without complaint
(well, it complains about do_free, of course).

  Thanks in advance.

  Brian
-- 
Brian Grayson (bgrayson@ece.utexas.edu)
Graduate Student, Electrical and Computer Engineering
The University of Texas at Austin
Office:  ENS 406       (512) 471-8011
Finger bgrayson@orac.ece.utexas.edu for PGP key.