Thread: Doubt on Pointer in C++

  1. #1
    Registered User
    Join Date
    Jan 2015
    Posts
    8

    Doubt on Pointer in C++

    Code:
    #include <iostream>
    using namespace std;
    class A{
    
    
    
    };
    
    int main(){
    
    
        A a;
        int x=sizeof(a);
        cout << "size is "<<x<<endl;
        return 0;
    }
    output: size is 1

    and if i modify it to A *a then the size is 8


    what could be the reason? any reply is appreciated

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    *moved to C++ programming forum*

    Read Stroustrup's answer to the FAQ: Why is the size of an empty class not zero?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    Quote Originally Posted by richard36 View Post
    Code:
    #include <iostream>
    using namespace std;
    class A{
    
    
    
    };
    
    int main(){
    
    
        A a;
        int x=sizeof(a);
        cout << "size is "<<x<<endl;
        return 0;
    }
    output: size is 1

    and if i modify it to A *a then the size is 8


    what could be the reason? any reply is appreciated
    In addition to laser's link about why empty structures are at least 1 byte, you're printing out the size of a pointer on your system. This will return 8 for all data types. It's because on your system, addresses to data are stored with 8 bytes.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by MutantJohn View Post
    This will return 8 for all data types. It's because on your system, addresses to data are stored with 8 bytes.
    First and foremost, this is not true because all pointers are not guaranteed to be of the same size.
    And you probably meant all pointer types.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    Quote Originally Posted by Elysia View Post
    First and foremost, this is not true because all pointers are not guaranteed to be of the same size.
    And you probably meant all pointer types.
    Ah yes, I did (with regards to all pointer types).

    And are you sure? All pointers won't be the same size? I've never heard that before. Do you have any sample code?

    Edit : I googled it and it seems like only old or obscure compilers have varying pointer sizes.

    Nevertheless, that is still a very important distinction so thank you anyway.
    Last edited by MutantJohn; 01-14-2015 at 01:55 PM.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by MutantJohn View Post
    And are you sure? All pointers won't be the same size? I've never heard that before. Do you have any sample code?
    Do I have any code? Not really.
    See c - Are all data pointers of the same size in one platform? - Stack Overflow.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointer doubt
    By ankur0691 in forum C Programming
    Replies: 3
    Last Post: 06-13-2013, 11:14 AM
  2. Pointer doubt
    By Gil Carvalho in forum C Programming
    Replies: 37
    Last Post: 06-18-2012, 04:49 AM
  3. Doubt if this is bad pointer
    By gusth in forum C Programming
    Replies: 2
    Last Post: 09-08-2011, 12:24 AM
  4. Doubt in pointer.
    By shwetha_siddu in forum C Programming
    Replies: 5
    Last Post: 03-21-2009, 01:28 AM
  5. Doubt regarding pointer
    By karthik537 in forum C Programming
    Replies: 7
    Last Post: 01-21-2009, 09:53 AM

Tags for this Thread