Setting a pointer to null crashes my C ++ program

I have a constructor that gets a pointer to a character. If it is empty, I need to set its member variable to NULL, however, when I try to execute a program crash on exit.

I checked that it hits the line where it sets to NULL, and this is causing the crash.

I tried the following:

val = NULL;

val = 0;

val = "";

All this fails, however, if I used:

val = new Char[1];
val = "o";

he did not crash.

Is there something I am not doing?

Update:

Here is a brief update of my problem.

The destructor that I use is:

~LField() { 
    if (val)
      delete[] val;
}

If I choose:

if (val)
  delete[] val;

then the program will not work when exiting with:

val = "";

Here is another code on request:

LField(int rowNumVal, int colNumVal, int widthVal, const char *valVal = "", bool canEditVal = true) { 
    if(strlen(valVal) > 0) {            
      //doesn't jump in here since valVal is empty
    }
    else {
      val = ""; // this is where I'm trying to set a NULL value
    }
}

LField(const LField &clone) { 
  if (val)
    delete[] val;

  val = new char[strlen(clone.val)]; 
  strcpy(val, clone.val);
  rowNum = clone.rowNum;
  colNum = clone.colNum;
  width = clone.width;
  canEdit = clone.canEdit;
  index = clone.index;
}

LField& operator=(const LField &lfieldobj) {
    if (this != &lfieldobj) {
    if (val)
       delete[] val;

    val = new char[strlen(lfieldobj.val)];
    strcpy(val, lfieldobj.val);
    rowNum = lfieldobj.rowNum;
    colNum = lfieldobj.colNum;
    width = lfieldobj.width;
    canEdit = lfieldobj.canEdit;
    index = lfieldobj.index;
   }

   return *this;
}

Modified:

LField(int rowNumVal, int colNumVal, int widthVal, const char *valVal = NULL, bool canEditVal = true) { 
    if(valVal != NULL) {            

    }
    else {
      val = NULL; 
    }
}

LField(const LField &clone) { 
  delete[] val;
  if (clone.val != NULL) {
     val = new char[strlen(clone.val) + 1]; 
     strcpy(val, clone.val);
  }
  else
    val = NULL;
  rowNum = clone.rowNum;
  colNum = clone.colNum;
  width = clone.width;
  canEdit = clone.canEdit;
  index = clone.index;
}

LField& operator=(const LField &lfieldobj) {
    if (this != &lfieldobj) {
       delete[] val;
    if (lfieldobj.val != NULL) {                
       val = new char[strlen(lfieldobj.val) + 1];
       strcpy(val, lfieldobj.val);
    }
    else
       val = NULL;
    rowNum = lfieldobj.rowNum;
    colNum = lfieldobj.colNum;
    width = lfieldobj.width;
    canEdit = lfieldobj.canEdit;
    index = lfieldobj.index;
   }

   return *this;
}

~LField() { 
      delete[] val;
}

I updated the code. Now val is either allocated memory with a new [] or NULL, so there should be no problem with delete []. However, it still crashes on exit.

+3
5

delete[] :

LField(const LField &clone) { 
  //good code here, then...
  if (val) //<+ some random address here
    delete[] val;//<-undefined behavior
}

, . , "".

delete[] , undefined . :

LField(int rowNumVal, int colNumVal, int widthVal, const char *valVal = "", bool canEditVal = true) { 
    if(strlen(valVal) > 0) {            
      //doesn't jump in here since valVal is empty
    }
    else {
      val = new char[1];
      *val = 0;
    }
}

:

val = new char[strlen(whatever)];  <-forgot to +1 for the null terminator
strcpy(val, whatever);

delete[] - delete[] .

+10

, - () val, - NULL.

, ,

  val=NULL; //in the constructor

  //somewhere in your code      
  char ch= *val; //This would be Undefined Behavior

delete[] val, "" ( ), undefined.

UB

 1)
   char *p="hello";
  delete p; //UB
  delete []p; //UB

 2)
  char *p==new char[20]("Hello");
  delete p; //UB
  delete []p; //fine

 3) 
  char *p=new char('a');
  delete []p; //UB
  delete p; //fine
+3

, , ?

:

LField(const LField &clone) { 
  if (val)
    delete[] val;

, val undefined. delete [] .

  val = new char[strlen(clone.val)]; 
  strcpy(val, clone.val);

c . [] .

+3

delete[] NULL;
delete[] 0;

, -.

delete[] "whatever"; 

, char * [].

, , strlen(), , .

val . undefined.

+2

. , , , - .

:

  • val = 0;
  • , , . . , .
  • . , .
0

Source: https://habr.com/ru/post/1753516/


All Articles