string의 크기
1871
작성한 질문수 25
#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
안녕하세요?
인내심을 갖고 차근차근 읽어보세요.
링크 글을 읽어보시면 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
음..제가 설명이 부족했던것 같은데 구조체의 사이즈는 56바이트 였고 double 8바이트, float4바이트, int4바이트, string이 40바이트 로 나옵니다. 참고로 Visual Studio 2019입니다.
링크보내주신 내용이 제가 질문드린 내용과 비슷한 것 같은데 아직 관련 지식이 많이 부족해서 이해하기가 어렵습니다ㅜ
cout << sizeof(string) << endl;
cout << sizeof(Person) << endl;
->
40
56
0
아마 구조체의 사이즈가 40바이트였을 것으로 추측됩니다. string의 사이즈는 컴파일러마다 다르지만, 24바이트이거나 32바이트입니다. 스트링은 짧은 길이의 문자열에 대해선 char형 배열로 저장하고, 긴 길이의 문자열에 대해선 동적으로 char 타입 배열을 할당받습니다. 따라서 string은 (동적할당을 위한 포인터 or char형 배열) + size + capacity로 구성되기 때문에 string의 크기는 24바이트 또는 32바이트입니다. 자세한 내용은 http://www.cplusplus.com/forum/general/218642/ 참고 부탁드립니다.
변수가 메모리에 저장되는 것을 알려주는 강의가 어떤강의였죠
1
461
1
메모리 주소 10진수로 출력
1
650
1
클래스 템플릿 특수화에서 boolalpha로 표현된 리턴값에 대해 질문이 있습니다.
1
495
1
여러가지 리턴 타입에 관한 강의가 어떤 걸까요?
1
529
1
메모리 주소에 관한 질분
0
676
1
인터페이스 클래스에서 reportError의 매개변수에 대해 궁금한 것이 있습니다.
0
545
1
형변환 오버로딩에서 const 관련 질문이 있습니다.
0
439
1
Digit 뒤에 reference를 사용하는 이유
0
504
1
4.2 전역 변수, 정적 변수, 내부 연결, 외부 연결
0
319
1
dat파일이...
0
534
1
TODO:대입 연산자 오버로딩에 대한 소스코드입니다.
0
640
1
복사 생성자 관련 질문이 있습니다.
0
450
1
수업 중 궁금한점이 있습니다.
1
386
1
라이브러리자체가 이해가 되지 않습니다.
0
557
1
마지막 예제 질문
0
299
1
증감연산자 위치에 따른 수행 순서 질문입니다.
0
371
1
단항 연산자 오버로딩에서 return 부분에 질문이 있습니다.
1
408
1
friend함수 관련 질문이 있습니다.
0
308
1
operator+ 정의부분에서 궁금한 것이 있습니다.
0
443
1
3분 17초 질문
0
346
1
함수에 값을 대입한다는 개념이 이해가 되지 않습니다.
0
442
1
int getvalue() const에서 const는 왜 뒤에 붙는건가요?
0
440
2
const Something &st에서 const를 빼면 안되나요?
0
296
1
friend함수는 다른 클래스의 멤버함수로 쓸 수 없나요??
1
489
1





