Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Is it safe to assume default value from dynamic memory allocation?

Reply
Thread Tools

Is it safe to assume default value from dynamic memory allocation?

 
 
howa
Guest
Posts: n/a
 
      12-02-2006
e.g.

#include<iostream>

using namespace std;

int main() {

double *d = new double[100];

d[1] = d[0] + 1;
cout<<d[1]<<endl;
}

since i don't get any warning...so

is it safe to assume d[0..99] will be initialized to 0 at the begining?

 
Reply With Quote
 
 
 
 
Robert Bauck Hamar
Guest
Posts: n/a
 
      12-02-2006
howa wrote:
> e.g.
>
> #include<iostream>
>
> using namespace std;
>
> int main() {
>
> double *d = new double[100];
>
> d[1] = d[0] + 1;
> cout<<d[1]<<endl;
> }
>
> since i don't get any warning...so
>
> is it safe to assume d[0..99] will be initialized to 0 at the begining?
>


No.

--
rbh
 
Reply With Quote
 
 
 
 
howa
Guest
Posts: n/a
 
      12-02-2006

Robert Bauck Hamar ¼g¹D¡G

> howa wrote:
> > e.g.
> >
> > #include<iostream>
> >
> > using namespace std;
> >
> > int main() {
> >
> > double *d = new double[100];
> >
> > d[1] = d[0] + 1;
> > cout<<d[1]<<endl;
> > }
> >
> > since i don't get any warning...so
> >
> > is it safe to assume d[0..99] will be initialized to 0 at the begining?
> >

>
> No.
>
> --
> rbh


thanks first...

so how to give them a default value without using a loop?

or it is possible?

 
Reply With Quote
 
Robert Bauck Hamar
Guest
Posts: n/a
 
      12-02-2006
howa wrote:
> Robert Bauck Hamar 寫é“:
>
>> howa wrote:
>>> e.g.
>>>
>>> #include<iostream>
>>>
>>> using namespace std;
>>>
>>> int main() {
>>>
>>> double *d = new double[100];
>>>
>>> d[1] = d[0] + 1;
>>> cout<<d[1]<<endl;
>>> }
>>>
>>> since i don't get any warning...so
>>>
>>> is it safe to assume d[0..99] will be initialized to 0 at the begining?
>>>

>> No.

>
> thanks first...
>
> so how to give them a default value without using a loop?
>
> or it is possible?


First of all. Had you used a class with a default constructor, all
elements had been initialized.

Preferably, use a vector:

#include <vector>
#include <iostream>
#include <ostream> //required for endl!

int main() {
using namespace std;
vector<double> d(100, 0.0);
d[1] = d[0] + 1;
cout << d[1] << endl;
//no need to delete [] d, as you forgot.
}

If you must use an array, you can fill it easily:
#include <algorithm>
....
fill(d, d+sizeof d/sizeof d[0], 0.0)

--
rbh
 
Reply With Quote
 
Alf P. Steinbach
Guest
Posts: n/a
 
      12-02-2006
* howa:
> Robert Bauck Hamar ??:
>
>> howa wrote:
>>> e.g.
>>>
>>> #include<iostream>
>>>
>>> using namespace std;
>>>
>>> int main() {
>>>
>>> double *d = new double[100];
>>>
>>> d[1] = d[0] + 1;
>>> cout<<d[1]<<endl;
>>> }
>>>
>>> since i don't get any warning...so
>>>
>>> is it safe to assume d[0..99] will be initialized to 0 at the begining?
>>>

>> No.
>>
>> --
>> rbh

>
> thanks first...
>
> so how to give them a default value without using a loop?
>
> or it is possible?


#include <iostream> // std::cout
#include <ostream> // operator<<, std::endl
#include <vector> // std::vector

int main()
{
using namespace std;
vector<double> d( 100 );

d.at( 1 ) = d.at( 0 ) + 1;
cout << d.at( 1 ) << endl;
}

Note everything in this program.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
 
Reply With Quote
 
Ivan Novick
Guest
Posts: n/a
 
      12-03-2006
> Alf P. Steinbach wrote:
> #include <iostream> // std::cout
> #include <ostream> // operator<<, std::endl
> #include <vector> // std::vector
>
> int main()
> {
> using namespace std;
> vector<double> d( 100 );
>
> d.at( 1 ) = d.at( 0 ) + 1;
> cout << d.at( 1 ) << endl;
> }
>
> Note everything in this program.

I'll bite. Can you please explain what you have done with this code?
Especially the 3rd line in main?

Thanks,
Ivan
-
http://www.0x4849.net

 
Reply With Quote
 
Salt_Peter
Guest
Posts: n/a
 
      12-03-2006

