sizeof continues to return 4 instead of actual size

前端 未结 5 1140
孤街浪徒
孤街浪徒 2020-12-21 04:07
#include 

using namespace std;

int main()
{
    cout << \"Do you need to encrypt or decrypt?\" << endl;
    string message;
    getline         


        
相关标签:
5条回答
  • 2020-12-21 04:16

    You want to use message.size() not sizeof(message).

    sizeof just gives the number of bytes in the data type or expression. You want the number of characters stored in the string which is given by calling size()

    Also indexing starts at 0, notice I changed from 1 to 0 below.

    for (int place = 0; place < message.size(); place++)
    {
        letter2number = static_cast<int>(message[place]);
        cout << letter2number << endl;
    }
    

    Any pointer on an x86 system is only 4 bytes. Even if it is pointing to the first element of an array on the heap which contains 100 elements.

    Example:

    char * p = new char[5000];
    assert(sizeof(p) == 4);
    

    Wrapping p in a class or struct will give you the same result assuming no padding.

    0 讨论(0)
  • 2020-12-21 04:18

    sizeof returns the size of an expression. For you, that's a std::string and for your implementation of std::string, that's four. (Probably a pointer to the buffer, internally.)

    But you see, that buffer is only pointed to by the string, it has no effect on the size of the std::string itself. You want message.size() for that, which gives you the size of the string being pointed to by that buffer pointer.

    As the string's contents change, what that buffer pointer points to changes, but the pointer itself is always the same size.


    Consider the following:

    struct foo
    {
        int bar;
    };
    

    At this point, sizeof(foo) is known; it's a compile-time constant. It's the size of an int along with any additional padding the compiler might add.

    You can let bar take on any value you want, and the size stays the same because what bar's value is has nothing to do with the type and size of bar itself.

    0 讨论(0)
  • 2020-12-21 04:22
    class string
    {
        char * ptr;
        //...
        size_t size();  // return number of chars (until null) in buffer pointed to by ptr
    };
    
    sizeof(message) == sizeof(string) == sizeof(ptr) == 4; // size of the struct
    
    message.size() == number of characters in the message...
    
    0 讨论(0)
  • 2020-12-21 04:24

    sizeof(type) returns the size of the type, not the object. Use the length() method to find the length of the string.

    0 讨论(0)
  • 2020-12-21 04:28
    #include<iostream>
    #include<conio.h>
    using namespace std;
    int main()
    {
    
        cout << "Do you need to encrypt or decrypt?" << endl;
        string message;
        getline(cin, message);
    
        int letter2number;
    
        for (int place = 0; place < message.size(); place++)
        {
            letter2number = static_cast<int>(message[place]);
            cout << letter2number << endl;
        }
    
    
        getch();
        return 0;
    }    
    
    0 讨论(0)
提交回复
热议问题