Thread: sizeof(String) returns 8/32 on two different compilers.

  1. #1
    Registered User
    Join Date
    Jan 2014
    Posts
    76

    sizeof(String) returns 8/32 on two different compilers.

    Hi, why the same code is having two different outputs (read it further):

    Code:
    #include<iostream>
    #include<string>
    
    
    using std::cin;
    using std::cout;
    using std::string;
    using std::endl;
    
    
    int main()
    {
            string i;
            if(getline(cin,i))
            {
            cout<<sizeof(i)<<endl;
            }
            else
            cout<<"gone";
            return 0;
    }
    ideone gives 32: Is it in Bytes? Can it be possible
    Centos gives 8

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    string is a class, in which the string data is probably dynamically allocated. Therefore sizeof can only get the total size of the class's members.

    The difference in size that you see is caused by a different underlying implementation. And yes, it is in bytes.
    Devoted my life to programming...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 03-06-2012, 04:44 PM
  2. Replies: 22
    Last Post: 07-28-2011, 01:26 PM
  3. sizeof always returns 4.
    By Ploe in forum C Programming
    Replies: 5
    Last Post: 11-10-2009, 08:35 AM
  4. sizeof(array) returns differnt values for same array?
    By Diamonds in forum C++ Programming
    Replies: 6
    Last Post: 02-02-2003, 04:27 PM

Tags for this Thread