sizeof (long double)


K

K4 Monk

#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;
}
 
Ad

Advertisements

R

Richard Kettlewell

K4 Monk said:
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.
 
Ad

Advertisements

K

K4 Monk

K4 Monk said:
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...
 

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

Top