473,387 Members | 1,859 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Allocating objects on stack using new in C++

Hi all,

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

Thanks in advance,
Aruna

Mar 6 '07 #1
7 3799
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: hall | last post by:
Hi. I have some problems with a class i've written that allocates memory dynamicaly. I want to put these objects into a std::vector, but it does not work. My class looks (simplified) like this: ...
14
by: Olumide | last post by:
In the following C++ class, MyClass{ // Members } From which I create two objects (using the default constructor), (1) MyClass *object1 = new MyClass(); // Stack (2) MyClass object2 =...
2
by: hakimks | last post by:
You are provided with a sample C programs: calc.c, which implements a reverse polish notation calculator. Study it carefully. This program uses a stack (of course!) but the stack implementation is...
0
by: Fabiano Sidler | last post by:
Hi folks! For getting a plan how a stack-based VM like Python works, I added a function that prints out the current object stack. Unfortunately, it crashes the VM. Could someone here take a look...
2
by: selam | last post by:
I have a problem in C++.My problem is how to push random number to a stack. It is to generate 20 random numbers and push it to the stack and pop it from the stack when it's needed.
5
by: ravi | last post by:
Can any body tell me How to implement a stack using two queues Thax in advance
3
by: palakagrawal | last post by:
concept of multiple stack
13
by: trbjr | last post by:
My client-side application works reliably and as intended in Firefox, but not in IE6. The problem seems to be that I do not have control over the page stack in IE, while I do in Firefox. So far I...
2
by: swagat12 | last post by:
How to implement a stack using queuqe
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.