Jump to content

  • Log In with Google      Sign In   
  • Create Account

We're offering banner ads on our site from just $5!

1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


Are pointers initialized to nullptr in a vector?


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
10 replies to this topic

#1 lride   Members   -  Reputation: 633

Like
1Likes
Like

Posted 23 March 2013 - 11:26 PM

class A{...};

int main()
{
	std::vector<A*> vec;
	vec.resize(10);
	std::cout<<(vec[9]==nullptr); //prints 1
}







 

 

I want to make sure this happens on every system.

Are pointers initialized to nullptr?


An invisible text.

Sponsor:

#2 Matias Goldberg   Crossbones+   -  Reputation: 3579

Like
1Likes
Like

Posted 23 March 2013 - 11:56 PM

I can't remember, but if you want to be 100% sure, you can do it explicitly:
vec.resize( 10, nullptr );


#3 Servant of the Lord   Crossbones+   -  Reputation: 20384

Like
3Likes
Like

Posted 24 March 2013 - 12:15 AM

They should be default initialized to 0 or nullptr, yes.
 
If it's a vector of ints, then each element should be the same value as doing:

int myInt = int();
assert(myInt == 0);

(without the assert, which is just to illustrate)
 
For a vector of pointers, they should be initialized with their default constructor:

int *myIntPtr = /* however you're supposed to default-initialize them - I'm not sure */;

 
Test this:

#include <iostream>

typedef int *IntPointer; //Just so I get the initialization syntax correct.

int main(int argc, char *argv[])
{
    int intA; //Not garunteed to be initialized.
    int intB = int(); //Default initialized.
    
    std::cout << intA << " - " << intB << std::endl;
    
    int *intPtrA;
    int *intPtrB = IntPointer();
	
    std::cout << intPtrA << " - " << intPtrB << std::endl;
	
    return 0;
}

These will either output:

0 - 0
0 - 0

 

Or:

2293640 - 0      //The first value being whatever random piece of memory was left there, but the second still being 0.
0x35434 - 0x0


Depending on whether your compiler is default-constructing everything or not (sometimes done for debugging purposes to help catch mistakes).

 

std::vector::resize() - "If [the second argument is] not specified, the default constructor is used instead."

 

The default constructor for ints is 0 and for pointers is null (whether explicitly 0 or nullptr, I'm not sure, but it probably doesn't matter).

Also see this: http://stackoverflow.com/a/937257/1177073 , where someone actually quotes the standard.


Edited by Servant of the Lord, 24 March 2013 - 12:20 AM.

It's perfectly fine to abbreviate my username to 'Servant' rather than copy+pasting it all the time.
All glory be to the Man at the right hand... On David's throne the King will reign, and the Government will rest upon His shoulders. All the earth will see the salvation of God.
Of Stranger Flames - [indie turn-based rpg set in a para-historical French colony] | Indie RPG development journal

[Fly with me on Twitter] [Google+] [My broken website]

[Need web hosting? I personally like A Small Orange]


#4 Álvaro   Crossbones+   -  Reputation: 13698

Like
0Likes
Like

Posted 24 March 2013 - 04:16 AM

Also, you should try to use vectors of objects, not vectors of pointers, unless you have a good reason not to. If you really do need pointers (say, because you are storing objects of different derived classes, to obtain polymorphism), you probably want to use a vector of unique_ptr.

#5 L. Spiro   Crossbones+   -  Reputation: 14033

Like
1Likes
Like

Posted 24 March 2013 - 04:28 AM

Every element of a vector gets initialized with its default value, which is NULL for pointers.

 

So the basic answer is “Yes”.

 

 

L. Spiro


It is amazing how often people try to be unique, and yet they are always trying to make others be like them. - L. Spiro 2011
I spent most of my life learning the courage it takes to go out and get what I want. Now that I have it, I am not sure exactly what it is that I want. - L. Spiro 2013
I went to my local Subway once to find some guy yelling at the staff. When someone finally came to take my order and asked, “May I help you?”, I replied, “Yeah, I’ll have one asshole to go.”
L. Spiro Engine: http://lspiroengine.com
L. Spiro Engine Forums: http://lspiroengine.com/forums

#6 Kraecker   Members   -  Reputation: 501

Like
0Likes
Like

Posted 24 March 2013 - 04:49 AM

afaik the initial value is compiler related and not defined in standard. so you can't be sure 100%, but basically most compilers do it this way.

#7 Brother Bob   Moderators   -  Reputation: 8455

Like
6Likes
Like

Posted 24 March 2013 - 05:40 AM

afaik the initial value is compiler related and not defined in standard. so you can't be sure 100%, but basically most compilers do it this way.

No, empty values in containers are value initialized, and value initialized primitive types (such as pointers, integers, floating point values and such) are required by the standard to have a zero value. This is different from default initialized values, which is undefined for primitive types.


Edited by Brother Bob, 24 March 2013 - 05:41 AM.


#8 Kraecker   Members   -  Reputation: 501

Like
2Likes
Like

Posted 24 March 2013 - 06:10 AM

ah, ok. thanks for correction.

#9 Lightness1024   Members   -  Reputation: 737

Like
0Likes
Like

Posted 26 March 2013 - 08:53 AM

Also, you should try to use vectors of objects, not vectors of pointers, unless you have a good reason not to. If you really do need pointers (say, because you are storing objects of different derived classes, to obtain polymorphism), you probably want to use a vector of unique_ptr.

boost::ptr_vector is here for that idiom.



#10 Álvaro   Crossbones+   -  Reputation: 13698

Like
0Likes
Like

Posted 26 March 2013 - 08:58 AM


Also, you should try to use vectors of objects, not vectors of pointers, unless you have a good reason not to. If you really do need pointers (say, because you are storing objects of different derived classes, to obtain polymorphism), you probably want to use a vector of unique_ptr.

boost::ptr_vector is here for that idiom.


My understanding is that boost::ptr_vector<blah> doesn't have any advantages over using std::vector<std::unique_ptr<blah>> now that we have move semantics. But I would love to learn something new, so please enlighten me.

#11 SiCrane   Moderators   -  Reputation: 9630

Like
0Likes
Like

Posted 26 March 2013 - 10:18 AM

About the only advantage I know of is that the pointer containers propagate constness. That is if you have a const_iterator to a ptr_vector you can't modify the stored object through that iterator, but a const_iterator to a std::vector of std::unique_ptrs can still be used to modify the pointed to objects.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS