0

Im getting an unexpeted behavior when passing a const string to a function as parameter.

I have this function:

std::string getStatusByTopic(const std::string topic){
    if (topic.compare(TOPIC_WP_SM_CMD) == 0)
            return state_to_string(STATUSES::WP_HANDLER_STATUS);
}

Which is not totally implemented yet.

And I have this function:

InsertStatus(getStatusByTopic(TOPIC_WP_SM_CMD),msg);

Which declared as:

void Msp::InsertStatus(const std::string status_topic, const std_msgs::String::ConstPtr& new_status)

I have 2 questions:

  1. These functions have been compiled successfully, even though there's a possible usage where getStatusByTopic() returns nothing, when its defined returned type is string. How is it possible?

  2. When the code is executed, with the scenario that and getStatusByTopic(TOPIC_WP_SM_CMD) returns nothing. Theres no runtime errors, more over - TOPIC_WP_SM_CMD is passed as parameter to InsertStatus() (which is defined ad "/some/topic/"). Wow is that possible?

Thanks.

Mehdi Mostafavi
  • 851
  • 1
  • 11
  • 23
  • 3
    [What are all the common undefined behaviours that a C++ programmer should know about?](https://stackoverflow.com/questions/367633/what-are-all-the-common-undefined-behaviours-that-a-c-programmer-should-know-a) Undefined Behaviour is undefined. It can do whatever it wants. To catch such errors, [enable your compiler warnings](https://stackoverflow.com/questions/57842756/why-should-i-always-enable-compiler-warnings) – Yksisarvinen Mar 15 '20 at 13:26
  • What options did you pass to your compiler? In particular, have you enabled all the available warnings (you should). Your compiler should be able to inform you about this example of UB. – bitmask Mar 15 '20 at 13:37
  • Also see this [question](https://stackoverflow.com/questions/1610030/why-does-flowing-off-the-end-of-a-non-void-function-without-returning-a-value-no) – 1201ProgramAlarm Mar 15 '20 at 15:10

0 Answers0