Segmentation fault inside a call to the 'new' operator
Marilyn Davis
marilyn@deliberate.com
Sun Jun 27 20:31:00 GMT 1999
>
> > > Good luck.
> > Thanks Marilyn, but I think that my post was not clear enough.
> > The problem is that if I write something like this
> >
> > {
> > const int N = 1024;
> >
> > // ...
> > double *p;
> >
> > cout << "before\n";
> > p = new double[N];
> > cout << "after\n";
> > // ...
> > }
> >
> > I get the first message ("before") and then the "segmentation fault"
> > message. The "after" message is not printed. So the crash happens
> > _inside_ the call to the new operator. Using a debugger confirms my
I think I understood you right.
I'd still say you should trace your steps since the last call to new.
What happens is that you overwrite memory, i.e., write into space not
allocated to you, but the process is ok until it looks at that memory
in the way it expects to see it. Something you did before your cout
<< "before" trounced on memory you allocated previously but the error
wasn't noticed until the call to new where it crashes.
> I'm betting that you have freed some memory more than once, or
> freed a pointer that you didn't really allocate (like freeing an
> offset of on one that you actually allocated).
Hmmm, wouldn't that crash right at the call to free? Maybe not, I'm
new to this compiler.
>
> I have written a replacement for the new and delete operators which
> (inefficiently) account for memory allocations. You can simply link against
> it and it will report problems. If you replace new and delete with macros,
This is great! How can I get a copy of these?
> it will give you line numbers as well. If interesetd, send email.
Does this count?
> Which actually brings me to a point...
>
>
> How come I can overload new and do this
>
> new (something) Some_Class;
>
> but if I overload delete and try this
>
> delete (something) some_ptr;
>
> I get an error. I know what the compiler is trying to do. It's trying to
> cast some_ptr to a "something" before the delete. However, I want it to
> make a call like this
>
> operator delete(some_ptr, something);
>
> which does compile.
>
Sorry, I don't know.
Marilyn@deliberate.com
More information about the Gcc
mailing list