Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6602625
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T18:53:55+00:00 2026-05-25T18:53:55+00:00

There is such code: #include <iostream> int main(){ unsigned int* wsk2 = new unsigned

  • 0

There is such code:

#include <iostream>

int main(){
  unsigned int* wsk2 = new unsigned int(5);
  std::cout << "wsk2: " << wsk2 << " " << *wsk2 << std::endl;
  delete wsk2;
  wsk2 = new unsigned int;
  std::cout << "wsk2: " << wsk2 << " " << *wsk2 << std::endl;
  return 0;
}

Result:

wsk2: 0x928e008 5
wsk2: 0x928e008 0

I have read that new doesn’t initialize memory with zeroes. But here it seems that it does. How does it work?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T18:53:56+00:00Added an answer on May 25, 2026 at 6:53 pm

    There are two versions:

    wsk = new unsigned int;      // default initialized (ie nothing happens)
    wsk = new unsigned int();    // zero    initialized (ie set to 0)
    

    Also works for arrays:

    wsa = new unsigned int[5];   // default initialized (ie nothing happens)
    wsa = new unsigned int[5](); // zero    initialized (ie all elements set to 0)
    

    In answer to comment below.

    Ehm… are you sure that new unsigned int[5]() zeroes the integers?

    Apparently yes:

    [C++11: 5.3.4/15]: A new-expression that creates an object of type T initializes that object as follows: If the new-initializer is omitted, the object is default-initialized (8.5); if no initialization is performed, the object has indeterminate value. Otherwise, the new-initializer is interpreted according to the initialization rules of 8.5 for direct-initialization.

    #include <new>
    #include <iostream>
    
    
    int main()
    {
        unsigned int   wsa[5] = {1,2,3,4,5};
    
        // Use placement new (to use a know piece of memory).
        // In the way described above.
        // 
        unsigned int*    wsp = new (wsa) unsigned int[5]();
    
        std::cout << wsa[0] << "\n";   // If these are zero then it worked as described.
        std::cout << wsa[1] << "\n";   // If they contain the numbers 1 - 5 then it failed.
        std::cout << wsa[2] << "\n";
        std::cout << wsa[3] << "\n";
        std::cout << wsa[4] << "\n";
    }
    

    Results:

    > g++ --version
    Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
    Apple LLVM version 5.1 (clang-503.0.40) (based on LLVM 3.4svn)
    Target: x86_64-apple-darwin13.2.0
    Thread model: posix
    > g++ t.cpp
    > ./a.out
    0
    0
    0
    0
    0
    >
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

There is such code: #include <iostream> int main() { int a; int* p =
There is such code: #include <iostream> int main(){ const int a = 2; int*
There is such code: #include <iostream> class A{ int a; int fun(){} }; class
There is such code: #include <iostream> class A { public: int a; A() :
i have following code #include <iostream> #include <cstdlib> using namespace std; int main() {
there! I'm writting graph classes. I've wrote such code in Graphs.h : #include <vector>
Is there such a thing? I'm talking about something like a C++ new command
In Java, there is no such thing as an unsigned byte. Working with some
The following noddy test code: #include <iostream> #include <list> #include <boost/any.hpp> #include <boost/foreach.hpp> #include
I normally in my projects use such these code: If user.IsInRole(Admin) Then deleteButton.Visible =

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.