jeudi 22 avril 2021

Size of vector returned by sizeof() in C++

I was just experimenting with C++ and here is a code snippet that I executed:

int main() {

std::vector<int> data = { 1, 2, 3, 4, 5 };
std::cout << data.size() << " " << sizeof(data) << " " << sizeof(data[0]) << std::endl;
return 0;

}

The output I get is: 5 16 4 Now I know that size() returns the number of elements in vector ie. 5 here and sizeof() returns the number of bytes occupied by the variable. sizeof(data[0]) makes sense to be 4 bytes as it is an int and visual studio compiler assigns 4 bytes to it but I dont understand why sizeof(data) returns 16. I was calculating 20. Any explanation for this would be helpful.

Aucun commentaire:

Enregistrer un commentaire