-1

I want to return a C style string from a function. I found this solution: Solution

const char* myName() {
  char *name = "Flavio";
  return name;
}

However I don't understand how this works. How come the c string isn't destroyed when it goes out of scope? How does the heap work in this case? Do consecutive calls to the function allocate more and more memory on the heap; causing a memory leak? How is the string cleared off the heap?

Thanks.

Rs Fps
  • 65
  • 1
  • 7
  • It works by returning string literals. If you don't have a string literal the method won't help. – drescherjm Feb 16 '21 at 15:12
  • See https://stackoverflow.com/questions/2589949/string-literals-where-do-they-go – m88 Feb 16 '21 at 15:13
  • Also: [https://stackoverflow.com/questions/349025/is-a-string-literal-in-%D0%A1-created-in-static-memory](https://stackoverflow.com/questions/349025/is-a-string-literal-in-%D0%A1-created-in-static-memory) – drescherjm Feb 16 '21 at 15:14
  • 2
    the "solution" you found is of rather limited use. The tutorial isnt outright wrong, but it is quite misleading – 463035818_is_not_an_ai Feb 16 '21 at 15:14
  • 2
    Please provide the code as text in the post, not as a link to an image – Damien Feb 16 '21 at 15:14
  • 2
    ***Do consecutive calls to the function allocate more and more memory on the heap; causing a memory leak?*** In the case that is presented there is no memory allocated. You are returning a pointer to a string literal. – drescherjm Feb 16 '21 at 15:16
  • 5
    I would recommend against using C tutorials when trying to learn about C++. C and C++ are different languages and facts and good habits for one language often don't apply to the other. Edit : Regarding your edit, this illustrates my point. `char *name = "Flavio";` is allowed in C but not in C++ since C++11. You need `const char *name = "Flavio";` in C++. – François Andrieux Feb 16 '21 at 15:16
  • 1
    ***How is the string cleared off the heap?*** The string is not on the heap. – drescherjm Feb 16 '21 at 15:19
  • please pick one language. If you want to return a c-string in C++ you can return a `std::string`. Constructing a `std::string` from a c-string and later getting a c-string from the `std::string` is a simple solution that relieves you from all the headaces that comes with passing raw c-arrays around – 463035818_is_not_an_ai Feb 16 '21 at 15:20

1 Answers1

3

In fact, your code does not illustrate your question.

Fixed version of your code:

const char* myName() {
  const char *name = "Flavio";    // name is just a pointer to a literal
  return name;
}

Here you just return a pointer to a string litteral: no allocation anywhere no dangling pointer

What I understand from your question:

char* myName() {
  char name[] = "Flavio";        // name is an automatic array here
  return name;                   // Oops: dangling pointer
}

Here name is a dynamic array initialized from the content of the literal. A new object comes to life on each call and reaches end of life when the function returns. The caller gets a pointer to a dead object (a dangling pointer) and using it invokes Undefined Behaviour.

Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252