Thread: new/delete vs array on stack

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    108

    new/delete vs array on stack

    suppose I need a quick and dirty array for a calculation. Will it be better to use new/delete as opposed to just allocating them on stack? For example:

    Code:
    int dostuff(){
       char * x = new char[2048];
       // do stuff
       delete x;
    }
    as opposed to:

    Code:
    int dostuff(){
       char x[2048];
       // do stuff with x
    }
    I have a feeling the second one isn't very common.. thanks in advance.

  2. #2
    Banned ಠ_ಠ's Avatar
    Join Date
    Mar 2009
    Posts
    687
    Quote Originally Posted by underthesun View Post
    Will it be better to use new/delete as opposed to just allocating them on stack?
    it wont be better to do that unless you want the size to change based off of some conditions
    ╔╗╔══╦╗
    ║║║╔╗║║
    ║╚╣╚╝║╚╗
    ╚═╩══╩═╝

  3. #3
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> I have a feeling the second one isn't very common.. thanks in advance.

    It usually depends on how large (in bytes) the buffer is going to be, and if the function is recursive or not. You generally have more heap space available to you than stack, so if the buffer is large or the recursion deep you might get into some trouble allocating off the stack.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  4. #4
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by underthesun View Post
    suppose I need a quick and dirty array for a calculation. Will it be better to use new/delete as opposed to just allocating them on stack? For example:

    Code:
    int dostuff(){
       char * x = new char[2048];
       // do stuff
       delete x;
    }
    Well I certainly wouldn't use the code you've got, since it has a big bug in it. You use the array version of new, but the single version of delete. You need to use:
    delete [] x; if x is allocated as an array instead of a single char.

    If you don't know how many chars you'll need, why bother with new/delete anyways? Just use std::vector<char> or std::string.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    108
    Whoops, nice catch there. I actually do know how much space I'll need, it's just that it might overflow from the stack.. I guess I'll switch to new[] and delete[].

    Thanks

  6. #6
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> I guess I'll switch to new[] and delete[].

    And if an exception is thrown from the function, who cleans up the memory? Nobody! Pointers don't have destructors. That's why their so unsafe for managing memory. As cpjust recommended, use std::vector, std::string, or at the very least some user-defined type to ensure that the resources are cleaned up properly.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    108
    good point, but the "operation" I have right now merely consists of memcpy's, and is used for an opengl function (mashing and interleaving vertex & color data from two arrays). I guess I'll switch to something else when the function grows more complicated. thanks

    p.s I believe opengl never throws exceptions, but instead just have their own error logging functionality.

  8. #8
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    OpenGL doesn't throw, indeed, but who will go through all the code if you switch to a wrapper that does?

    Be exception-safe now, because it's not going to happen later. There is simply no good reason to use a local pointer over a vector. The vector is simpler to write, less things to remember, safer to use - it simply has no drawbacks.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    108
    Hmm, good point. Here comes vector<GLfloat>..

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  2. Stringy Sums
    By bumfluff in forum C++ Programming
    Replies: 14
    Last Post: 05-15-2006, 01:52 AM
  3. infix evaluation using stack
    By lewissi in forum C++ Programming
    Replies: 0
    Last Post: 11-03-2005, 02:56 AM
  4. stack implementation problem-help needed
    By sanju in forum C Programming
    Replies: 1
    Last Post: 12-10-2002, 07:29 AM
  5. Array Stack Problem
    By Drew in forum C++ Programming
    Replies: 3
    Last Post: 09-04-2001, 06:58 PM