Why passing a const char* from gdb to a function returning a string view behaves different from a function returning a string

Viewed 68

I have a question regarding the behavior of gdb (version 8.3). If I compile the below example code with gcc (9.2.1) and the compile options -O0 -g -std=c++17 and debug it running the following commands

br main
run
p make_string("test")
p make_string_view("test")

I observe different behavior for the two functions. The one returning a string prints 0x7ffff7a67780 : test whereas the one returning a string_view prints 0x7fffffffdf60 : hßÿÿÿ . I don't quite understand why this is happening or is this an issue in gdb ?

#include <string>
#include <string_view>
#include <iostream>

std::string make_string(const char* c) {
  std::cout << (void *) c << " : " << c << std::endl;
  return {};
}

std::string_view make_string_view(const char* c) {
  std::cout << (void *) c << " : " << c << std::endl;
  return {};
}

int main() {
}

Thanks and kind regards,

Lars

0 Answers
Related