I'm not a c++ expert so I was wondering if someone could explain to me what's going wrong with this code. When it gets to delete[] str I get a an error
HEAP CORRUPTION DETECTED. CRT detected that the application wrote to memory after end of heap buffer.
This seems to be telling me that my buffer isn't large enough, but I can't see why.
char* foo()
{
std::string s = "01";
char* buffer = new char[s.size()+1];
strncpy_s(buffer, sizeof(buffer), s.c_str(), s.size());
buffer[s.size()] = '\0';
return buffer;
}
int main()
{
char* str = foo()
for (int i = 0; i < strlen(str); ++i)
{
std::cout << str[i];
}
delete[] str;
std::getchar();
return 0;
}