• 카테고리

    질문 & 답변
  • 세부 분야

    프로그래밍 언어

  • 해결 여부

    미해결

string의 크기

20.11.15 23:15 작성 조회수 1.21k

1

#include  <iostream>

#include  <string>

using namespace std;

struct Person

{

double height;

float weight;

int age;

string name;

void print()

{

cout << height << " " << weight << " " << age << " " << name;

cout << endl;

}

};

int main()

{

cout << sizeof(string) << endl;

cout << sizeof(Person) << endl;

return 0;

}

구조체의 사이즈를 sizeof를 이용해 출력하는 것을 보고 갑자기 string의 사이즈는 얼마인가 궁금해서 sizeof를 이용해 찍어보았는데 40byte가 찍혔습니다.

string의 사이즈는 어떻게 결정이 되는지 알고 싶습니다.

어떻게 40바이트가 찍혔는지 아직 잘 모르겠습니다.

답변 3

·

답변을 작성해보세요.

2

Hello Yeo님의 프로필

Hello Yeo

2020.11.17

안녕하세요?
인내심을 갖고 차근차근 읽어보세요.
링크 글을 읽어보시면 40 bytes에 대한 실마리가 있네요.
아마 x64 platform에 Debug mode 였을 것으로 생각되는군요.

Anatomy of a C++ string is a great question.

In general, a string is effectively a union between
* an array of char and a length
* vector<char> (allocator, begin pointer, end pointer or size, capacity pointer or size)

some of the four elements of the vector usually stick out of the union to be able to tell whether a given string object is using the array or the vector, but see below for a hack used by libc++

There are only three non-obsolete C++ standard library implementations, here's what they do:

MSVC 2015 for x64 target, Release mode: 32 bytes
0 bytes std::allocator<char> (this allocator is stateless)
16 byte union between:
* a 16-byte char array (to hold small strings, max small string length is 15)
* an 8-byte pointer (to point to long strings)
8 bytes for current length of the string
8 bytes for current capacity of the string

16+8+8 = 32

MSVC 2015 x64 target, Debug mode: 40 bytes (sounds like what you saw)
same as above, except there is an extra 8-byte pointer after the allocator and before the union. This pointer points to something MS calls "container proxy", which is used to do bounds checking on iterators.

GNU libstdc++ 7.1, x64 target: 32 bytes
0 bytes std::allocator<char> (this allocator is stateless)
8 bytes pointer to the beginning of the string (for small strings, it points just 16 bytes down)
8 bytes for current length of the string
16 bytes union between
* a 16-byte char array (to hold small strings, max small string length is 15)
* 8 bytes for current capacity of the string

8+8+16 = 32

LLVM libc++ 4.0, x64 target: 24 bytes
0 bytes std::allocator<char> (this allocator is stateless)
24 bytes union between
* short string representation, consisting of
** one byte size multiplied by 2(!)
** a 23-byte char array (to hold small strings, max small string length is 22)
* long string representation, consisting of
** 8-byte capacity with the least significant bit always set
** 8-byte size
** 8-byte pointer to the start of the string

(to check if the string is long or short, libc++ looks at the least significant bit of the first byte of the union: short string size is always recorded doubled, so for short strings that bit is always clear, while long string capacity always has that put purposefully set)

1+23 == 8 + 8 + 8 == 24


PS "DEV 5.11" is not a compiler, but "8 bytes using sizeof() operator" sounds like it's using a pre-C++11 GNU library, where it was quite different.

0

solitary님의 프로필

solitary

질문자

2020.11.16

음..제가 설명이 부족했던것 같은데 구조체의 사이즈는 56바이트 였고 double 8바이트, float4바이트,  int4바이트, string이 40바이트 로 나옵니다. 참고로 Visual Studio 2019입니다.

링크보내주신 내용이 제가 질문드린 내용과 비슷한 것 같은데 아직 관련 지식이 많이 부족해서 이해하기가 어렵습니다ㅜ

cout << sizeof(string) << endl;

cout << sizeof(Person) << endl;

->

40

56

0

kkkh0712님의 프로필

kkkh0712

2020.11.16

아마 구조체의 사이즈가 40바이트였을 것으로 추측됩니다. string의 사이즈는 컴파일러마다 다르지만, 24바이트이거나 32바이트입니다. 스트링은 짧은 길이의 문자열에 대해선 char형 배열로 저장하고, 긴 길이의 문자열에 대해선 동적으로 char 타입 배열을 할당받습니다. 따라서 string은 (동적할당을 위한 포인터 or char형 배열) + size + capacity로 구성되기 때문에 string의 크기는 24바이트 또는 32바이트입니다. 자세한 내용은 http://www.cplusplus.com/forum/general/218642/ 참고 부탁드립니다.