Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Initialize to zero arrays allocated with new

Posted on 2001-08-09
17
590 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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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
 

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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

792 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