C++ delete[] calls destructor -


edited : added constructor , destructor.

edit : leak :

c:\users\sijaan\desktop\1\starray.cpp(61) : {148} normal block @ 0x007c0910, 40 bytes long.  data: <        h |     > c8 00 00 00 02 00 00 00 68 09 7c 00 cd cd cd cd  c:\users\sijaan\desktop\1\starray.cpp(43) : {145} normal block @ 0x007c04b0, 40 bytes long.  data: <d         {     > 64 00 00 00 01 00 00 00 a8 e2 7b 00 cd cd cd cd  c:\users\sijaan\desktop\1\starray.cpp(24) : {143} normal block @ 0x007c0150, 816 bytes long.  data: <    x {         > 80 00 00 00 58 e1 7b 00 ce cd cd cd ce cd cd cd  object dump complete. 

i'm trying create new object after every call function :

    bool starray::addcs_course(int studentid, int courseid, char * coursename, int hwnum, double hwweigh, int flag, char * bookname) {     (int = 0; < max_student_num; i++) {         if (starray_[i] == null) continue;         if (starray_[i]->getid() == studentid) {              cs_course* new_cs = new cs_course(courseid, coursename, hwnum, hwweigh, flag, bookname);              starray_[i]->addcs_course(new_cs);             return true;         }      }         return false; } 

should use new this? or constructor makes object used outside function?

here's class declaration :

class cs_course : public course {  public:     /*interface functions*/     /* *****************************************************     function: cs_course     abstract: constructor     parameters:          course_id : course number         course_name : course name         hw_num : homeworks number         hw_weigh : weigh of homeworks     return value:      ***************************************************** */     cs_course(int course_id, char* course_name, int hw_num, double hw_weigh, int flag, char* bookname); // constructor     ~cs_course();  private:     int flag_; char* bookname_; 

i'm asking because of when i'm deleting coursename string , bookname, i'' still have new allocated object has nothing. im doing : delete[] pcsc[j]; pcsc has pointers cs_course. error because delete calls cs_course destructor trys destroy coursename destroyed.

this constructor :

cs_course::cs_course(int course_id, char * course_name, int hw_num, double hw_weigh, int flag, char * bookname)     :course(course_id, course_name, hw_num, hw_weigh), flag_(flag) {     bookname_ = new char[strlen(bookname) + 1];     strcpy(bookname_, bookname); } 

and destructor :

cs_course::~cs_course()  {     delete[] bookname_; } 

the new here necessary, since passing pointer newly created object. if creating local variable , passing it, wont available outside function.

also, make sure delete objects when no longer needed (i.e. if remove array). otherwise cause memory leak.


Comments

Popular posts from this blog

multithreading - Exception in Application constructor -

React Native allow user to reorder elements in a scrollview list -

windows - CertCreateCertificateContext returns CRYPT_E_ASN1_BADTAG / 8009310b -