FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


[C++] new initializes values?





Peterssidan
I'm a bit confused about when C++ initialize values and when they are unspecified.

If I create an array like this:
Code:
int *arr = new int[100];
all the values in the array will have the value 0. But if I instead create my array like this:
Code:
int arr[100];
the values are varying.

So does an array created with new set all values to zero? Isn't that unnecessary overhead in some cases? Is this behavior specified by some C++ standard or is it only my compiler (g++) that want to make it this way?
AftershockVibe
That doesn't strike me as the sort of thing C++ would do, it would be considered inefficient by the designers. Unless I'm mistaken, you are just being lucky when creating your object.

This can be explained because each statement initialises using different parts of memory. The first creates and object on the heap and the second uses the stack. Generally, the stack is far more volatile than the heap because of the way programs execute (call - push scope onto stack, and return - pop off the stack).

An interesting experiment would to see what happens when you do the following:

Code:

int maxSize = 100;
int *arr = new int [maxSize];

/* Print original contents and change values */
for(int i = 0; i < maxSize; ++i)
{
std::cout << arr[i];
arr[i] = i;
std::cout << std::endl;
}

/* Delete the array (shouldn't zero memory) */
delete[] arr;

/* Initialise a new array on the heap with the same size */
int *arr2 = new int [maxSize ];

/* Print new array contents to see if they are the same as the values you wrote to the old array */
for(int i = 0; i < maxSize; ++i)
{
std::cout << arr2[i];
}


Of course, this may not work if the compiler is doing something funky with heap allocation. Just wondering what would happen...
traxion
Peterssidan wrote:
I'm a bit confused about when C++ initialize values and when they are unspecified.

If I create an array like this:
Code:
int *arr = new int[100];
all the values in the array will have the value 0. But if I instead create my array like this:
Code:
int arr[100];
the values are varying.

So does an array created with new set all values to zero? Isn't that unnecessary overhead in some cases? Is this behavior specified by some C++ standard or is it only my compiler (g++) that want to make it this way?


the reason you get strange default values is because C++ only get a peace of memory and not clean it.

you need make sure the memory is clean, of clean it before you use the variable further up in the program.
Peterssidan
AftershockVibe wrote:
That doesn't strike me as the sort of thing C++ would do, it would be considered inefficient by the designers. Unless I'm mistaken, you are just being lucky when creating your object.

This can be explained because each statement initialises using different parts of memory. The first creates and object on the heap and the second uses the stack. Generally, the stack is far more volatile than the heap because of the way programs execute (call - push scope onto stack, and return - pop off the stack).

An interesting experiment would to see what happens when you do the following:

Code:

int maxSize = 100;
int *arr = new int [maxSize];

/* Print original contents and change values */
for(int i = 0; i < maxSize; ++i)
{
std::cout << arr[i];
arr[i] = i;
std::cout << std::endl;
}

/* Delete the array (shouldn't zero memory) */
delete[] arr;

/* Initialise a new array on the heap with the same size */
int *arr2 = new int [maxSize ];

/* Print new array contents to see if they are the same as the values you wrote to the old array */
for(int i = 0; i < maxSize; ++i)
{
std::cout << arr2[i];
}


Of course, this may not work if the compiler is doing something funky with heap allocation. Just wondering what would happen...


I tested the program and the first array always prints all values as 0. The second array gets the values that was given to the first array. thanks Smile Now I can be sure that I will have to set the values manually.
Related topics
Command and Conquer
script backup database
[java scripts] Text effect , img ....
New Data confirms a big earthquake in Central US
http://muonline.vn --> New server has copyright of Webzen
Hide Drives and Partitions
Creating a new Operating System
Site Nav Suggestions...
New here
C++ assignment help
A new game im making...E.s.c.a.p.e
Cricket World Cup 2006
C++ Program Basics
static keyword in PHP
Reply to topic    Frihost Forum Index -> Scripting -> Others

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.