Allocating objects on stack using new in C++

A

aruna.mysore

Hi all,

Is it possible to allocate an object on stack in C++ using new()
operator.

Thanks in advance,
Aruna
 
J

John Harrison

Hi all,

Is it possible to allocate an object on stack in C++ using new()
operator.

Thanks in advance,
Aruna

No, but why would you want to? What benefit is there over declaring an
object on the stack in the normal way?

john
 
I

Ian Collins

John said:
No, but why would you want to? What benefit is there over declaring an
object on the stack in the normal way?
Well you can do it with placement new, but the question why still remains...
 
J

John Harrison

Ian said:
Well you can do it with placement new, but the question why still remains...

You could do that, but I'd question whether placement new allocates
anything. In it's normal use it just constructs, it doesn't allocate.

It's a difficult one, because the OP could have a legitmate question, or
they could just be confused about something.

OP we need some context.

john
 
S

Sarath

Hi all,

Is it possible to allocate an object on stack in C++ using new()
operator.

Thanks in advance,
Aruna

Are you looking for a managed style new/gcnew which take care of
memory allocation like stack (do cleanup upon exiting it's scope)?

There's a garbage collect under low priority consideration in the new
standard.

You can also use auto_pointers to do automatic cleanup on exit.
In the new proposed standard, there are new set of pointers to manage
the resource. (seems those are not relevant for your context)
 
P

Pete Becker

Sarath said:
There's a garbage collect under low priority consideration in the new
standard.

The registration draft for C++0x says "The next C++ standard is intended
to include support for programmer-controlled garbage collection." That's
one of a handful of new features that haven't been fully fleshed out
yet. These are definitely not low priority.

--

-- Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com)
Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." (www.petebecker.com/tr1book)
 
D

dseiler


In a similar-looking vein, it's possible to give an STL container an
allocator that allocates from a stack allocated buffer, as described
here: http://www.greythumb.org/blog/index...ating-entire-STL-structures-on-the-stack.html

You have to know the maximum memory usage in advance, so there's no
real point in using this technique on linear containers like vectors
and lists, but if you need a super-speedy map in a particular scope,
it's there.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top