sizeof (long double)

Discussion in 'C++' started by K4 Monk, Feb 18, 2011.

  1. K4 Monk

    K4 Monk Guest

    #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;
    }
     
    K4 Monk, Feb 18, 2011
    #1
    1. Advertising

  2. K4 Monk <> 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/
     
    Richard Kettlewell, Feb 18, 2011
    #2
    1. Advertising

  3. K4 Monk

    K4 Monk Guest

    On Feb 18, 9:08 pm, Richard Kettlewell <> wrote:
    > K4 Monk <> 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...
     
    K4 Monk, Feb 18, 2011
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. ferran
    Replies:
    9
    Views:
    3,116
    Kevin Goodsell
    Apr 12, 2004
  2. JKop
    Replies:
    4
    Views:
    16,570
  3. Sydex
    Replies:
    12
    Views:
    6,646
    Victor Bazarov
    Feb 17, 2005
  4. Michael Mair

    Typecast long double->double seems to go wrong

    Michael Mair, May 24, 2004, in forum: C Programming
    Replies:
    4
    Views:
    694
    Michael Mair
    May 24, 2004
  5. Daniel Rudy

    unsigned long long int to long double

    Daniel Rudy, Sep 19, 2005, in forum: C Programming
    Replies:
    5
    Views:
    1,246
    Peter Shaggy Haywood
    Sep 20, 2005
Loading...

Share This Page