개체의 벡터와 함께 구조체와 개체 포인터의 벡터로 구조체의 크기 차이를 찾으려고합니다.벡터로 구조체의 크기
필자가 작성한 코드는 두 구조체의 크기가 이론적으로는 적어도 내용이 서로 다른 경우에도 동일하다는 것을 보여줍니다.
구조체의 내용을 기준으로 구조체의 정확한 크기를 찾는 올바른 방법은 무엇입니까?
#include <iostream>
#include <string>
#include <vector>
using namespace std;
struct Song{
string Name;
string Artist;
};
struct Folder{
vector<Song*> list;
};
struct Library{
vector<Song> songs;
};
int main(int argc, const char * argv[]) {
Library library;
Folder favorites;
Folder recentPurchaces;
library.songs.push_back(Song{"Human After All", "Daft Punk"});
library.songs.push_back(Song{"All of my love", "Led Zepplin"});
favorites.list.push_back(&library.songs[0]);
favorites.list.push_back(&library.songs[2]);
cout << "Size of library: " << sizeof(library) << endl;
cout << "Size of favorites: " << sizeof(favorites) << endl;
return 0;
}
예상되는 실제 출력은 무엇입니까? – NPE
내 예상 출력은 라이브러리가 즐겨 찾기보다 클 것이지만 둘 다 24 바이트라는 것을 보여줍니다. – user1721803
'std :: vector <>'인스턴스의 크기는 저장되는 내용과 상관없이 동일하게 유지됩니다. 포함 된 모든 데이터는 힙으로 이동합니다. –