-2

When I execute the following statement:

vector <int> v;

What exactly will the value of v be?

Will it just be a pointer that points to the start of a memory block? Will its value be NULL?

Some points to possible duplicates to another question which is more complicated than this one, and focus less on the comparison of 1) calling the default constructor of vector class and 2) initialization of an array which I believe is a pointer to int.

yuqli
  • 4,461
  • 8
  • 26
  • 46
  • 1
    A) The value is an empty vector B) Why do you need to know if the internal pointer is null or not? – Borgleader Aug 12 '18 at 21:59
  • @Borgleader Maybe because the OP is used to C? – eesiraed Aug 12 '18 at 22:00
  • 1
    It's value will be an instance of the type `vector `. It does contain a pointer of type `int` which will be set to `nullptr` (and can later point to a buffer if the vector allocates some memory for `int`s) but you don't need to worry about that when using it. For more information on the interface `std::vector` provides, please see the [docs](https://en.cppreference.com/w/cpp/container/vector). – George Aug 12 '18 at 22:00
  • An vector with a size of 0 and a capacity of 0 or more. How that is managed internally is implementation defined. – Retired Ninja Aug 12 '18 at 22:02
  • The interesting point here is: what would `data` return? Implementation defined as well? – Aconcagua Aug 12 '18 at 22:11
  • 1
    @Aconcagua Answered [here](https://stackoverflow.com/q/25419851/509868) – anatolyg Aug 12 '18 at 22:15
  • @Aconcagua according to https://en.cppreference.com/w/cpp/container/vector/data If size() is 0, data() may or may not return a null pointer. – vdavid Aug 12 '18 at 22:15
  • Rolled back the edit: Firstly, please don't edit the question to include the answer. Instead, you can accept one of the posted answers. Secondly, if you have a new question then post a new question. This site uses a Question/Answer format (with one question per question) – M.M Aug 12 '18 at 22:38
  • Possible duplicate of [C++ - value of uninitialized vector](https://stackoverflow.com/questions/5222404/c-value-of-uninitialized-vectorint) – rsjaffe Aug 12 '18 at 23:46
  • @rsjaffe This is not a duplicate of the issue you mentioned. – vdavid Aug 13 '18 at 08:57

3 Answers3

1

Your syntax will call the constructor with no parameters, also known as the default constructor. According to the std::vector constructor documentation, you can see that it will create an empty vector.

The pointer where it points to does not matter since you are not supposed to dereference its values while the container is empty. Please note that if you want to store the value of the internal pointer, such as std::vector::data(), it may change anytime you add an element to the vector (well, technically, you can predict when the pointer will change, but it’s a good practice to do as if the pointer always changes).

vdavid
  • 2,434
  • 1
  • 14
  • 15
1

What exactly will the value of v be?

  • pointer? - no.

  • NULL - no.

  • nullptr - no.

v is an instance of class std::vector<T> (where T is int).

On Ubuntu Linux 64 bit, a "std::vector<T> tVec;" occupies 24 bytes regardless of

  • sizeof(T),

or

  • number of elements.

The guts of the object are not similar to an array of int, but the implementation does maintain an array of T, probably in dynamic memory.

For each compiler, the implementation may vary.

eerorika
  • 232,697
  • 12
  • 197
  • 326
2785528
  • 5,438
  • 2
  • 18
  • 20
0
vector <int> v;

What exactly will the value of v be?

That is the syntax of default initialisation. Therefore the object will be in a default initialised state. For class types such as std::vector, default initialisation calls the default constructor. An online reference describes the default constructor of vector thusly:

1) Default constructor. Constructs an empty container. If no allocator is supplied, allocator is obtained from a default-constructed instance.


Will it just be a pointer that points to the start of a memory block? Will its value be NULL?

A vector is not a pointer.

Among other members, a vector implementation does contain a pointer which may point to a buffer that the vector manages - you can get a copy of that pointer using the std::vector::data member function. The state of the internal pointer of a default initialised vector is unspecified. Since an empty vector does not need a buffer, that pointer may be null - but is not required to be.

eerorika
  • 232,697
  • 12
  • 197
  • 326