473,842 Members | 1,553 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 3833
ar**********@gm ail.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**********@gm ail.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...@gm ail.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**********@gm ail.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...@mari ani.wswrote:
aruna.mys...@gm ail.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
2508
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: class Mem{ int *p, num; public: Mem(int a):p(0),n(a){ cout<< "created "<<num<<endl;
14
8215
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 = MyClass(); // Stack or Heap?
2
19340
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 missing and you have to add it. You are to use the linked list structure defined within the program, to implement a stack. In short, you need to implement the functions pop() and push() using a linked list. You are provided with calc-array.c which...
0
910
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 at it? What's wrong with it?: --- snip --- static PyObject * sys_stack(PyObject *self)
2
3563
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
10757
by: ravi | last post by:
Can any body tell me How to implement a stack using two queues Thax in advance
3
2994
by: palakagrawal | last post by:
concept of multiple stack
13
1650
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 have not figured out how to use “focus” to assure proper page sequencing. Here is an overview of my client-side application. There are 6 screens that capture user-entered data. Screen 1 opens 2, which opens 3, which opens 4, which opens 5, which...
2
1784
by: swagat12 | last post by:
How to implement a stack using queuqe
0
10945
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10614
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10674
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10317
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9454
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5697
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5886
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4500
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4090
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.