c++ - Is setting pointer to null an allocated memory? -
i reading code initializes pointer null, thought code not allocating new memory pointer storing 2d array of values (which does) made me wonder, pointer initialized null pointer allocated memory?
class int8_tset : public gridset { public: int8_t** set; // // int8_tset():set(0) {} int8_tset( const int8_tset& x ):set(0) {copy(x);} virtual ~int8_tset() { free2darray(set);} // // --- opeartor int8_t operator() ( intvector2d x ) const {return set[x.i][x.j];} int8_t& operator() ( intvector2d x ) {return set[x.i][x.j];} // --- function void set(); void set(int8_t val); void set( intvector2d x ){ ns_griddata::set(x,*this,(int8_t)-1); } void set( intvector2d x,int8_t val){ ns_griddata::set(x,*this,val); } void switch(); // --- output & input void output(std::ostream& out ) const; void input (std::istream& in ); // --- copy & substitute public: void copy( const int8_tset& x ) {ns_griddata::copy(*this,x,(int8_t) -1);} const int8_tset& operator = ( const int8_tset& x ); void extract(intvector2d &ll, intvector2d &ur,int8_tset &subgrid) const; }; void int8_tset::set() { if(!set) {std::cerr<<" memory not allocated. call set(x) first \n"; exit(1); } std::fill(set[0],set[0]+size.i*size.j,-1); }
i realized allocating memory in void set.
is pointer initialized null pointer allocated memory
i guess mean like:
type* ptr = null;
in case memory allocated pointer (about variable ptr
) - sizeof(type*)
.
but not point allocated memory, delete ptr;
not necessary (but still safe).
Comments
Post a Comment