By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,403 Members | 855 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,403 IT Pros & Developers. It's quick & easy.

Allocating objects on stack using new in C++

P: n/a
Hi all,

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

Thanks in advance,
Aruna

Mar 6 '07 #1
Share this Question
Share on Google+
7 Replies


P: n/a
ar**********@gmail.com wrote:
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
Mar 6 '07 #2

P: n/a
John Harrison wrote:
ar**********@gmail.com wrote:
>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?
Well you can do it with placement new, but the question why still remains...

--
Ian Collins.
Mar 6 '07 #3

P: n/a
Ian Collins wrote:
John Harrison wrote:
>>ar**********@gmail.com wrote:

>>>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?

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
Mar 6 '07 #4

P: n/a
On Mar 6, 4:42 pm, aruna.mys...@gmail.com wrote:
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)

Mar 6 '07 #5

P: n/a
ar**********@gmail.com wrote:
Hi all,

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

Thanks in advance,
Aruna
Kinda - sorta. Here is an interesting discussion.

http://groups.google.com/group/comp....bf343b7f5b76c1
Mar 6 '07 #6

P: n/a
Sarath wrote:
>
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)
Mar 6 '07 #7

P: n/a
On Mar 6, 7:07 am, Gianni Mariani <gi3nos...@mariani.wswrote:
aruna.mys...@gmail.com wrote:
Hi all,
Is it possible to allocate an object on stack in C++ using new()
operator.
Thanks in advance,
Aruna

Kinda - sorta. Here is an interesting discussion.

http://groups.google.com/group/comp....thread/78dd7b1...
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....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.

Mar 7 '07 #8

This discussion thread is closed

Replies have been disabled for this discussion.