Destructor in C++ doesn't work -


can tell me what's wrong me destructor ? if remove working well.

#include "stdafx.h" #include <stdio.h> #include <string.h> #include <conio.h>  class dynstring { private :      char *string;     int size;  public :      dynstring(const char *tab)         {                 string = new char[strlen(tab)];                 size = strlen(tab);                 if (string != null)                 {                     strcpy(this->string, tab);                 }         }          dynstring(const dynstring& s)         {             string = new char[s.size];             if (string != null)             {                      strcpy(string, s.string);                     size = s.size;             }             else size = 0;       }      int size() const     {             return size;     }      const char* cstr()     {             return string;     }      dynstring& operator +=(const char* tab)     {             char *bufor = new char[size + strlen(tab)];             if (bufor != null)             {                      strcpy(bufor, string);                     strncat(bufor, tab, strlen(tab));                     string = new char[strlen(bufor) + sizeof(char)];             }              if (string != null)             {                      strcpy(string, bufor);                     size = strlen(string);             }             return *this;     }      dynstring& operator !()     {             unsigned int size = strlen(string);             (unsigned int = 0; < size; i++)             {                     if (string[i] >= 97 && string[i] <= 122)                     {                             string[i] -= 32;                     }                     else if (string[i] >= 65 && string[i] <= 90)                     {                             string[i] += 32;                     }             }             return *this;     }     ~dynstring();   }; dynstring::~dynstring() {         if (size == 0)         {                 delete string;         }         else         {                 delete[] string;         } }  int _tmain(int argc, _tchar* argv[]) {         dynstring string("test words.");         printf("string %s\n", string.cstr());         printf("characters: %i\n", string.size());          dynstring copy = string;         printf("copy %s\n", copy.cstr());         printf("characters: %i\n", copy.size());          copy += " - first fragment -";         copy += " second fragment.";          printf("copy %s\n", string.cstr());         printf("characters: %i\n", string.size());         printf("copy %s\n", copy.cstr());         printf("characters: %i\n", copy.size());          !string;         printf("string %s\n", string.cstr());         printf("\nend.");         _getch();         return 0; } 

your destructor shall be:

if(string!=null) {      delete[] string; } 

or just

delete[] string; 

(given delete[]ing null pointer safe).

remember: allocated new[] must destructed delete[]; allocated new must destructed delete.

edit:

also, should allocate string strlen(tab)+1 space, accomodate terminating null byte (for copy constructor, means s.size+1).


Comments

Popular posts from this blog

google chrome - Developer tools - How to inspect the elements which are added momentarily (by JQuery)? -

angularjs - Showing an empty as first option in select tag -

php - Cloud9 cloud IDE and CakePHP -