reassigning pointers without calling delete

M

ma740988

I'm reviewing code (see below) that does identification of lasers on
four rails. The first thing the code does is create four 'Null_Type'
objects.

The next step in the process is to do initial identification. During
initial identification voltage values (not shown) are checked to
determine if it's a TYPE1 or TYPE2 laser.

The last step in the process compares another set of voltage values
(not shown) to determine if any TYPE2 is _really_ a SPECIAL_TYPE
(during initial identification above a SPECIAL_TYPE is _always_
treated as a TYPE2. i.e. A TYPE2 could be a TYPE2 or a
SPECIAL_TYPE).

The code:

enum Types { TYPE1, TYPE2, SPECIAL_TYPE, NULL_TYPE };
class Lsr_Types {

public :
virtual void Read () = 0 ;
virtual void Write () = 0;
virtual Types GetType() = 0 ;
};

class Type1 : public Lsr_Types {

public :
void Read () { }
void Write () { }
Types GetType() { return TYPE1; }

};
class Type2 : public Lsr_Types {

public :
void Read () { }
void Write () { }
Types GetType() { return TYPE2; }
};

class SuperSpecialType : public Lsr_Types {

public :
void Read () { }
void Write () { }
Types GetType() { return SPECIAL_TYPE; }
};

class Null_Type : public Lsr_Types {

public :
void Read () { }
void Write () { }
Types GetType() { return NULL_TYPE; }
};



#include <ctime>
#include <iostream>
#include <vector>

int main(){
int const NumLasers ( 4 ) ;
typedef std::vector < Lsr_Types* > LSR_TYPE_VEC ;
LSR_TYPE_VEC ltVecPtr;
for ( int odx ( 0 ) ; odx < NumLasers ; ++odx ) {
ltVecPtr.push_back ( new Null_Type() ) ;
}

// for test purposes create typoes Lasers will report
// during identification
typedef std::vector < Types > TYPE_VEC ;
TYPE_VEC tv ( 4 ) ;
tv [ 0 ] = TYPE1;
tv [ 1 ] = TYPE2;
tv [ 2 ] = TYPE2;
tv [ 3 ] = TYPE1;

//run initial identification.
//Here I could get either a Type1 or Type2
//
for ( std::size_t odx ( 0 ) ; odx < tv.size() ; ++odx ) {
if ( tv [ odx ] == TYPE1 ) {
Lsr_Types *ptr = new Type1() ;
ltVecPtr [ odx ] = ptr;
} else if ( tv [ odx ] == TYPE2 ) { // must be a Type2
Lsr_Types *ptr = new Type2() ;
ltVecPtr [ odx ] = ptr ;
} else {
// do nothing already a Null_Type
}
}

// for all Type2's check
for ( std::size_t odx ( 0 ) ; odx < ltVecPtr.size() ; ++odx ) {
if ( ltVecPtr [ odx ]->GetType() == TYPE2 ) {
// check some voltage value somewheres.. then do
// if ( someCondition ) { // you're really a SuperSpecialType
Lsr_Types *ptr = new SuperSpecialType() ;
ltVecPtr [ odx ] = ptr;
// }
}
}

for ( std::size_t odx ( 0 ) ; odx < ltVecPtr.size() ; ++odx ) {
std::cout << static_cast< int > ( ltVecPtr [ odx ]->GetType() ) <<
std::endl;
}

std::cin.get();
}

At issue pointers within the vector get reassigned but the previous
memory address was never deleted - which is borderline scary to me. I
like the concept starting out with Null_Types then working from
there. What could be done to restructure this besides calling delete
during the reseating of the pointer? Thanks
 
I

Ian Collins

ma740988 wrote:

At issue pointers within the vector get reassigned but the previous
memory address was never deleted - which is borderline scary to me. I
like the concept starting out with Null_Types then working from
there. What could be done to restructure this besides calling delete
during the reseating of the pointer? Thanks
Use a vector of smart pointers rather than raw pointers.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,961
Messages
2,570,131
Members
46,689
Latest member
liammiller

Latest Threads

Top