Thread: New operator

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User ex-mortis's Avatar
    Join Date
    Mar 2012
    Posts
    37

    New operator

    Don't really understand what the new operator does. My books say it's a memory allocation operator which makes perfect sense, but does it simply free up memory to store a placeholder for a pointer? If so, what is the usefulness of reserving memory instead of just initializing a pointer and thus using the necessary amount of memory? What is the essential difference between:

    Code:
    int x = 100;
    int *p = &x;
    //do stuff
    delete p;
    and

    Code:
    int x = 100;
    int *p = new int; //really not too sure about this syntax, do you need to specify the type of the pointer when using new?
    //…
    *p = &x;
    //do stuff
    delete p;
    If your program needs pointers to hold memory addresses, why don't you just initialize the pointers when they are needed? Maybe I just have no idea what new does.
    Last edited by ex-mortis; 06-16-2012 at 06:50 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Ternary Operator to choose which operator to use
    By cncool in forum C Programming
    Replies: 7
    Last Post: 06-27-2011, 01:35 PM
  2. [ ] operator requires + operator?
    By m37h0d in forum C++ Programming
    Replies: 10
    Last Post: 02-04-2009, 10:21 AM
  3. Replies: 3
    Last Post: 12-09-2008, 11:19 AM
  4. Replies: 2
    Last Post: 07-07-2008, 03:46 AM
  5. Replies: 1
    Last Post: 07-07-2008, 03:38 AM