0

when I try to take the element by sizeof , then I am getting different answers in array and vector

#include<iostream>
#include<vector>
using namespace std;

int main(){
    vector<int> num = {1,2,5,4};
    int k = sizeof(num)/sizeof(num[0]);
    cout << k << endl;
    int nums[] = {1,2,5,4};
    int w = sizeof(nums)/sizeof(nums[0]);
    cout << w << endl; 
}


Expected 4 4

Output 3 4

iabhishek15
  • 343
  • 1
  • 2
  • 9
  • 1
    The size of the vector is not dependent on the number of elements. – David G Oct 15 '19 at 17:04
  • why did you exect that output? Did you read about what `sizeof` does? – 463035818_is_not_an_ai Oct 15 '19 at 17:05
  • 3
    *Expected 4 4* -- It would help if you stated why you expected this output, so that others know where the fault you're making is. – PaulMcKenzie Oct 15 '19 at 17:07
  • The short answer is that `vector` is not an array. It's a reasonable first interpretation that `sizeof(x)/sizeof(x[0])` is how you obtain all container lengths, but it's not true. – Max Langhof Oct 15 '19 at 17:07
  • well i am storing integer in both cases. how is that different size is allocated in two cases ?? – iabhishek15 Oct 15 '19 at 17:09
  • ok, back to the second comment: Do you know what `sizeof` does? A vector *dynamically*, at runtime, creates entries. The `sizeof` knows nothing about what happens at runtime, as it is a compile-time construct. – PaulMcKenzie Oct 15 '19 at 17:10
  • Max Langhof well i was hoping to get the array length by this method. – iabhishek15 Oct 15 '19 at 17:11
  • @iabhishek15 Unlike inbuilt arrays, most C++ containers store the actual data "somewhere else", which `sizeof` cannot know about. `sizeof(x)` is a constant known at compile-time, so it cannot possibly tell you how many elements a container like `vector` contains at runtime. – Max Langhof Oct 15 '19 at 17:12
  • PaulMcKenzie it can be used to get the size of classes, structures, unions and any other user defined data type. well i get the point what you are trying to say .it actually gives only 12 bit of memory for vector. – iabhishek15 Oct 15 '19 at 17:13
  • 2
    @iabhishek15 -- Think of it this way -- the `sizeof` is like knowing how tall a building is. It doesn't tell you how many people are in the building, as that can change. – PaulMcKenzie Oct 15 '19 at 17:14
  • @iabhishek15 _it can be used to get the size of classes,..._ That's exactly what you get. `sizeof(num)` is a byte-size of an object of type `std::vector`. Vectors typically consist of 3 pointer member variables. On your architecture, pointers are seemingly 32-bit, therefore `sizeof(num)` is 12. Then, `sizeof(num[0])` is a byte-size of an object of type `int` which is 4. Finally, 12/4=3, which is a result you get. – Daniel Langr Oct 15 '19 at 17:16
  • Max Langhof , Daniel Langr , PaulMcKezie thnx now i understand . – iabhishek15 Oct 15 '19 at 17:16
  • `struct Holder { char* s; }; Holder holder; holder.s = new char[100]; auto sz = sizeof holder;` ... sz will be the number of bytes of a pointer (and maybe some padding bytes on some platforms). Even though the pointer contained in holder refers to an array of 100 char. – Eljay Oct 15 '19 at 17:17
  • Side note: Arrays are an elegant 1970s solution to vexing 1970 problems. While they are still very useful and form the basis for many other structures, the cracks start showing as soon as you start trying to pass arrays around and assign them. Case in point: the clumsy `sizeof(nums)/sizeof(nums[0])` used to get the number of elements vs the `size` method of a container class. Consider using [`std::array`](https://en.cppreference.com/w/cpp/container/array) unless you really do need the simplicity and light weight of an array. – user4581301 Oct 15 '19 at 17:28

1 Answers1

1

Let's see what sizeof returns:

sizeof Queries size of the object or type.

When applied to a class type, the result is the size of an object of that class plus any additional padding required to place such object in an array.


vector<int> and int [] are totally different things. Vector uses dynamic storage while array uses automatic storage.

sizeof(vector) return the size of vector class. It doesn't work the way you expect for the same reason it won't work on a dynamiclly allocated array.

Oblivion
  • 7,176
  • 2
  • 14
  • 33