Topic: Operator new
Author: Olivier Fourel <olivier.fourel@animaths.com>
Date: 2000/04/19 Raw View
Il s'agit d'un message multivolet au format MIME.
--------------F94FF5429F4E9D5841445FB6
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
If i do the following thing :
{
....
Thing& thing = *new Thing;
}
Do I still have to delete the object ?
--------------F94FF5429F4E9D5841445FB6
Content-Type: text/x-vcard; charset=us-ascii;
name="olivier.fourel.vcf"
Content-Description: Carte pour Olivier Fourel
Content-Disposition: attachment;
filename="olivier.fourel.vcf"
Content-Transfer-Encoding: quoted-printable
X-MIME-Autoconverted: from 8bit to quoted-printable by mumnunah.cs.mu.OZ.AU id VAA03814
begin:vcard=20
n:Fourel;Olivier
tel;fax:01.55.43.13.49
tel;work:01.55.43.13.42
x-mozilla-html:FALSE
org:Math=E9matiques Appliqu=E9es S.A.
adr:;;;;;;
version:2.1
email;internet:olivier.fourel@animaths.com
fn:Olivier Fourel
end:vcard
--------------F94FF5429F4E9D5841445FB6--
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Christopher Eltschka <celtschk@physik.tu-muenchen.de>
Date: 2000/04/19 Raw View
Olivier Fourel wrote:
>
> If i do the following thing :
> {
> ....
> Thing& thing = *new Thing;
> }
> Do I still have to delete the object ?
Yes.
The only special rule is for temporaries bound to const references,
where the life time is extended to the end of the scope.
Also note that the temporary must be directly bound:
int const& f(int const& i)
{
return i;
}
int main()
{
int const& r1 = 5; // lifetime extended until end of main
int const& r2 = f(5); // lifetime *not* extended
// now r2 is a dangling reference
}
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Paul Black <paul.black@oxsemi.com>
Date: 2000/04/19 Raw View
Olivier Fourel <olivier.fourel@animaths.com> wrote:
>
> If i do the following thing :
> {
> .....
> Thing& thing = *new Thing;
> }
> Do I still have to delete the object ?
Yes (delete &thing).
Paul
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Steve Clamage <stephen.clamage@sun.com>
Date: 2000/04/20 Raw View
Christopher Eltschka wrote:
>
> Olivier Fourel wrote:
> >
> > If i do the following thing :
> > {
> > ....
> > Thing& thing = *new Thing;
> > }
> > Do I still have to delete the object ?
>
> Yes.
> The only special rule is for temporaries bound to const references,
> where the life time is extended to the end of the scope.
But this question has nothing to do with temporary objects. The Thing
object created is not a temp. but is an object with dynamic storage
duration. It exists until it is explicitly destroyed. If the program
ends without explicitly destoying the object, the desructor for the
object never gets run.
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: rjpierce@bga.com (Robert J. Pierce)
Date: 14 Jun 1994 22:56:52 GMT Raw View
Is the 'based' version of the operator new (e.g. new (base_ptr) object)
standard C++? I have code I wrote using IBM's C++ compiler that uses
new this way, but now my Borland compiler does not seem to support it.
I am passing pointers to objects between processes (in OS/2) and need to
create the objects in shared memory so both processes can use them.
BoB Pierce
Author: cbarber@bbn.com (Christopher Barber)
Date: 15 Jun 1994 14:45:07 GMT Raw View
>>>>> "BP" == Robert J Pierce <rjpierce@bga.com> writes:
BP> Is the 'based' version of the operator new (e.g. new (base_ptr)
BP> object) standard C++? I have code I wrote using IBM's C++ compiler
BP> that uses new this way, but now my Borland compiler does not seem
BP> to support it.
This is also known as the 'placement' form of new. I would be very
suprised if the Borland compiler did not support this but you will
need to get the declaration and definition of the placement form from
somewhere. Look for a system header <new.h> to see if it is there. If
not, you can define this yourself quite easily:
void *operator new (size_t size, void *addr)
{
return addr ;
}
BP> I am passing pointers to objects between processes (in OS/2) and
BP> need to create the objects in shared memory so both processes can
BP> use them.
I don't know how shared memory works in OS/2, but in many operating systems
the same piece of memory can be mapped to a different virtual address in
different processes so beware of using pointers within shared memory. Also
watch out for anything that relies on stuff outside of shared memory; this
restriction usually means that you can't have classes with virtual bases or
virtual methods in shared memory....
- Chris
--
Christopher Barber
(cbarber@bbn.com)
Author: fjh@munta.cs.mu.OZ.AU (Fergus Henderson)
Date: Thu, 16 Jun 1994 18:11:20 GMT Raw View
rjpierce@bga.com (Robert J. Pierce) writes:
>Is the 'based' version of the operator new (e.g. new (base_ptr) object)
>standard C++?
Yes. (It's normally called "placement" new rather than "based" new.)
See the ARM for details.
--
Fergus Henderson - fjh@munta.cs.mu.oz.au