Oct 16, 2020 at 5:08pm UTC
In the program I wrote the 'Y' should line up under the 0059 and the '@' should line up under the 0040.
Function address_view prints correctly for int and char, but not for the double.
I really can't understand why, could someone explain what I'm doing wrong?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
#include<iostream>
#include<limits>
#include<cstring>
#include<iomanip>
#include<stdexcept>
using std::cout;
template <typename T>
void address_view(T* var, size_t size) {
cout << "The passed size of var is: " << size << '\n'
<< "The address is: " << std::hex << var << '\n' ;
size = size > 100 ? 100 : size;
char view[100];
std::memcpy(view, var, size);
for (size_t i = 0; i != size ; ++i) cout << std::setw(4) << std::setfill('0' ) << std::hex << static_cast <int16_t>(view[i]) << ' ' ;
cout << '\n' ;
for (size_t i = 0; i != size ; ++i) cout << std::setw(4) << std::setfill(' ' ) <<view[i] << ' ' ;
cout << '\n' ;
}
int main() try {
int d1{0};
int d2{'1' };
int d3{'2' };
int d4{0x59};
int d5{0x40};
char c1{'@' };
const char * wilson = "wilson" ;
double dbl{100};
address_view(&d1, sizeof (d1));
address_view(&d2, sizeof (d2));
address_view(&d3, sizeof (d3));
address_view(&d4, sizeof (d4));
address_view(&d5, sizeof (d4));
address_view(&c1, sizeof (c1));
address_view(wilson, std::strlen(wilson));
address_view(&dbl, sizeof (dbl));
}
catch (std::exception& e) {
cout << "Caught: " << typeid (e).name() << '\n'
<< "Message: " << e.what() << '\n' ;
return 1;
}
this is my output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
The passed size of var is: 4
The address is: 0x7ffed53169c8
0000 0000 0000 0000
The passed size of var is: 4
The address is: 0x7ffed53169c4
0031 0000 0000 0000
1
The passed size of var is: 4
The address is: 0x7ffed53169c0
0032 0000 0000 0000
2
The passed size of var is: 4
The address is: 0x7ffed53169bc
0059 0000 0000 0000
Y
The passed size of var is: 4
The address is: 0x7ffed53169b8
0040 0000 0000 0000
@
The passed size of var is: 1
The address is: @@
0040
@
The passed size of var is: 6
The address is: wilson
0077 0069 006c 0073 006f 006e
w i l s o n
The passed size of var is: 8
The address is: 0x7ffed53169a0
0000 0000 0000 0000 0000 0000 0059 0040
Y @
Last edited on Oct 16, 2020 at 5:09pm UTC
Oct 16, 2020 at 5:42pm UTC
you are trying to print a non-printable character ('\0'
) so nothing is printed
but the filler was already done, to just three spaces instead of four
> The address is: @@
cast `var' to void *
, if you try to print a char *
it will go on until it finds a null terminator.
Oct 16, 2020 at 6:07pm UTC
I'm sorry, I left them in, the chars ints, to show that they're printing correctly, my problem is with the double.
It's address was: 0x7ffed53169a0.
This is the output I don't understand.1 2 3 4
The passed size of var is: 8
The address is: 0x7ffed53169a0
0000 0000 0000 0000 0000 0000 0059 0040
Y @
My suspicion, although I don't know, is the '@@' isn't an address at all. I've been told that sometimes the compiler optimizes some variables away.
Last edited on Oct 17, 2020 at 10:55am UTC
Oct 16, 2020 at 6:17pm UTC
@ness555-- I'm sorry, I just figured out what you're saying. I think I can fix it now. Thank you.
Oct 16, 2020 at 6:34pm UTC
for completeness here is the fixed function. Thank you again.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
template <typename T>
void address_view(T* var, size_t size) {
cout << "The passed size of var is: " << size << '\n'
<< "The address is: " << std::hex << (void *)var << '\n' ;
size = size > 100 ? 100 : size;
char view[100];
std::memcpy(view, var, size);
for (size_t i = 0; i != size ; ++i){
cout << std::setw(4) << std::setfill('0' ) << std::hex << int {view[i]} << ' ' ;
}
cout << '\n' ;
for (size_t i = 0; i != size ; ++i) {
if (view[i] == 0) cout << " " ;
else cout << std::setw(4) << std::setfill(' ' ) <<view[i] << ' ' ;
}
cout << '\n' ;
}
edit: added cast to solve char* address problem.
Last edited on Oct 17, 2020 at 10:47am UTC