Zero values

C

cppquester

I am forced to work with a

char* buf;

I allocate memory and then construct some
data types in buf (several different at different locations of buf
kind of struct alike)

Sometimes all data need to be zeroed.
For integral types that is fine, I just zero all
char within buf.

Q: What about float, double, complex,
can I assume that a bit pattern of only 0s
corresponds to zero values also for these types?

What about std::string?
Is all zero bit pattern an empty string?

If not in the standard, than at least "de-facto"?
Or for g++ on i386?

My program does not need to work on "exotic" systems and
just zeroing the char array saves lot of resources.

Thanks,
Marc
 
S

Spidey07

you are using a character array
and since you specified you alloacted it
you are aware of the size

just go through each allocated space and make the content 0
 
C

cppquester

you are using a character array
and since you specified you alloacted it
you are aware of the size

just go through each allocated space and make the content 0

That is what my question is about:
Is this necessary?
a simple

for( long i=0; i<bufSize; ++i) buf[ i] = 0;

is faster and way easier to write.
 
G

Guest

I am forced to work with a

char* buf;

I allocate memory and then construct some
data types in buf (several different at different locations of buf
kind of struct alike)

If you want a struct, use a struct.
Sometimes all data need to be zeroed.
For integral types that is fine, I just zero all
char within buf.

Q: What about float, double, complex,
can I assume that a bit pattern of only 0s
corresponds to zero values also for these types?

If you really, really must do it that way then you should get a pointer
to the first char representing the type, then cast that pointer to a
pointer of the type (such as a pointer to a double) and then use that
pointer to zero the value.

So if you have first an int, then a double you would do something like this:

char* ptr = new char[sizeof(int) + sizeof(double)];

int* iptr = reinterpret_cast<int*>(ptr);
*iptr = 0;

What about std::string?
Is all zero bit pattern an empty string?

You can not use this for anything but built-in types (and probably POD
types also) but not std::string (and probably not std::complex either).
You could use placement new to construct such types into place, but you
have to remember to destruct them when you are done.

You should probably wrap all of this with a class and make all accesses
through that class.
 
R

Ron AF Greve

Hi,

Well on linux/g++ it as least works for float and double (and I think on all
regular other systems). I tested it by doing the reverse (storing zero and
checking the bytes, see below).

I wouldn't do it myself on a class structure (like complex<> ) though. So
with floats and doubles in your own allocated memory a

char a* Buf;

// Allocate memory to buf for BufSize ....

memset( Buf, 0, BuffSize );

Should do.

Regards, Ron AF Greve




#include <iostream>
#include <iomanip>

using namespace std;



int main(int ArgC, char *ArgV[] )
{
double Double = 0.0;
float Float = 0.0;

cout << hex;

for( char* DPtr = (char*)&Double; DPtr != (char*)&Double + sizeof(
Double ); ++DPtr )
{
cout << (unsigned int)*DPtr << endl;
}
for( char* FPtr = (char*)&Float; FPtr != (char*)&Float + sizeof( Float );
++FPtr )
{
cout << (unsigned int)*FPtr << endl;
}

return 0;

}

Output 8 zero's for double and 4 for float.

[moonlit@informationsuperhighway test]$ ./a.out
0
0
0
0
0
0
0
0
0
0
0
0
[moonlit@informationsuperhighway test]$

Regards, Ron AF Greve

http://www.InformationSuperHighway.eu
 
K

Kai-Uwe Bux

I am forced to work with a

char* buf;

I allocate memory and then construct some
data types in buf (several different at different locations of buf
kind of struct alike)

Sometimes all data need to be zeroed.
For integral types that is fine, I just zero all
char within buf.

Q: What about float, double, complex,
can I assume that a bit pattern of only 0s
corresponds to zero values also for these types?

It is not guaranteed, but it may happen.

What about std::string?
Is all zero bit pattern an empty string?

No way.
If not in the standard, than at least "de-facto"?
Or for g++ on i386?

