-1

I am doing an assignment that asked me to implement a priority queue. It has to pass 19 assertions. I can pass 18/19 assertions but it gets stuck on this one.

assert(head->data == "first node");

This doesn't make sense to me because why is it using == to compare two strings rather than strcmp? How am I supposed to pass this assertion? I'm not allowed to change the code with the assertions. Is this a mistake by the prof or is there something I'm missing.

The struct used to access the data is below.

typedef struct node {
    int priority;
    char * data;
    struct node * next;
} Node_t, * Node_ptr_t;
Mike
  • 41
  • 2
  • 6
    why c and C++ tag, they two different languages, you have to chose one – Seek Addo Jan 21 '17 at 02:36
  • 3
    is `data` a `std::string`? – Daniel A. White Jan 21 '17 at 02:37
  • 5
    Please provide a [mcve] – Nicol Bolas Jan 21 '17 at 02:37
  • 2
    If `data` is an `std::string` then it is using the overloaded `==` operator for that class to compare the string. – Govind Parmar Jan 21 '17 at 02:39
  • Provide some more code so that we can better understand the problem. – BusyProgrammer Jan 21 '17 at 02:40
  • 1
    It's a mistake by your professor (assuming you are accurately representing the situation). – Benjamin Lindley Jan 21 '17 at 02:46
  • @Mike, I would agree to your hypothesis. It is a mistake. You can't use `==` to check logical equality of the strings. – VHS Jan 21 '17 at 02:46
  • @VHS this may be possible with some C compilers/linkers using interning - also indeed if it is the case it would be relying on unspecified behavior (I'v posted that as answer too). – Alexei Levenkov Jan 21 '17 at 02:53
  • @AlexeiLevenkov, in your answer you state that `interning` (in C) is not guaranteed. And also, probably only some C run times support it. So with these points in mind, do you still think it is right to do pointer comparison for checking logical string equality? – VHS Jan 21 '17 at 03:01
  • @VHS I don't see other solution to this code viewed as code puzzle (ok, ignoring linking alternative `assert` that will always pass, but it is even less likely to work). Indeed relying on pointer comparison for C "strings" should not be done in real code - but definitely good conversation point with teacher/TA. – Alexei Levenkov Jan 21 '17 at 03:08
  • `head->data == "first node"` does not compare 2 strings. It compare 2 pointers. `head->data` is not an array, nor a string. It is a pointer. – chux - Reinstate Monica Jan 21 '17 at 03:45
  • To compare 2 strings , code should be `assert(strcmp(head->data, "first node") == 0);` – chux - Reinstate Monica Jan 21 '17 at 04:02

1 Answers1

1

If this is strictly C such assertion can be satisfied with for most compilers with "interning strings" - reusing pointers to existing constant strings instead of copying value to local array. Most compiler/linker tools will unify all references to same constant. See How can I do string interning in C or C++?

Note that interning is not guaranteed - but I think that is the only shot at the problem. Should be good starting point for discussion about what is "defined"/"undefined"/"unspecified" behavior in C.


As originally it was marked C++: if this question is about C++ overloaded operator == - if class of data is open for you to modify than you can add operator == that will satisfy that assertion (you can simply always return true from it).

Community
  • 1
  • 1
Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
  • Thank you! The problem was I used strcpy when inserting a new node to the priority queue instead of doing node->data = data. – Mike Jan 21 '17 at 02:49
  • @Mike please make sure to carefully read question linked in my answer too - C is not required to actually support this everywhere. – Alexei Levenkov Jan 21 '17 at 02:54
  • Plus1 for additional insight on interning in C / C++ in the context of this question. – VHS Jan 21 '17 at 03:18
  • The assertion could have been satisfied by the assignment `head->data = "first node"`. No interning is required. – DYZ Jan 21 '17 at 04:23
  • 2
    @DYZ: Yes, interning is required in order for that to work. If interning doesn't happen, then the string literal in that assignment will refer to a unique memory block, different from the string literal in the assertion. – Benjamin Lindley Jan 21 '17 at 19:27