K
kiran.agashe
Hi,
Please refer program below:
#include <string>
#include <cstdio>
using namespace std;
const char* f();
main()
{
const char* str = f();
printf("%s", const_cast<char*> (str));
}
Now I have 2 implementations for f():
1.
const char* f()
{
return "Hello";
}
2.
const char* f()
{
string str = "Hello";
return str.c_str();
}
Are both of these implemetations exactly equivalent and "Safe" with
respect to memory model?
[
e.g.
char* f1()
{
char a[10];
return a;
}
is not safe.
]
What happens when we return a const char*?
Thanks & Regards,
- Kiran Agashe
Please refer program below:
#include <string>
#include <cstdio>
using namespace std;
const char* f();
main()
{
const char* str = f();
printf("%s", const_cast<char*> (str));
}
Now I have 2 implementations for f():
1.
const char* f()
{
return "Hello";
}
2.
const char* f()
{
string str = "Hello";
return str.c_str();
}
Are both of these implemetations exactly equivalent and "Safe" with
respect to memory model?
[
e.g.
char* f1()
{
char a[10];
return a;
}
is not safe.
]
What happens when we return a const char*?
Thanks & Regards,
- Kiran Agashe