Intended behavior or bug -- VC 2005 Beta 2

  • Thread starter Thread starter George Poulose
  • Start date Start date
G

George Poulose

#include <string>
using namespace std;

{
const string s("George");
char* p = const_cast<char*>(s.c_str());
strcpy_s(p, s.length(), "John");
}

Thanks.

George
 
George said:
#include <string>
using namespace std;

{
const string s("George");
char* p = const_cast<char*>(s.c_str());
strcpy_s(p, s.length(), "John");
}

The const_cast invokes undefined behaviour. What were you expecting and how
is it different from what you sought? As a reminder, it is legal for
std::string to have embedded "\0" characters, but of course display of such
a string may be problematic.

Arnaud
MVP - VC
 
George Poulose said:
#include <string>
using namespace std;

{
const string s("George");
char* p = const_cast<char*>(s.c_str());

This cast isn't allowed by the language. That makes the program
invalid.
strcpy_s(p, s.length(), "John");
}

Thanks.

George

Bo Persson
 
Back
Top