My program does not need to work on "exotic" systems and
just zeroing the char array saves lot of resources.

You can check yourself:


#include <iostream>
#include <string>
#include <cstring>
#include <iomanip>
#include <vector>

template < typename T >
void test_T ( void ) {
std::cout << std::dec << sizeof(T) << " ";
std::vector< unsigned char > buffer ( sizeof(T), 0 );
T t = T();
std::memcpy( &buffer[0], &t, sizeof(T) );
for ( std::vector< unsigned char >::const_iterator iter = buffer.begin();
iter != buffer.end(); ++iter ) {
std::cout << std::hex << std::setw(2) << std::setfill('0')
<< unsigned( *iter );
}
std::cout << '\n';
}

int main ( void ) {
test_T< float >();
test_T< double >();
test_T< long double >();
test_T< std::string >();
}



But note: Programming outside the guarantees of the standard is tricky and
requires careful code management. If you think it will be less work, you
are deluding yourself. In order to leave an acceptable situation for the
maintenance programmer who has to work on your code, you will need to
document all the steps outside the standard carefully, supply all the test
cases necessary to make sure that one will notice if the next compiler /
library update breaks something, and you have to document the rationale for
going outside the standard so that the decision can be revisited
periodically. All in all, it never saves labor when done properly (but
sometimes it is necessary, e.g., for performance reasons).



Best

Kai-Uwe Bux
 
A

Andrew Koenig

Sometimes all data need to be zeroed.
For integral types that is fine, I just zero all
char within buf.
Q: What about float, double, complex,
can I assume that a bit pattern of only 0s
corresponds to zero values also for these types?

Yes, you can. Whether that assumption is correct is another matter entirely
:)

More seriously, on all implementations of which I am aware, a bit pattern of
all 0's corresponds to 0 for every integral and floating-point type. This
correspondence is a requirement for IEEE754 floating-point. Nevertheless, I
think it is a bad idea to take advantage of it.
What about std::string?
Is all zero bit pattern an empty string?

Absolutely not. Don't even think about it!
 
A

Anand Hariharan

More seriously, on all implementations of which I am aware, a bit pattern of
all 0's corresponds to 0 for every integral and floating-point type. This
correspondence is a requirement for IEEE754 floating-point.

I haven't read the IEE754 spec in detail, but my understanding was
that an all bits zero per the IEEE spec meant an extremely small
value, and that a zero value could not be represented at all. Looks
like I have misunderstood?

- Anand
 
J

Jack Klein

I haven't read the IEE754 spec in detail, but my understanding was
that an all bits zero per the IEEE spec meant an extremely small
value, and that a zero value could not be represented at all. Looks
like I have misunderstood?

Yes, you have misunderstood. An exponent of 0 with a fraction of
exactly 0 represents a value of 0, either positive or negative 0
depending on the sign bit. So ALL bits 0 represents +0.0.

An exponent of 0 with a non-zero fraction represents a denormalized
number, which is I think what you mean by "extremely small".

At first glance, the introduction at
http://en.wikipedia.org/wiki/IEEE_floating-point_standard seems
reasonable, although Wikipedia always needs to be taken with a grain
of salt.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
 
A

Anand Hariharan

Yes, you have misunderstood. An exponent of 0 with a fraction of
exactly 0 represents a value of 0, either positive or negative 0
depending on the sign bit. So ALL bits 0 represents +0.0.

An exponent of 0 with a non-zero fraction represents a denormalized
number, which is I think what you mean by "extremely small".

At first glance, the introduction athttp://en.wikipedia.org/wiki/IEEE_floating-point_standardseems
reasonable, although Wikipedia always needs to be taken with a grain
of salt.

Thank you, Jack.

My misunderstanding might have come from the oft-repeated advice of
"Don't use calloc" given in specific context to allocating dynamic
memory for either floating point numbers or for pointers. In the
case of the latter, it meant that an all-zero bit representation
need not correspond to a NULL or 0 value.

- Anand
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,070
Latest member
BiogenixGummies

Latest Threads

Top