[C++] Pointers and Vectors.

Samael88

Evil always finds a way
Reaction score
181
Okay, so I am trying to use pointers and vectors together and I am a bit confused.

My question is, if I have the code provided below, how do I clean it up afterwards?

Code:
vector <int*> myVector;
int* a = new int;
*a = 8;
myVector.push_back(a);
 

Sgqvur

FullOfUltimateTruthsAndEt ernalPrinciples, i.e shi
Reaction score
62
delete myVector[0];

Edit:
It seams you can merge those two lines
int* a = new int;
*a = 8;

int* a = new int(8);
 

Samael88

Evil always finds a way
Reaction score
181
Does that delete the entire vector or just the first variable?
Edit:
If I have more than one variable in my vector should I loop through it then and delete them all separately?
 

Sgqvur

FullOfUltimateTruthsAndEt ernalPrinciples, i.e shi
Reaction score
62
Well "myVector[0]" is an array/vector look up/dereferencing so it can't be freeing the whole vector.

>If I have more than one variable in my vector should I loop through it then and delete them all separately?

Well I guess so but maybe there could be some better way(foreach) or something idk... =)

Edit:
Probably the vector has a destructor that will delete it when it gets out of scope.
 

seph ir oth

Mod'n Dat News Jon
Reaction score
262
Always a good reference:

http://www.cplusplus.com/doc/tutorial/pointers/

As pointers are fugly.

Anywho, not sure if there is no definite way to clean up the code as you cannot delete integer a otherwise its value is inaccessible in myVector, and myVector is the vector to be used elsewhere in the code (I assume). Simply delete all pointers when you are done using them (delete a).
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Staff online

      • Ghan
        Administrator - Servers are fun

      Members online

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top