-2

Is returning &string[0] is same as char* from a function foo

char* foo(){
    string str="fsafsdf";
    return &str[0];
}

If above is legal can we return a c styled string in this way

char* foo(){
    string str="fsafsdf";
    str=str+'\0';
    return &str[0];
}
Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268
aka123
  • 17
  • 4
    "If above is legal " - it isn't. –  Aug 26 '19 at 22:09
  • 5
    `string str` is a local variable scoped by `foo`. It will expire at the end of `foo`, so returning references to this variable is a fatal flaw. – user4581301 Aug 26 '19 at 22:10
  • But memory is allocated in heap for string @user4581301 – aka123 Aug 26 '19 at 22:11
  • 2
    "memory is allocated in heap for string " and deallocated once the function exits. –  Aug 26 '19 at 22:12
  • This imply &s[0] will be in heap so what is the problem in returning this – aka123 Aug 26 '19 at 22:13
  • 2
    Read a good book on C++. –  Aug 26 '19 at 22:13
  • @aka123 But when `std::string` is destroyed, all memory it used is deallocated. Gone. As good as never existed. It cleaned up after itself and rests in peace. – Yksisarvinen Aug 26 '19 at 22:14
  • So we can not return reference to string from a local function also am i correct? – aka123 Aug 26 '19 at 22:14
  • `std::string` follows the idiom of [RAII](https://stackoverflow.com/questions/2321511/what-is-meant-by-resource-acquisition-is-initialization-raii) The `string` [owns](https://stackoverflow.com/questions/49024982/what-is-ownership-of-resources-or-pointers) the memory allocation and when the `string` goes, it takes the allocation with it . – user4581301 Aug 26 '19 at 22:17
  • C++ has no concept of ``heap" – lost_in_the_source Aug 26 '19 at 22:20
  • True, but it will gleefully use a heap for dynamic memory f it is offered. – user4581301 Aug 26 '19 at 22:53

2 Answers2

3

It is not legal.

You return char* to an object that just died. str ceases to exist as soon as you meet the ending curly brace } of the function. You are returning a dangling pointer, which doesn't point to anything useful.

Yksisarvinen
  • 18,008
  • 2
  • 24
  • 52
  • "Illegal" is actually the least of your worries. This can actually be worse than that: at first it may just appear to work. Then you start seeing strange results and experience "unexplainable" crashes. Especially if your program has more than one thread. – E. van Putten Aug 26 '19 at 22:30
  • My favourite is the [Great Comdex Crash of '98](https://www.youtube.com/watch?v=73wMnU7xbwE). How many times do you think the demo worked before Bill Gates got on the stage to show it off? If a hidden bug is going to pop up, it's when it going to get you fired. Or worse, kill people. – user4581301 Aug 26 '19 at 22:58
3

If above is legal

It is syntactically correct.

Is it semantically correct? It depends on what you do with the pointer.

The line

return &str[0];

returns a pointer to an object that won't be alive after the function returns. The pointer is a dangling pointer in the calling function.

If you try to dereference the poiner, your code is subject to undefined behavior.

if ( *(foo()) == 'a' ) { ... }   // Not OK.

If you use the pointer simply as a pointer without dereferencing it, the code should be OK. For example, you can compare the returned value against nullptr.

if ( foo() == nullptr ) { ... }  // OK.

Anecdotal Note

I have seen commercially deployed code that uses addresses of function local variables to deduce whether the stack grows up or down in a platform.

char* foo()
{
   char c;
   return &c;
}

bool stackGrowsUp()
{
   char c;
   return (&c < foo());
}
R Sahu
  • 204,454
  • 14
  • 159
  • 270