Allocating objects on stack using new in C++

Discussion in 'C++' started by aruna.mysore@gmail.com, Mar 6, 2007.

  1. Guest

    Hi all,

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

    Thanks in advance,
    Aruna
    , Mar 6, 2007
    #1
    1. Advertising

  2. 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
    John Harrison, Mar 6, 2007
    #2
    1. Advertising

  3. Ian Collins Guest

    John Harrison wrote:
    > 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.
    Ian Collins, Mar 6, 2007
    #3
  4. Ian Collins wrote:
    > John Harrison wrote:
    >
    >> 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
    John Harrison, Mar 6, 2007
    #4
  5. Sarath Guest

    On Mar 6, 4:42 pm, 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)
    Sarath, Mar 6, 2007
    #5
  6. Gianni Mariani, Mar 6, 2007
    #6
  7. Pete Becker Guest

    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)
    Pete Becker, Mar 6, 2007
    #7
  8. Guest

    On Mar 6, 7:07 am, Gianni Mariani <> wrote:
    > 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.std.c /browse_frm/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...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.
    , Mar 7, 2007
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. cppaddict
    Replies:
    2
    Views:
    337
    Chris Uppal
    Aug 10, 2004
  2. Samuele Armondi

    Allocating istringstream objects

    Samuele Armondi, Jul 11, 2003, in forum: C++
    Replies:
    1
    Views:
    356
    Samuele Armondi
    Jul 11, 2003
  3. Rakesh Kumar
    Replies:
    5
    Views:
    666
    James Kanze
    Dec 21, 2007
  4. Michael Tsang
    Replies:
    13
    Views:
    1,561
  5. Michael Tsang
    Replies:
    11
    Views:
    510
Loading...

Share This Page