User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 391,995 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 4,232 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser:
Views: 255 | Replies: 7
Reply
Join Date: Mar 2007
Posts: 36
Reputation: titaniumdecoy is on a distinguished road 
Rep Power: 2
Solved Threads: 4
titaniumdecoy's Avatar
titaniumdecoy titaniumdecoy is offline Offline
Light Poster

Type initialization

  #1  
Jul 7th, 2008
I did some searching on this topic but couldn't find anything definitive.

Are basic types automatically initialized to 0 in C++ when allocated via new ? For example, are all values of nums guaranteed to be 0 in the code below?

int *nums = new int[10];
Last edited by titaniumdecoy : Jul 7th, 2008 at 4:39 pm.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Aug 2007
Posts: 128
Reputation: hacker9801 is on a distinguished road 
Rep Power: 2
Solved Threads: 13
hacker9801 hacker9801 is offline Offline
Junior Poster

Re: Type initialization

  #2  
Jul 7th, 2008
I guess not - it still has garbage.

Did this test.

  1. #include <cstdlib>
  2. #include <iostream>
  3. using namespace std;
  4.  
  5. int main(int argc, char *argv[])
  6. {
  7. int *nums = new int[10];
  8.  
  9. for(int i=0;i<10;++i) cout << i << ": " << nums[i] << endl;
  10.  
  11. delete [] nums;
  12.  
  13. system("PAUSE");
  14. return EXIT_SUCCESS;
  15. }

but like everything in c/c++ I'm sure it's compiler-dependant.
Last edited by hacker9801 : Jul 7th, 2008 at 5:35 pm.
Don't yell at me if I'm wrong - I'm thirteen. :)
Reply With Quote  
Join Date: Oct 2007
Location: Cherry Hill, NJ
Posts: 1,809
Reputation: Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold 
Rep Power: 11
Solved Threads: 184
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: Type initialization

  #3  
Jul 7th, 2008
In C and C++, nothing is automatically initialized. (I'm pretty sure.)

You can use the STL fill_n() function to initialize it for you:
  1. #include <algorithm> // fill_n() and copy()
  2. #include <iostream> // cout, of course
  3. #include <iterator> // ostream_iterator<>
  4. #include <limits> // numeric_limits<>
  5.  
  6. using namespace std;
  7.  
  8. int main()
  9. {
  10. int *nums = new int[ 10 ];
  11.  
  12. cout << "Before initialization: ";
  13. copy( nums, nums +10, ostream_iterator <int> ( cout, " " ) );
  14. cout << endl;
  15.  
  16. fill_n( nums, 10, 42 );
  17.  
  18. cout << "After initialization: ";
  19. copy( nums, nums +10, ostream_iterator <int> ( cout, " " ) );
  20. cout << endl;
  21.  
  22. cout << "Press ENTER to finish";
  23. cin.ignore( numeric_limits <streamsize> ::max(), '\n' );
  24.  
  25. delete[] nums;
  26. return 0;
  27. }
Hope this helps.
Reply With Quote  
Join Date: Jun 2008
Posts: 11
Reputation: pradhan.nc is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 2
pradhan.nc pradhan.nc is offline Offline
Newbie Poster

Re: Type initialization

  #4  
Jul 8th, 2008
It needs always to be initialized otherwise its containt will be undefined.

When you define an array. The compiler only initializes the starting address of it on the stack. It only reserves the memory space as per the size specified.
Reply With Quote  
Join Date: Sep 2004
Posts: 6,020
Reputation: Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of 
Rep Power: 26
Solved Threads: 414
Super Moderator
Narue's Avatar
Narue Narue is online now Online
Expert Meanie

Re: Type initialization

  #5  
Jul 8th, 2008
>Are basic types automatically initialized to 0 in C++ when allocated via new ?
No, automatic default initialization of built-in objects doesn't happen most of the time. You'll see it with objects that have static storage duration, typically. But you can force default initialization with an explicit empty parameter list:
  1. int *nums = new int[10]();
Member of: Beautiful Code Club.
Reply With Quote  
Join Date: Mar 2007
Posts: 36
Reputation: titaniumdecoy is on a distinguished road 
Rep Power: 2
Solved Threads: 4
titaniumdecoy's Avatar
titaniumdecoy titaniumdecoy is offline Offline
Light Poster

Re: Type initialization

  #6  
Jul 8th, 2008
Originally Posted by Narue View Post
But you can force default initialization with an explicit empty parameter list:
  1. int *nums = new int[10]();

That is a neat trick. Thanks.
Reply With Quote  
Join Date: Jun 2008
Posts: 11
Reputation: pradhan.nc is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 2
pradhan.nc pradhan.nc is offline Offline
Newbie Poster

Re: Type initialization

  #7  
Jul 8th, 2008
Originally Posted by titaniumdecoy View Post
That is a neat trick. Thanks.



No!!

It may not work properly on all the compilers. Don't do something which is undefined it may create problems in the long run.
Reply With Quote  
Join Date: Sep 2004
Posts: 6,020
Reputation: Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of 
Rep Power: 26
Solved Threads: 414
Super Moderator
Narue's Avatar
Narue Narue is online now Online
Expert Meanie

Re: Type initialization

  #8  
Jul 9th, 2008
>It may not work properly on all the compilers.
Please quote chapter and verse from the standard that proves this.
Member of: Beautiful Code Club.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb C++ Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the C++ Forum

All times are GMT -4. The time now is 9:54 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC