I am trying to find the difference in size from a struct with vector of object and struct with a vector of object pointers.
The code I have written shows that size of the both structs are the same even in theory at least based their content they should be different.
What would be the correct way of finding the correct size of a struct based on it's contents?
#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;
}
std::vector<>instance will stay the same, no matter what's stored there. All of the contained data goes to the heap.