Thread: Using new on primitive data types

  1. #1
    Registered User
    Join Date
    Oct 2014
    Posts
    9

    Using new on primitive data types

    int* count;
    count = new int(1); // what??? is this on the heap?? do i have to delete it now?

    So is 'new' on a primitive data type just a way for me to allocate primitive data types (int, char, etc.) on the heap instead of the stack?

    And, out of curiosity, can you do that in Java?
    Last edited by needhelp171; 12-12-2014 at 09:17 PM.

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    That is not valid C++. The result of operator new cannot be directly stored in an int.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Registered User
    Join Date
    Oct 2014
    Posts
    9
    Ack, typed it in here wrong. 'New' returns a pointer....I edited it to make count a pointer to an int....

    Thanks for the reply!

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Yeah, count points to an int on the heap. Yes, you have to delete it now (consider using smart pointers).
    "new" is a keyword that allows you to allocate any data type on the heap instead of the stack (but the pointer to the data is still located on the stack).
    Yes, you can do it in Java. In fact, Java requires you to allocate everything on the heap (although there are primitive types which I'm not sure if they're located on the heap or not).
    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.

  5. #5
    Registered User
    Join Date
    Oct 2014
    Posts
    9
    Thanks! You guys are really helpful!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. data types
    By SDH in forum C Programming
    Replies: 15
    Last Post: 01-13-2013, 07:04 AM
  2. A mini data-base handler. Very primitive but it works.
    By Treborh in forum C++ Programming
    Replies: 2
    Last Post: 03-06-2011, 01:24 PM
  3. data types
    By britto116 in forum C Programming
    Replies: 12
    Last Post: 11-13-2009, 07:32 AM
  4. data types types...
    By gftmc in forum C++ Programming
    Replies: 3
    Last Post: 09-11-2006, 11:30 AM
  5. putting primitive non pointer data types on stl vector
    By Silvercord in forum C++ Programming
    Replies: 8
    Last Post: 01-19-2003, 11:42 AM