vector<string> 에러관련

zooloo의 이미지

#include <iostream>
#include <string>
#include <vector>
 
using namespace std;
int main()
{
 vector<string> vs = new vector<string>;
 cout << "vector<string>.max_size() : " vector<string>->max_size();
 // vector<string>.max_size : 178956970
 
 cont int loop_count = 20000000;
 string tmp_str = "가나다라마바사아자abcdefghi1234567";
 for (int i = 0; i < loop_count; i++) {  // error
  vs->push_back(tmp_str);
 }
 vs->clear();
 delete vs;
 
 return 0;
}

error 부분이 std::bad_alloc 을 throw 합니다
loop 를 천만번은 돌려도 에러가 안나는데 2천만번은 에러납니다
맥스사이즈만큼 안되는건지요?
이거 버그인지 아니면 os에 따라 컴퓨터에 따라 map_size가 틀린건지
아시는분 힌트좀 주십시오

zooloo의 이미지

map_size 가 아니라 max_size 오타..

zooloo의 이미지

아.. 제가 vector<>.max_size() 에 대해 오해한것 같습니다.
이거 원.. 질문글 지우질 못하니...

세벌의 이미지

질문 글 못 지우는 건 kldp 설정이 그래요.
원 글에서 수정하기 탭 누르면 자기가 쓴 글 수정할 수는 있어요. 지우는 건 안 되지만...

shint의 이미지

https://en.cppreference.com/w/cpp/container/vector

#include <iostream>
#include <string>
#include <vector>
 
using namespace std;
int main()
{
 vector<string>* vs = new vector<string>;
 cout << "vector<string> vs->max_size() : " << vs->max_size() << endl;
 cout << "vector<string> vs->size() : " << vs->size() << endl;
 
 const int loop_count = 2000000;
 string tmp_str = "가나다라마바사아자abcdefghi1234567";
 for (int i = 0; i < loop_count; i++)
 {
  vs->push_back(tmp_str);
 }
 cout << "vector<string> vs->size() : " << vs->size() << endl;
 vs->clear();
 cout << "vector<string> vs->size() : " << vs->size() << endl;
 delete vs;
 
 return 0;
}
 
//
vector<string> vs->max_size() : 576460752303423487
vector<string> vs->size() : 0
vector<string> vs->size() : 2000000
vector<string> vs->size() : 0

----------------------------------------------------------------------------
젊음'은 모든것을 가능하게 만든다.

매일 1억명이 사용하는 프로그램을 함께 만들어보고 싶습니다.
정규 근로 시간을 지키는. 야근 없는 회사와 거래합니다.

각 분야별. 좋은 책'이나 사이트' 블로그' 링크 소개 받습니다. shintx@naver.com

zooloo의 이미지

이분 꾸준하시군요..
아직까지도...

많은 도움이 되었습니다.