Solved

Initialize to zero arrays allocated with new

Posted on 2001-08-09
17
585 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
VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

 
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

Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Handle Exceptions during instantiation 28 415
How to split this in C++ 4 102
C++ Class Serialization, Encapsulation and Formatted Output 8 68
Problem with SqlConnection 4 169
Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

777 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