Thread: Does C++ new initialise it's value to zero?

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    113

    Does C++ new initialise it's value to zero?

    Does C++ new initialise it's value to zero?

    Code:
    int main()
    {
    int *i=new int;
    cout<<*i;//Will it always  be 0
    getch();
    return 0;
    }

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    No. Though your compiler may support such a feature, it's not portable.
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Aug 2005
    Posts
    113
    Do you know any compiler which does not do so???

  4. #4
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    Variables of built-in type defined inside the body of a function are uninitialized. You must initialize them explicitly. The new expression allows you to do this through direct-initialization.

    int *p = new int(0); //initialized to 0;

    Only types that are initialized by default inside a function, are classes that provide a default constructor.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    I know of no compiler (or, more accurately, it's library) which offers a guarantee that memory allocated by operator new is implicitly initialised to zero, as providing such a guarantee implies a performance hit (it would be necessary to explicitly initialise the memory). In your example, the value of *i will correspond to whatever happens to be in memory when it is reserved for *i by the use of operator new. And that value is likely to be random junk.

    In any event, what you are doing is yielding undefined behaviour (the act of reading an uninitialised value always yields undefined behaviour, and it is necessary to read a value to print it). Your program could just as easily terminate with a system crash as print junk data.

    If you want something created with operator new to be initialised, then you have to do it.
    Code:
    #include <iostream>
    
    int main()
    {
         int *i=new int(0);
        std::cout<<*i;//  Now it will always be 0
        return 0;
    }
    If you are working with user defined types (with a constructor), rather than int, then you can do the initialisation in the default constructor.

  6. #6
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    I think there's a function called ZeroMemory or something which sets everything to 0 ... I might be wrong though.

  7. #7
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    ZeroMemory is an MS-Windows specific function. memset() is more portable.
    Code:
    // allocate 255 integers
    int* array = new int[255];
    // non-portable use of memset.  
    memset(array,0,255 * sizeof(int));
    // more portable way to set elements to 0
    for(int i = 0; i < 255; i++)
       array[i] = 0;

  8. #8
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >I think there's a function called ZeroMemory or something which sets everything to 0
    memset is a standard C++ function that has the same effect as ZeroMemory and friends. However, it's somewhat dangerous in the presence of objects and certain primitive types. Better to use assignment or constructors to initialize your variables and ensure that it's done properly.
    My best code is written with the delete key.

  9. #9
    Registered User
    Join Date
    Aug 2005
    Posts
    113
    Ok,It was just my luck that I got garbage value as 0. I have tried several times but you will not always get it as zero.

  10. #10
    Registered User
    Join Date
    May 2006
    Posts
    903
    ZeroMemory is a macro which uses memset().
    Code:
    #define ZeroMemory(a, b) memset((a), 0, (b))
    If you care about portability you're better off defining it yourself or using memset().

  11. #11
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Yeah let's just ignore what Prelude said about it and throw all our two cents in.

    Well the OP will never again declare uninitialized variables on purpose thanks to this thread.

  12. #12
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Quote Originally Posted by Desolation
    ZeroMemory is a macro which uses memset().
    Code:
    #define ZeroMemory(a, b) memset((a), 0, (b))
    If you care about portability you're better off defining it yourself or using memset().
    Not on 64-bit Windows. There it's a macro that evaluates to RtlZeroMemory.

    Not that anyone should care about the implementation.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  13. #13
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by citizen
    Well the OP will never again declare uninitialized variables on purpose thanks to this thread.
    We can live in hope.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to initialise char array?
    By te5la in forum C++ Programming
    Replies: 3
    Last Post: 09-18-2008, 06:32 AM
  2. Is there a quicker way to initialise an array?
    By webznz in forum C Programming
    Replies: 5
    Last Post: 10-25-2007, 03:45 AM
  3. Initialising data when application initialises?
    By Davros in forum C++ Programming
    Replies: 4
    Last Post: 01-02-2007, 11:05 AM
  4. Replies: 2
    Last Post: 11-16-2003, 10:16 PM
  5. help with loops
    By ica0330 in forum C++ Programming
    Replies: 6
    Last Post: 01-14-2003, 09:15 PM