-6

I have been asked to find bug in the following code This code just prints abcdef, it seems pretty harmless to me but will appreciate any suggestions

#include <iostream>
using namespace std;

const char* append(const char* s1, const char* s2) {    
    string s(s1);    
    s += s2; 
    return s.c_str();
} 
void foo() { 
    const char* total = append("abc", "def"); 
    cout<<"total = "<<total<<endl;
}

int main() { 
  foo();
  return 0;
}
Tushar
  • 1
  • 2

1 Answers1

0

You have a dangling pointer. s is destroyed when it goes out of scope, meaning the pointer to its internal data is no longer valid.