Jump to content

  • Log In with Google      Sign In   
  • Create Account


#ActualServant of the Lord

Posted 24 March 2013 - 12:20 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.


#3Servant of the Lord

Posted 24 March 2013 - 12:16 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).


#2Servant of the Lord

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 initializer:

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-initializing everything or not (sometimes for debugging purposes).


#1Servant of the Lord

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 initializer:

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.
0x35434 - 0x0


Depending on whether your compiler is default-initializing everything or not (sometimes for debugging purposes).


PARTNERS