Ivan Novick wrote:
> > Alf P. Steinbach wrote:
> > #include <iostream> // std::cout
> > #include <ostream> // operator<<, std::endl
> > #include <vector> // std::vector
> >
> > int main()
> > {
> > using namespace std;
> > vector<double> d( 100 );
> >
> > d.at( 1 ) = d.at( 0 ) + 1;
> > cout << d.at( 1 ) << endl;
> > }
> >
> > Note everything in this program.

> I'll bite. Can you please explain what you have done with this code?
> Especially the 3rd line in main?
>
> Thanks,
> Ivan


You can specify an alternate value for the elements when constructing
the std::vector.
The at() member function is a range-checking accessor to the elements
stored in that vector.
Its simply an alternate to op[ ] since op[ ] does no such range check.

In the case you have an unquencheable desire to throw at()'s exception,
capture it in a catch block:

#include <iostream>
#include <ostream>
#include <vector>
#include <iterator> // for std:stream_iterator
#include <stdexcept> // for std::exceptions

int main()
{
std::vector< double > vd(10, 1.1); // 10 doubles, all set to 1.1

// iterate through and stream to cout
// each of the vector's elements
std::copy( vd.begin(),
vd.end(),
std:stream_iterator< double >(std::cout, "\n") );

try {
// there is *no* tenth element
std::cout << vd.at(10) << std::endl;
}
catch( const std::exception& r_e )
{
std::cerr << "Error:";
std::cerr << r_e.what();
std::cerr << std::endl;
}
}

Personally, the try-catch block is a requirement when developing code.

 
Reply With Quote
 
Alf P. Steinbach
Guest
Posts: n/a
 
      12-03-2006
* Ivan Novick:
>> Alf P. Steinbach wrote:
>> #include <iostream> // std::cout
>> #include <ostream> // operator<<, std::endl
>> #include <vector> // std::vector
>>
>> int main()
>> {
>> using namespace std;
>> vector<double> d( 100 );
>>
>> d.at( 1 ) = d.at( 0 ) + 1;
>> cout << d.at( 1 ) << endl;
>> }
>>
>> Note everything in this program.

> I'll bite. Can you please explain what you have done with this code?
> Especially the 3rd line in main?


Ah, yes. It wasn't meant as a mystery. The third line could have been
written as just

d[1] = d[0] + 1;

But in general, operator[] does no bounds checking, so if the indices
are out of bounds (i.e. negative or larger than the vector size) the
result is immediate C++ Undefined Behavior, which is difficult to detect
and debug because anything or nothing might happen.

'at' does bounds checking and throws an exception for out-of-bounds, and
is otherwise the same as operator[]; therefore recommended as default,
writing

d.at(1) = d.at(0) + 1;

unless profiling shows this to be a real bottlneck performancewise,
/and/ a reasonable proof that no out-of-bounds access happens, exists.

The immediate (possible) Undefined Behavior of operator[] is with 'at'
turned into a more well-behaved exception, which is more easily
detectable and easy to debug.

Since this program has no try-catch the formal C++ Undefined Behavior
again rears it ugly head (or would that be "behind"?) at a higher level
-- when the possible exception propagates out of 'main'.

Happily, on most C++ implementations an exception out of 'main' causes
well-defined /implementation defined/ behavior, which we usually call a
"crash"; again, easy to detect and debug. In practice it can be much
easier to detect and debug than if there is a try-catch in 'main'. On
the other hand, for a program distributed to end-users, the end-user
will not necessarily have a debugger or understand a core dump or
whatever a "crash" is on the end-user's machine, so for such a program
it should perhaps be written as

#include <cstddef> // EXIT_SUCCESS, EXIT_FAILURE
#include <stdexcept> // std::exception
...

int main()
{
using namespace std;
try
{
// Former contents of 'main', then
return EXIT_SUCCESS;
}
catch( std::exception const& x )
{
cerr << "!Sorry, I crashed (program failure)." << endl;
cerr << "!Technical info: " << x.what() << endl;
return EXIT_FAILURE;
}
}

And of course that construction can be written once and for all and
reused, just calling a 'cppMain' function (or whatever) from 'main'.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Is there a way to make testbenches assume simulation failure as default? py VHDL 9 11-09-2012 04:04 AM
Date#parse assume a EU format - can it assume a US format? Josh Sharpe Ruby 1 09-21-2010 09:16 AM
Assume program under constant attack Tomás Ó hÉilidhe C Programming 7 01-22-2008 07:46 PM
Can I assume the memory is continuous? linq936 C++ 21 09-19-2007 12:12 PM
The CIT200 VOIP phone I bought will not establish connection to the USB base. It just beeps - busy signal I assume. Carl Rossman VOIP 0 03-16-2007 10:29 PM



Advertisments