-1

I have this function:

char* return_string(){
    char buffer[] = "Hi world!";
    return buffer;
}

bool test08()
{
    char compare[] = "Hi world!";
    int result = strcmp(compare,return_string());
if (result == 0) return true;
return false;
}
int main()
{
if(test08) printf("\nTRUE");
else printf("\nFALSE");
}

Why this code run in c++ Shell and it doesn't in codeblocks v. 13.12 (Segmentation fault); it'll work if i change my char buffer[]= declaration to char *buffer=; i'm a beginner at C++ (easy to know) so please be clear...

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
IBot
  • 356
  • 3
  • 20

5 Answers5

1

This:

char* return_string(){
    char buffer[] = "Hi world!";
    return buffer;
}

copies the string "Hi world!" into the local variable buffer and then returns that local variable - this results in undefined behaviour if you try to use that return value, because the buffer is discarded on function exit.

This:

   char* return_string(){
       char *buffer = "Hi world!";
        return buffer;
   }

would actually be OK, though probably not what you want. You would be returning the address of the start of a string literal (stored in a mysterious place, re the standard), which is OK.In C++, you would have problems with const.

1

Just change the function return_string the following way

const char* return_string(){
    const char *buffer = "Hi world!";
    return buffer;
}

The problem with the original function implementation is that the array buffer is a local array of the function with the automatic storage duration that will not be alive after exiting the function.

In the modified function there is used a string literal that has static storage duration. So you may return a pointer to the first character of the string literal.

The function test08 can be written simpler

bool test08()
{
    char compare[] = "Hi world!";
    return strcmp( compare, return_string() ) == 0;
}
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
1

Your function return_string is returning pointer to a local variable buffer that is defined only in the scope of return_string. When the function returns, you return the address of where buffer used to be stored while the value stored in that address is no longer valid. Well, if you insist on returning a pointer to character, you can dynamically allocate "Hello World" and remember to free it later:

char * return_string() {
    char local_var[] = "hello world";
    char *buffer = (char *) malloc(sizeof(local_var));
    strcpy(buffer, local_var); // we know that buffer is big enough to hold local_var
    return buffer;
}

Since the memory in which "hello world" is stored is not local to the function, it won't be released when the function exits.

Another (better) solution is to pass a statically allocated buffer (hopefully large enough to hold your value) to return_string and modify its content there.

void return_string(char *buffer, size_t buffer_size) {
    strncpy(buffer, "hello world", buffer_size);
}
Pejman
  • 1,328
  • 1
  • 18
  • 26
0

You are returning a pointer to a buffer but the buffer variable gets destroyed after the function returns.

You need to make your buffer variable static.

ChrisK
  • 118
  • 8
0

This is an answer at all, but still...
I've tried on CodeBlocks 16.01 (mingw) and everything works smoothly. So a quick suggestion, always try working with the upgraded versions of any software, especially if you still in learning process.

Don Exo
  • 75
  • 6
  • This is a typical case of "undefined behavior" *accidentally* working even though the code is wrong, making it much harder to identify the bug. – merlinND Jan 15 '17 at 00:54
  • C++ does not work this way. Just because it seems to work does not mean the code is "ok". Returning the address of a local variable is undefined behavior, regardless of what that behavior may be. It could work, it could fail, it could fail if compiled with different options, it may work today, fail tomorrow, etc. – PaulMcKenzie Jan 15 '17 at 01:17