Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > sizeof (long double)

Reply
Thread Tools

sizeof (long double)

 
 
K4 Monk
Guest
Posts: n/a
 
      02-18-2011
#include<iostream>
using namespace std;
int main()
{
cout<<sizeof(double)<<" "<<sizeof(long double)<<endl;
return 0;
}

this on my computer prints:
8 16

However, with a different program I wrote to check out all different
types, sizeof(long double) print 10, Why? below is the code

#include<iostream>
#include <climits>
#include <cstdio>
#include <cstring>
#include <typeinfo>
#include <string>
using namespace std;

template <typename T>
void print_representation(T*addr){
unsigned char* chaddr = (unsigned char*)addr;
for(size_t q=0;q<sizeof(T); ++q){
for (int bit=CHAR_BIT;bit > 0 ;--bit){
std::cout << ((*(chaddr+q)&(1U<<(bit-1))) ? 1 : 0);
}
}
std::cout<<std::endl;

}

template <typename T>
void print(T* addr, const string& s="")
{
unsigned char* chaddr=reinterpret_cast<unsigned char*> (addr);
cout<<"address of pointer "<<std::hex<<addr;
cout<<" sizeof object: "<<sizeof(*addr);
cout<<" type of object: "<<typeid(*addr).name()<<" "<<s<<endl;

}

int main()
{
//char types
bool b=1;
print<bool> (&b);

char c='A';
print<char> (&c);

short s=1;
print<short> (&s);

short int si=2;
print<short int> (&si);

int i=10;
print<int> (&i);

long l=5;
print<long> (&l);

long int li=6;
print<long int> (&li);


/*
enum color { blue, red, white };
color c1 = blue;
print<enum> (&c1);
*/

// unsigned types

unsigned char c1='B';
print<unsigned char> (&c1);

unsigned short us=10;
print<unsigned short> (&us);

unsigned short int usi=11;
print<unsigned short int> (&usi);

unsigned int ui=11;
print<unsigned int> (&ui);

unsigned long ul=12;
print<unsigned long> (&ul);

unsigned long int uli=14;
print<unsigned long int> (&uli);

//floating point types
float f=3.1415;
print<float> (&f);

double d=2.71828;
print<double> (&d);

long double ld=1.414214;
print<long double> (&ld);

cout<<sizeof(long double)<<endl;

return 0;
}


 
Reply With Quote
 
 
 
 
Richard Kettlewell
Guest
Posts: n/a
 
      02-18-2011
K4 Monk <(E-Mail Removed)> writes:
> However, with a different program I wrote to check out all different
> types, sizeof(long double) print 10, Why? below is the code
>

[...]
> cout<<"address of pointer "<<std::hex<<addr;

^^^^^^^^

You asked for the answer in base 16.

--
http://www.greenend.org.uk/rjk/
 
Reply With Quote
 
 
 
 
K4 Monk
Guest
Posts: n/a
 
      02-18-2011
On Feb 18, 9:08*pm, Richard Kettlewell <(E-Mail Removed)> wrote:
> K4 Monk <(E-Mail Removed)> writes:
> > However, with a different program I wrote to check out all different
> > types, sizeof(long double) print 10, Why? below is the code

>
> [...]
> > * *cout<<"address of pointer "<<std::hex<<addr;

>
> * * * * * * * * * * * * * * * * * * *^^^^^^^^
>
> You asked for the answer in base 16.
>
> --http://www.greenend.org.uk/rjk/


wow! Thanks! awesome...
 
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
sizeof(EmptyStruct) in C and C++ (was: Base {}; sizeof(Base) == 1?) Alex Vinokur C Programming 7 08-14-2006 04:57 PM
sizeof( int ) != sizeof( void * ) blufox C Programming 2 05-22-2006 03:25 PM
#define ARR_SIZE sizeof(arr)/sizeof(arr[0]) Vinu C Programming 13 05-12-2005 06:00 PM
sizeof(enum) == sizeof(int) ??? Derek C++ 7 10-14-2004 05:11 PM
sizeof(str) or sizeof(str) - 1 ? Trevor C Programming 9 04-10-2004 05:07 PM



Advertisments