•
•
•
•
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
![]() |
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
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.
•
•
Join Date: Aug 2007
Posts: 128
Reputation:
Rep Power: 2
Solved Threads: 13
I guess not - it still has garbage.
Did this test.
but like everything in c/c++ I'm sure it's compiler-dependant.
Did this test.
c++ Syntax (Toggle Plain Text)
#include <cstdlib> #include <iostream> using namespace std; int main(int argc, char *argv[]) { int *nums = new int[10]; for(int i=0;i<10;++i) cout << i << ": " << nums[i] << endl; delete [] nums; system("PAUSE"); return EXIT_SUCCESS; }
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. :)
•
•
Join Date: Oct 2007
Location: Cherry Hill, NJ
Posts: 1,809
Reputation:
Rep Power: 11
Solved Threads: 184
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:
Hope this helps.
You can use the STL fill_n() function to initialize it for you:
C++ Syntax (Toggle Plain Text)
#include <algorithm> // fill_n() and copy() #include <iostream> // cout, of course #include <iterator> // ostream_iterator<> #include <limits> // numeric_limits<> using namespace std; int main() { int *nums = new int[ 10 ]; cout << "Before initialization: "; copy( nums, nums +10, ostream_iterator <int> ( cout, " " ) ); cout << endl; fill_n( nums, 10, 42 ); cout << "After initialization: "; copy( nums, nums +10, ostream_iterator <int> ( cout, " " ) ); cout << endl; cout << "Press ENTER to finish"; cin.ignore( numeric_limits <streamsize> ::max(), '\n' ); delete[] nums; return 0; }
>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:
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:
cplusplus Syntax (Toggle Plain Text)
int *nums = new int[10]();
Member of: Beautiful Code Club.
•
•
•
•
But you can force default initialization with an explicit empty parameter list:
cplusplus Syntax (Toggle Plain Text)
int *nums = new int[10]();
That is a neat trick. Thanks.
![]() |
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
•
•
•
•
•
•
•
•
DaniWeb C++ Marketplace
Similar Threads
- Data type initialization error (C++)
- Initialization Lists (C++)
- Linker Error in derived class initialization (C++)
- template array initialization (C++)
- Dynamic Initialization implies Dynamic Help (C++)
- Need Help with variable initialization (Java)
- Helping for initialization (C++)
Other Threads in the C++ Forum
- Previous Thread: problem in accessing methods from VC++
- Next Thread: Problem with CString.Format



Linear Mode