[7229] in Athena Bugs
Bug in G++ on athena
daemon@ATHENA.MIT.EDU (daemon@ATHENA.MIT.EDU)
Sat Mar 2 18:51:49 1991
Date: Sat, 2 Mar 91 18:51:37 EST
From: andru@concerto.lcs.mit.edu (Andrew Myers)
To: bugs@ATHENA.MIT.EDU
The version (1.37.2) of g++ currently on athena contains a rather fatal
bug. I have run into this problem myself, and am enclosing a mail
message from someone else who has run into this problem as well. This
mail message includes a purported fix.
------------------------------------------------------------------------------
>From coolidge Thu Feb 7 15:31:52 1991
From: coolidge
Newsgroups: gnu.g++.bug
Subject: Re: compile errors with the new operator (and a bug fix)
Distribution: gnu
References: <22826@netcom.UUCP> <9102061007.AA10538@teacake.Eng.Sun.COM>
Reply-To: coolidge@cs.uiuc.edu
Unfortunately, the g++.texinfo distributed with 1.39.0 1) doesn't
document what 1.39.0 is supposed to do and 2) 1.39.0 doesn't do what
it's supposed to do anyway.
[...]
This gets you about half-way done; you can now write your own new
operator that g++ will actually recognize. Now, lets say we want to
use this new operator new:
char *manipulate_string (char *string)
{
int len = strlen (string) + 1;
char *s = strcpy (new char [len], string);
// ...
char *t = new {s} char;
return strcat (t, string);
}
At this point everything's fine and dandy, because new works in the
stock version iff you're using a non-structured type. But let's try
it with a structured type:
inline void * operator new( int new_size, void * foo ) { return( foo ); }
class Object {
public:
Object() {};
};
main()
{
char array[1024]; // Get a chunk of memory
printf("Array is based at %x\n",array);
Object * o = new {array} Object();
printf("o is based at %x\n",o);
}
Oops, this doesn't work too well on a standard g++:
nnew.cc:13: request for member `Object' in something not a structure or union
After toiling away in the guts of g++ for a while, I determined that
the problem is that doing new {foo} is returning a void *. The compiler
then tries to run the constructor for Object, but gets annoyed that
it's being handed a void * and not an Object *. Solution: convert the
pointer to an Object pointer. This involves patching cplus-init.c:
*** cplus-init.c.orig Mon Aug 6 07:53:49 1990
--- cplus-init.c Thu Feb 7 11:00:45 1991
***************
*** 2721,2726 ****
--- 2721,2727 ----
else if (placement)
{
rval = build_opfncall (NEW_EXPR, LOOKUP_GLOBAL|LOOKUP_COMPLAIN, ptr_ty
pe_node, size, placement);
+ rval = convert( TYPE_POINTER_TO(type), rval );
}
else if (flag_this_is_variable
&& TYPE_HAS_CONSTRUCTOR (type) && init != void_type_node)
Once this is done, you can have a user-defined operator new that can
build structured types. The project that I'm working on (an operating
system written in C++) uses the explicit placement syntax my test case
above uses in the early initialization code; hence, this had been
preventing us from moving to the newer g++'s.
--John
--------------------------------------------------------------------------
John L. Coolidge Internet:coolidge@cs.uiuc.edu UUCP:uiucdcs!coolidge
Of course I don't speak for the U of I (or anyone else except myself)
Copyright 1991 John L. Coolidge. Copying allowed if (and only if) attributed.
You may redistribute this article if and only if your recipients may as well.