Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ segmentation error when first parameter is null in comparison operator overload

I am writing a class called Word, that handles a c string and overloads the <, >, <=, >= operators.

word.h:

friend bool operator<(const Word &a, const Word &b);

word.cc:

bool operator<(const Word &a, const Word &b) {
  if(a == NULL && b == NULL)
    return false;

  if(a == NULL)
    return true;

  if(b == NULL)
    return false;

  return strcmp(a.wd, b.wd) < 0;  //wd is a valid c string, EDIT: changed to strcmp
}

main:

char* temp = NULL;     //EDIT: i was mistaken, temp is a char pointer
Word a("blah");    //a.wd = [b,l,a,h]
cout << (temp<a);

I get a segmentation error before the first line of the operator< method after the last line in the main. I can correct the problem by writing

cout << (a>temp);

where the operator> is similarly defined and I get no errors but my assignment requires (temp < a) to work so this is where I ask for help.

EDIT: I made a mistake the first time and i said temp was of type Word, but it is actually of type char*. So I assume that the compiler converts temp to a Word using one of my constructors. I dont know which one it would use and why this would work since the first parameter is not Word.

here is the constructor I think is being used to make the Word using temp:

Word::Word(char* c, char* delimeters="\n") {
  char *temporary = "\0";
  if(c == NULL)
    c = temporary;
  check(stoppers!=NULL, "(Word(char*,char*))NULL pointer"); // exits the program if the expression is false
  if(strlen(c) == 0)
    size = DEFAULT_SIZE;  //10
  else
    size = strlen(c) + 1 + DEFAULT_SIZE;
  wd = new char[size];
  check(wd!=NULL, "Word(char*,char*))heap overflow");
  delimiters = new char[strlen(stoppers) + 1];      // EDIT: changed to []
  check(delimiters!=NULL,"Word(char*,char*))heap overflow");
  strcpy(wd,c);
  strcpy(delimiters,stoppers);
  count = strlen(wd);
}

wd is of type char*

thanks for looking at this big question and trying to help. let me know if you need more code to look at

like image 266
fvf Avatar asked Jul 11 '26 06:07

fvf


1 Answers

I'm almost positive you did not mean to construct a char on the heap with an initial value of some integer based on the size of stoppers:

delimiters = new char(strlen(stoppers) + 1); // Should use [] not ()

Also you are using C++ and I would never tell you what to do, but please, unless you know exactly that there is no danger, do not use strcpy. For exactly this reason.

It is a blind copy of strings, and when the destination does not have enough space (as is the case from your typo-ed allocation), things go BAD.

EDIT:

I also see in your overload of operator< that you use

a.wd < b.wd

and claim that .wds are valid C strings. If that is the case, you cannot apply a simple < operator to them and must use strcmp, strncmp or some other full compare function

like image 189
im so confused Avatar answered Jul 13 '26 18:07

im so confused



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!