Skip to main content
Missing pointer
Source Link
Graeme Perrow
  • 57.6k
  • 24
  • 87
  • 126

As obelix mentioned, you should use a vector from the Standard Template Library. However, if you're determined to use a raw array:

const int MAX_CHARACTERS = 11; Character *characters[MAX_CHARACTERS]; for(int characterCount = 0; characterCount < MAX_CHARACTERS; ++characterCount) { characters[characterCount] = new Character(); } ... if (characters != NULL) { for(int i = 0; i < MAX_CHARACTERS; ++i) { delete characters[i]; } } 

Paolo Capriotti is correct that characters should be declared with new if you want it to last beyond its scope:

const int MAX_CHARACTERS = 11; Character **characters = new Character[MAX_CHARACTERS];Character*[MAX_CHARACTERS]; for(int characterCount = 0; characterCount < MAX_CHARACTERS; ++characterCount) { characters[characterCount] = new Character(); } ... if (characters != NULL) { for(int i = 0; i < MAX_CHARACTERS; ++i) { delete characters[i]; } delete [] characters; } 

A better solution is the standard vector class:

#include <vector> ... const int MAX_CHARACTERS = 11; std::vector<Character> characters; for (int i = 0; i < MAX_CHARACTERS; ++i) { characters.push_back(Character()); } ... characters.clear(); 

Notice how much easier the cleanup was? (And in this case, it's optional, since when characters is destroyed it will automatically call the destructor of each item it contains.)

As obelix mentioned, you should use a vector from the Standard Template Library. However, if you're determined to use a raw array:

const int MAX_CHARACTERS = 11; Character *characters[MAX_CHARACTERS]; for(int characterCount = 0; characterCount < MAX_CHARACTERS; ++characterCount) { characters[characterCount] = new Character(); } ... if (characters != NULL) { for(int i = 0; i < MAX_CHARACTERS; ++i) { delete characters[i]; } } 

Paolo Capriotti is correct that characters should be declared with new if you want it to last beyond its scope:

const int MAX_CHARACTERS = 11; Character **characters = new Character[MAX_CHARACTERS]; for(int characterCount = 0; characterCount < MAX_CHARACTERS; ++characterCount) { characters[characterCount] = new Character(); } ... if (characters != NULL) { for(int i = 0; i < MAX_CHARACTERS; ++i) { delete characters[i]; } delete [] characters; } 

A better solution is the standard vector class:

#include <vector> ... const int MAX_CHARACTERS = 11; std::vector<Character> characters; for (int i = 0; i < MAX_CHARACTERS; ++i) { characters.push_back(Character()); } ... characters.clear(); 

Notice how much easier the cleanup was? (And in this case, it's optional, since when characters is destroyed it will automatically call the destructor of each item it contains.)

As obelix mentioned, you should use a vector from the Standard Template Library. However, if you're determined to use a raw array:

const int MAX_CHARACTERS = 11; Character *characters[MAX_CHARACTERS]; for(int characterCount = 0; characterCount < MAX_CHARACTERS; ++characterCount) { characters[characterCount] = new Character(); } ... if (characters != NULL) { for(int i = 0; i < MAX_CHARACTERS; ++i) { delete characters[i]; } } 

Paolo Capriotti is correct that characters should be declared with new if you want it to last beyond its scope:

const int MAX_CHARACTERS = 11; Character **characters = new Character*[MAX_CHARACTERS]; for(int characterCount = 0; characterCount < MAX_CHARACTERS; ++characterCount) { characters[characterCount] = new Character(); } ... if (characters != NULL) { for(int i = 0; i < MAX_CHARACTERS; ++i) { delete characters[i]; } delete [] characters; } 

A better solution is the standard vector class:

#include <vector> ... const int MAX_CHARACTERS = 11; std::vector<Character> characters; for (int i = 0; i < MAX_CHARACTERS; ++i) { characters.push_back(Character()); } ... characters.clear(); 

Notice how much easier the cleanup was? (And in this case, it's optional, since when characters is destroyed it will automatically call the destructor of each item it contains.)

Source Link
Bill
  • 14.8k
  • 4
  • 45
  • 55

As obelix mentioned, you should use a vector from the Standard Template Library. However, if you're determined to use a raw array:

const int MAX_CHARACTERS = 11; Character *characters[MAX_CHARACTERS]; for(int characterCount = 0; characterCount < MAX_CHARACTERS; ++characterCount) { characters[characterCount] = new Character(); } ... if (characters != NULL) { for(int i = 0; i < MAX_CHARACTERS; ++i) { delete characters[i]; } } 

Paolo Capriotti is correct that characters should be declared with new if you want it to last beyond its scope:

const int MAX_CHARACTERS = 11; Character **characters = new Character[MAX_CHARACTERS]; for(int characterCount = 0; characterCount < MAX_CHARACTERS; ++characterCount) { characters[characterCount] = new Character(); } ... if (characters != NULL) { for(int i = 0; i < MAX_CHARACTERS; ++i) { delete characters[i]; } delete [] characters; } 

A better solution is the standard vector class:

#include <vector> ... const int MAX_CHARACTERS = 11; std::vector<Character> characters; for (int i = 0; i < MAX_CHARACTERS; ++i) { characters.push_back(Character()); } ... characters.clear(); 

Notice how much easier the cleanup was? (And in this case, it's optional, since when characters is destroyed it will automatically call the destructor of each item it contains.)