new keyword

This is a discussion on new keyword within the C++ Programming forums, part of the General Programming Boards category; can someone explain the new keyword to me and how to use it? im confused by it! thanks...

  1. #1
    Unregistered
    Guest

    new keyword

    can someone explain the new keyword to me and how to use it? im confused by it! thanks

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    42
    It creates a new object of type


    new int;
    new class;


    Marky_Mark
    Last edited by Marky_Mark; 10-30-2001 at 04:07 PM.

  3. #3
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    no it doesn't......

    new is basically c++ version of malloc(). It is used to ask the OS for some memory.It returns a pointer to that memory if successful and if not it returns NULL.new is used only for dynamic memory allocation. It is an operator that can be overloaded.The big difference between new and malloc is that new will call pertinent constructors whereas malloc wont.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  4. #4
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    can someone explain the new keyword to me and how to use it?
    Cool, so what is the new keyword? I didn't think they were going to update until 2002.

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    42
    Originally posted by Stoned_Coder
    no it doesn't......

    new is basically c++ version of malloc(). It is used to ask the OS for some memory.It returns a pointer to that memory if successful and if not it returns NULL.new is used only for dynamic memory allocation. It is an operator that can be overloaded.The big difference between new and malloc is that new will call pertinent constructors whereas malloc wont.

    Thanks for the detailed explaination, but it still creates
    a new instantiation of a particular type of object
    that's how i use it anyway


    Marky_Mark
    Last edited by Marky_Mark; 10-31-2001 at 09:15 AM.

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    42
    Constructors are called as usual, after the allocation
    function is finished


    Marky_Mark

  7. #7
    Unregistered
    Guest
    what would be an appropriate use for new?

  8. #8
    Registered User
    Join Date
    Sep 2001
    Posts
    156
    new is used for dynamic memory allocation. If I don't know how much memory I'll need before I start, I'll need to consume memory as needed. A good example would be a linked list, typically I don't know how big the list is before I start so I use new to create nodes for the list as required.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. keyword replacement
    By JackBear in forum C Programming
    Replies: 11
    Last Post: 05-18-2007, 11:54 PM
  2. Virtual Keyword
    By Shal in forum C++ Programming
    Replies: 6
    Last Post: 05-18-2006, 12:37 PM
  3. 'new' keyword
    By pktcperlc++java in forum C++ Programming
    Replies: 4
    Last Post: 02-28-2005, 09:31 PM
  4. how to search a keyword in C?
    By kreyes in forum C Programming
    Replies: 2
    Last Post: 03-01-2002, 08:22 PM
  5. keyword searching in C
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 02-08-2002, 02:26 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21