0

Let's say my object is called THING and it has a pointer to another THING. I want to be able to point the THING pointer within my object to any element within my vector of pointers to THINGS, but I don't want it to point to some trash value, so I'm wondering if the elements are initialized to NULL, which would work with how I'm building a class containing these objects and vector of object pointers.

Simply put, does anyone know what the elements in a vector of pointers are initialized to?

Edit: To clarify, I'm using the standard vector class in .

morr36
  • 53
  • 5
  • `THING` is responsible for initializing it's own member functions. Initialize the member variable in the ctor initializer list. – Captain Obvlious Nov 29 '14 at 19:45
  • Yeah, THING is initializing its own members, but I'm not initially putting anything inside the vector and I'm pointing to elements inside the vector, so I'm just making sure I don't point to trash when I'd rather be pointing to NULL. – morr36 Nov 29 '14 at 19:51

2 Answers2

1

When a vector is created, all the elements in it are value initialized. So if you have a vector of pointers, they will all point to NULL unless you change the value.

Daniel T.
  • 32,821
  • 6
  • 50
  • 72
  • @Daniel T. No no no, default initialization (in contrast to value initialization) DOES NOT initializes pointers to NULL. http://stackoverflow.com/questions/1910832/why-arent-pointers-initialized-with-null-by-default Your answer is correct only if OP is using `std::vector`, which value-initializes its elements. – vsoftco Nov 29 '14 at 20:02
  • I'm using the vector class in . If the vector holds pointers, the standard class will initialize elements to NULL? – morr36 Nov 29 '14 at 20:08
  • @morr36, yes, as I mentioned in my answer. Please clarify this in your question, since as it is written right now, it is not clear you are using `std::vector` – vsoftco Nov 29 '14 at 20:09
  • @vsoftco `Foo cvfoo[10];` is not a vector, it's an array. – Daniel T. Nov 29 '14 at 20:10
  • @DanielT. technically yes, however (and you would probably agree), most people use it interchangeably. And anyway, I -1 your post since you said that the elements are "default initialized". This is not true, a `std::vector` does value initialization, which is what one wants. – vsoftco Nov 29 '14 at 20:11
  • @vsoftco, no I wouldn't agree to that. A vector is a vector and an array is an array. They are not the same thing. You call my answer into question and presumably down voted it because *you* misunderstood the question. Bad form. – Daniel T. Nov 29 '14 at 20:13
  • @DanielT. no, I didn't downvote it because of that, I did because you said "default initialization", which is indeed what is happening in case of plain old C arrays. That was another reason I assumed the OP talks about plain old arrays. – vsoftco Nov 29 '14 at 20:14
  • "default initialization" is the correct term: http://en.cppreference.com/w/cpp/language/default_initialization – Daniel T. Nov 29 '14 at 20:16
  • Oops, my bad. I see that the term was changed in C++03. Sorry about that. – Daniel T. Nov 29 '14 at 20:17
  • @DanielT. yeah I actually just saw that, for some reason I am used with the post C++03 term. Removed the -1 as I see it is confusing. – vsoftco Nov 29 '14 at 20:18
  • I haven't used C++ since before '03, so I didn't know about the change. :-) – Daniel T. Nov 29 '14 at 20:20
0

If you have a plain old C-style array like Foo vfoo[10];, then the pointers are NOT initialized to nullptr (i.e. are not value-initialized), but default-initialized. Only if you have a std::vector declared e.g. as std::vector<Foo> vfoo(10);, then its elements will be value-initialized, and your pointers will be nullptr.

See the constructor of std::vector<>:

explicit vector (size_type n, const T& value= T(), const Allocator& = Allocator()); 

Here const T& value= T() does the value-initialization.

Jeremy Friesner
  • 70,199
  • 15
  • 131
  • 234
vsoftco
  • 55,410
  • 12
  • 139
  • 252