Topic: stack variables and new constructor
Author: "Paul D. DeRocco" <pderocco@ix.netcom.com>
Date: 1996/03/13 Raw View
Jon.Cassorla wrote:
> 1) For a variable that is declared locally to a function of a
> structure
> type, is the standard new constructor executed to allocated the
> memory for the data structure?
No. You mustn't confuse "new" with "constructor". "new" allocates memory
from the heap (and "delete" returns it). A constructor turns raw memory
into a valid object (and a destructor turns it back into raw memory).
Using "new" always calls a constructor if there is one (and "delete"
always calls a destructor if there is one), but the opposite isn't true.
If an object is static, it is allocated space by the
compiler/linker/loader, constructed at the start of the program, and
destroyed at the end. If an object is dynamic, it is allocated space on
the stack, constructed when its declaration is executed, and destroyed
when the containing block exits. Only if it is created in the heap with
"new" does "operator new" get called.
> 2) When using "new" to allocate data space for a variable of a
> structure
> type (ie no constructor defined), is the resulting memory space
> zero'd
> out as a standard?
Nope, although there may be OSes that do this.
--
Ciao,
Paul D. DeRocco
---
[ 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 ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]
Author: jdmorris@ix.netcom.com (Jason D. Morris)
Date: 1996/03/13 Raw View
On 13 Mar 96 15:04:23 GMT, "Jon.Cassorla"
<cassorla@casey.columbiasc.ncr.com> wrote:
>1) For a variable that is declared locally to a function of a structure
> type, is the standard new constructor executed to allocated the
> memory for the data structure?
new is an operator not a constructor. The non-overloaded form of
operator new allocates memory in which an object can be constructed.
After the memory is allocated, the object's ctor is called to build
the object from raw memory.
>2) When using "new" to allocate data space for a variable of a structure
> type (ie no constructor defined), is the resulting memory space zero'd
> out as a standard?
No.
Jason
---
[ 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 ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]
Author: "Jon.Cassorla" <cassorla@casey.columbiasc.ncr.com>
Date: 1996/03/13 Raw View
I'm fairly new to the world of C++ so I didn't know if these questions would
be better asked in this group or in comp.lang.c++. Anyway, here goes.
1) For a variable that is declared locally to a function of a structure
type, is the standard new constructor executed to allocated the
memory for the data structure?
2) When using "new" to allocate data space for a variable of a structure
type (ie no constructor defined), is the resulting memory space zero'd
out as a standard?
--
Jon Cassorla email: Jon.Cassorla@ColumbiaSC.ATTGIS.COM
AT&T GIS Server Systems Software
3325 Platt Springs Road phone: (803) 939-2070, VP 632-2070
Columbia, SC 29170 FAX: (803) 939-7317
---
[ 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 ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]