Thread: Quick Question New Operator

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    224

    Cool Quick Question New Operator

    Hi guys,

    Been away from programming for a while.

    I've read some info on the new operator and get the general jist.

    Can someone tell me in laymans terms/baby language, what I get and what I can basically done and can do with a statement like:

    Code:
    x = new double[2000]
    Cheers!!

  2. #2
    template<typename T> threahdead's Avatar
    Join Date
    Sep 2002
    Posts
    214
    After this assignment, you can index x from 0 to 1999.
    Another way to put it is to say now you have 2000 doubles allocated in memory which must be deleted afterwards again with

    Code:
    delete [] x;

  3. #3
    Registered User
    Join Date
    Nov 2006
    Posts
    224
    Great. Simply put.

    Thanks.

    So how do I use x now? Just like an array??

    e.g.)
    Code:
    x[0] = 1; x[1] = 2;

  4. #4
    template<typename T> threahdead's Avatar
    Join Date
    Sep 2002
    Posts
    214
    Quote Originally Posted by strokebow View Post
    Great. Simply put.

    Thanks.

    So how do I use x now? Just like an array??

    e.g.)
    Code:
    x[0] = 1; x[1] = 2;
    Yes.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Since you are new to pointers, I think it would be a good idea to teach you about smart pointers:
    std::shared_ptr<double> x = new double;
    (Requires C++0x.)
    Done. No need to delete.
    For arrays, you can do
    std::vector<double> x(2000);
    Done. No need to delete.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    Apr 2011
    Posts
    5
    You're allocating memory on the heap. The new operator allocates the amount of memory specified and returns a pointer to that memory.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Quick question about operator overloading
    By niudago in forum C++ Programming
    Replies: 3
    Last Post: 04-02-2003, 08:03 AM
  2. quick question about sizeof operator
    By *ClownPimp* in forum C++ Programming
    Replies: 1
    Last Post: 01-19-2002, 07:35 AM
  3. quick c++ operator question
    By pkananen in forum C++ Programming
    Replies: 3
    Last Post: 12-11-2001, 06:46 PM
  4. C number operator question
    By unanimous in forum C Programming
    Replies: 6
    Last Post: 10-26-2001, 09:36 PM