From: maigre_dragon on 15 Dec 2009 18:13 #include <iostream> using namespace std; class Byte { public: Byte(const unsigned char c):ch_(c) {}; protected: friend std::ostream& operator << (std::ostream& out,const Byte& b); private: unsigned char ch_; }; std::ostream& operator << (std::ostream& out,const Byte& b) { for (int i = 7; i >= 0; i--) { if ( b.ch_ & (1 << i) ) out << '1'; else out << '0'; } return out; } template <typename T> class MemoryMap ; template <typename T> std::ostream& operator << (std::ostream& out,MemoryMap <T>& x) ; template <typename T> class MemoryMap { public: MemoryMap(const T& x):data_(x) {}; protected: friend std::ostream& operator << (std::ostream& out, MemoryMap <T>& x) { size_t length=sizeof(x.data_); Byte* p=reinterpret_cast <Byte*>(&x.data_); for (size_t i=0;i <length;i++) { out <<*p++ <<' '; if (i>4 && i%4==0 ) out <<'\n'; } return out; } private: T data_; }; // int main() { char* s="0a"; WKX::MemoryMap<unsigned char> c1='0'; WKX::MemoryMap<unsigned char> c2='a'; WKX::MemoryMap<char*> mm(s); cout<< c1<<'\n'<< c2<<'\n'<< s<<'\t'<< sizeof(char)<<'\t'<< sizeof(s)<<'\t'<< mm<<endl; int x=5; WKX::MemoryMap<int> i(x); cout<<"sizeof(WKX::MemoryMap<int>)="<<sizeof(i)<<"\n" <<x<<"=\n"<<i<<"\n"; WKX::MemoryMap<int> si(-x); cout<<-x<<"=\n"<<si<<"\n"; float y=0.1; WKX::MemoryMap<float> f(y); cout<<"sizeof(WKX::MemoryMap<float>)="<<sizeof(f)<<"\n" <<y<<"=\n"<<f<<"\n"; WKX::MemoryMap<double> d(y); cout<<"sizeof(WKX::MemoryMap<float>)="<<sizeof(d)<<"\n" <<static_cast<double>(y)<<"=\n"<<d<<"\n"; return 0; }
From: Ian Collins on 15 Dec 2009 22:52 maigre_dragon wrote: <snip code> Did you have a question? -- Ian Collins
From: maigre_dragon on 16 Dec 2009 03:31 > // snip code > int main() > { > char* s="0a"; > WKX::MemoryMap<unsigned char> c1='0'; > WKX::MemoryMap<unsigned char> c2='a'; > WKX::MemoryMap<char*> mm(s); > cout<< > c1<<'\n'<< > c2<<'\n'<< > s<<'\t'<< > sizeof(char)<<'\t'<< > sizeof(s)<<'\t'<< //why sizeof(s)==4 (char* s="0a") //result 0a 1 4 00000000 00110001 01000001 00000000 Oops! Why not the result what I know!? > > int x=5; > WKX::MemoryMap<int> i(x); > cout<<"sizeof(WKX::MemoryMap<int>)="<<sizeof(i)<<"\n" > <<x<<"=\n"<<i<<"\n"; //result sizeof(WKX::MemoryMap<int>)=4 5= 00000101 00000000 00000000 00000000 //yes, the result what I think > WKX::MemoryMap<int> si(-x); > cout<<-x<<"=\n"<<si<<"\n"; > //result -5= > 11111011 11111111 11111111 11111111 //OK! I think I need learn complement code! > float y=0.1; > WKX::MemoryMap<float> f(y); > cout<<"sizeof(WKX::MemoryMap<float>)="<<sizeof(f)<<"\n" > <<y<<"=\n"<<f<<"\n"; >//result sizeof(WKX::MemoryMap<float>)=4 0.1= 11001101 11001100 11001100 00111101 WKX::MemoryMap<double> d(y); > cout<<"sizeof(WKX::MemoryMap<float>)="<<sizeof(d)<<"\n" > <<static_cast<double>(y)<<"=\n"<<d<<"\n"; //result sizeof(WKX::MemoryMap<float>)=8 0.1= 00000000 00000000 00000000 10100000 10011001 10011001 10111001 00111111 //OK! I think I need learn float code
From: Antoon on 18 Dec 2009 10:21 "maigre_dragon" <maigre_dragon(a)126.com> schreef in bericht news:hga5to$9dg$1(a)www.shinco.com... > > // > snip code > > int main() > > { > > char* s="0a"; > > WKX::MemoryMap<unsigned char> c1='0'; > > WKX::MemoryMap<unsigned char> c2='a'; > > WKX::MemoryMap<char*> mm(s); > > cout<< > > c1<<'\n'<< > > c2<<'\n'<< > > s<<'\t'<< > > sizeof(char)<<'\t'<< > > sizeof(s)<<'\t'<< //why sizeof(s)==4 (char* s="0a") > //result > 0a 1 4 00000000 00110001 01000001 00000000 > Oops! Why not the result what I know!? Because on your machine the size of a pointer pointing to a char (array) happens to be 4. The size of a pointer is in no way related to the size of the object it points to. Antoon > > > int x=5; > > WKX::MemoryMap<int> i(x); > > cout<<"sizeof(WKX::MemoryMap<int>)="<<sizeof(i)<<"\n" > > <<x<<"=\n"<<i<<"\n"; > //result > sizeof(WKX::MemoryMap<int>)=4 > 5= > 00000101 00000000 00000000 00000000 > //yes, the result what I think > > WKX::MemoryMap<int> si(-x); > > cout<<-x<<"=\n"<<si<<"\n"; > > > //result > -5= > > 11111011 11111111 11111111 11111111 > //OK! I think I need learn complement code! > > float y=0.1; > > WKX::MemoryMap<float> f(y); > > cout<<"sizeof(WKX::MemoryMap<float>)="<<sizeof(f)<<"\n" > > <<y<<"=\n"<<f<<"\n"; > >//result > sizeof(WKX::MemoryMap<float>)=4 > 0.1= > 11001101 11001100 11001100 00111101 > WKX::MemoryMap<double> d(y); > > cout<<"sizeof(WKX::MemoryMap<float>)="<<sizeof(d)<<"\n" > > <<static_cast<double>(y)<<"=\n"<<d<<"\n"; > //result > sizeof(WKX::MemoryMap<float>)=8 > 0.1= > 00000000 00000000 00000000 10100000 10011001 10011001 10111001 00111111 > //OK! I think I need learn float code
From: Francis Glassborow on 18 Dec 2009 13:33
Antoon wrote: > "maigre_dragon" <maigre_dragon(a)126.com> schreef in bericht > news:hga5to$9dg$1(a)www.shinco.com... >>> // >> snip code >>> int main() >>> { >>> char* s="0a"; >>> WKX::MemoryMap<unsigned char> c1='0'; >>> WKX::MemoryMap<unsigned char> c2='a'; >>> WKX::MemoryMap<char*> mm(s); >>> cout<< >>> c1<<'\n'<< >>> c2<<'\n'<< >>> s<<'\t'<< >>> sizeof(char)<<'\t'<< >>> sizeof(s)<<'\t'<< //why sizeof(s)==4 (char* s="0a") >> //result >> 0a 1 4 00000000 00110001 01000001 00000000 >> Oops! Why not the result what I know!? > > Because on your machine the size of a pointer pointing to a char (array) > happens to be 4. > > The size of a pointer is in no way related to the size of the object it > points to. > That is a slight overstatement. With some limitations the size of a pointer can (on some implementations is) different depending on the type. All pointers to class types must be the same size and (I think) layout. But pointers to enums can depend on the underlying type and pointers to builtin types can vary in size. |