anasagasti
asked on
Initialize to zero arrays allocated with new
I would like to know an easy way to initialize to zero the arrays(int, long, double,...) created with new
Example:
pd_myvar = new double [l_numelements];
Obviously, one way is a loop and going element by element, but I don't want to fill the code with loops just for initialize the arrays.
In C, I used calloc();, and in some cases, memset();, but I'm not sure if the memory allocated by new is guaranteed to be in a continuous memory position.
I'm sure one way is overloading the new[] operator but I'm afraid of not doing correctly.
Thanks.
Example:
pd_myvar = new double [l_numelements];
Obviously, one way is a loop and going element by element, but I don't want to fill the code with loops just for initialize the arrays.
In C, I used calloc();, and in some cases, memset();, but I'm not sure if the memory allocated by new is guaranteed to be in a continuous memory position.
I'm sure one way is overloading the new[] operator but I'm afraid of not doing correctly.
Thanks.
I was hoping the site change might have destroyed that post :( Ignore it, of course.
<Head hung in shame>
<Head hung in shame>
In exchange, this is the way I do it:
std::vector<double> d_myvar(l_numelements,0);
or:
std::vector<double> d_myvar;
d_myvar = std::vector<double>(l_nume lements,0) ;
std::vector<double> d_myvar(l_numelements,0);
or:
std::vector<double> d_myvar;
d_myvar = std::vector<double>(l_nume
or, if you use arrays and not STL containers, simply:
int arr* = new int[10];
memset(&arr[0], 0, sizeof(int) * 10);
or on Windows:
ZeroMemory(&arr[0], sizeof(int) * 10);
int arr* = new int[10];
memset(&arr[0], 0, sizeof(int) * 10);
or on Windows:
ZeroMemory(&arr[0], sizeof(int) * 10);
Which is entirely wrong, since the question was about
>> initialize to zero the arrays(int, long, double,...)
memsetting an array of floating point elements results in undefined behaviour, since nothing is determined about the format of a floating point types. For non-build in types this method is off course even worse...
>> initialize to zero the arrays(int, long, double,...)
memsetting an array of floating point elements results in undefined behaviour, since nothing is determined about the format of a floating point types. For non-build in types this method is off course even worse...
ASKER
In this sense, the comment by KangRoo confirms what I suspected about using memset for arrays in C++.
Thanks for your answer.
Thanks for your answer.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
In this sense, the comment by KangRoo confirms what I suspected about using memset for arrays in C++.
Thanks for your answer.
Thanks for your answer.
ASKER
This is a comment for IainHere:
In Stroustrup book I think explicity says arrays are not initialized. Also, because of this, I've had problems using Visual C++ compiler allocating arrays for built in types (doubles) with garbage values inside of them.
Thanks.
In Stroustrup book I think explicity says arrays are not initialized. Also, because of this, I've had problems using Visual C++ compiler allocating arrays for built in types (doubles) with garbage values inside of them.
Thanks.
The fact that arrays are not initialized has nothing to do with the fact that the internal representation of the floating types are implementation defined.
Initialize floats and compound types with a for loop or a fill() call.
Initialize integral arrays with memset.
Initialize floats and compound types with a for loop or a fill() call.
Initialize integral arrays with memset.
anasagasti:
>>explicity says arrays are not initialized.
Yes. Please don't rub it in (I did retract that claim).
>>The fact that arrays are not initialized has nothing to do with ...
I don't think that anasagasti is really saying this. What it comes down to is that you need to use a loop to assign the members. It is not possible to initialize them to 0.
So you could do that using AssafLavie's fill() suggestion, or using a for loop, which really amount to the same thing. If you can do so, I would suggest you use the std::vector I mentioned above - again, it will essentially be calling a for loop, but it is prettier (you don't have to explicitly state the loop) and you have the many other benefits of the standard containers (Part 3 of Stroustrup :)
>>explicity says arrays are not initialized.
Yes. Please don't rub it in (I did retract that claim).
>>The fact that arrays are not initialized has nothing to do with ...
I don't think that anasagasti is really saying this. What it comes down to is that you need to use a loop to assign the members. It is not possible to initialize them to 0.
So you could do that using AssafLavie's fill() suggestion, or using a for loop, which really amount to the same thing. If you can do so, I would suggest you use the std::vector I mentioned above - again, it will essentially be calling a for loop, but it is prettier (you don't have to explicitly state the loop) and you have the many other benefits of the standard containers (Part 3 of Stroustrup :)
ASKER
Assaf: ok, sorry for the question but I'm new in C++.
I understand the array is not initialized but because you are calling the constructor of the built in type, in this case double, each element is initialized to zero. But what is the reason for having strange values in the array?
Another question: why not using fill() with integral arrays too?
Thanks
I understand the array is not initialized but because you are calling the constructor of the built in type, in this case double, each element is initialized to zero. But what is the reason for having strange values in the array?
Another question: why not using fill() with integral arrays too?
Thanks
ASKER
Thanks IainHere for your answers.
There's no constructor for built in types. They are simply initialized with new values.
The internal representation of the float types is not defined by the std. So a Zero value float doesn't necessary have to look like all-zeros in memory.
fill is not as efficient as a memset since it is a wrapper to a for loop. When you can use memset (for integral arrays) - use them.
Regardless, there's no reason to use c arrays other than performance. So unless you're writing real-time code you can switch to vector's and such and join the C++ world.
The internal representation of the float types is not defined by the std. So a Zero value float doesn't necessary have to look like all-zeros in memory.
fill is not as efficient as a memset since it is a wrapper to a for loop. When you can use memset (for integral arrays) - use them.
Regardless, there's no reason to use c arrays other than performance. So unless you're writing real-time code you can switch to vector's and such and join the C++ world.
oh, and you don't have to apologize for your questions.
ASKER
Just one more thing IainHere if I can abuse:
If I use the vector defined in the Standar Library, is it compatible if in some place of the code I'm calling a C function (for example, from a dll) that expects an standard array of doubles?
std::vector<double> d_myvar
my_function(d_myvar,....)
the definition of the function in C is:
my_function(double *d_doublearray,....)
Thanks very much.
If I use the vector defined in the Standar Library, is it compatible if in some place of the code I'm calling a C function (for example, from a dll) that expects an standard array of doubles?
std::vector<double> d_myvar
my_function(d_myvar,....)
the definition of the function in C is:
my_function(double *d_doublearray,....)
Thanks very much.
I'm afraid you can't do that. The vector looks after its internals, it would be silly to poke around with them. Even if you passed a pointer to a const set of data, there is no guarantee about the internals of the vector itself (generally, the vector implementation would store the doubles in memory in the same way as an array, so the operation would be safe, but this is not required).
Do nothing :)