about new

This is a discussion on about new within the C++ Programming forums, part of the General Programming Boards category; hello i cannot understand whats the benefit of saying that Code: int x=new int rather saying Code: int x; i ...

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    25

    about new

    hello i cannot understand whats the benefit of saying that
    Code:
     int x=new int
    rather saying
    Code:
     int x;
    i know that new allocates memmory but please tell me the different of the above codes

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,439
    The first is a compile error.
    Regardless, if you really meant a pointer, then the difference is that

    - You aren't limited to stack space (~1 MB usually).
    - The allocated storage won't be reclaimed as the function ends (extended lifetime; but comes with the disadvantage that you have to manually deallocate that).
    - You can dynamically specify size (you can only specify the amount of storage to reserve on the stack with something that is known at compile time).
    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.

Popular pages Recent additions subscribe to a feed

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