Solved

Initialize to zero arrays allocated with new

Posted on 2001-08-09
17
579 Views
Last Modified: 2008-02-20
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.
0
Comment
Question by:anasagasti
  • 6
  • 5
  • 5
  • +1
17 Comments
 
LVL 4

Expert Comment

by:IainHere
ID: 6367414
The default constructor for a built in type performs initialization to 0.  That constructor is called by new()[].

Do nothing :)
0
 
LVL 4

Expert Comment

by:IainHere
ID: 6367422
I was hoping the site change might have destroyed that post :( Ignore it, of course.

<Head hung in shame>
0
 
LVL 4

Expert Comment

by:IainHere
ID: 6367441
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_numelements,0);
0
 
LVL 4

Expert Comment

by:AssafLavie
ID: 6367504
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);

0
 
LVL 7

Expert Comment

by:KangaRoo
ID: 6367536
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...
0
 

Author Comment

by:anasagasti
ID: 6367572
In this sense, the comment by KangRoo confirms what I suspected about using memset for arrays in C++.

Thanks for your answer.
0
 
LVL 4

Accepted Solution

by:
AssafLavie earned 75 total points
ID: 6367589
then use fill() for float types:
fill(&arrayodDouble[0], &arrayodDouble[size-1] + 1, 0)
If you dislike writing for loops so much...
0
 

Author Comment

by:anasagasti
ID: 6367759
In this sense, the comment by KangRoo confirms what I suspected about using memset for arrays in C++.

Thanks for your answer.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:anasagasti
ID: 6367868
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.
0
 
LVL 4

Expert Comment

by:AssafLavie
ID: 6367892
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.
0
 
LVL 4

Expert Comment

by:IainHere
ID: 6367936
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 :)
0
 

Author Comment

by:anasagasti
ID: 6367955
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
0
 

Author Comment

by:anasagasti
ID: 6367970
Thanks IainHere for your answers.
0
 
LVL 4

Expert Comment

by:AssafLavie
ID: 6368022
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.
0
 
LVL 4

Expert Comment

by:AssafLavie
ID: 6368024
oh, and you don't have to apologize for your questions.
0
 

Author Comment

by:anasagasti
ID: 6368030
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.
0
 
LVL 4

Expert Comment

by:IainHere
ID: 6368113
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).
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now