In this part of my code (receiving the time) I have a dynamically sized array of chars. This is for a school project and dynamically sized arrays are required.
char* msgtime::getTime() {
std::string t;
t.append(std::to_string(hour));
t.append(":");
t.append(std::to_string(minute));
char *charTime = new char[t.length()];
strcpy(charTime, t.c_str());
return charTime;
}
However, I can't delete the charTime since I am returning the value. I tried following another question I saw on here to return it into a char* in the main program then delete that when I'm done with it. The code here is what the function is returning to:
void receive(packet &data, SOCKET con) {
msgtime tim;
cout << "Receiving data" << endl;
int in = recv(con, (char*)&data, sizeof(data), 0);
cout << "Data received: " << in << endl;
tim.updateTime();
char *newTime = tim.getTime();
strcpy(data.time, newTime);
delete[] newTime;
}
Except when I run it I get this error:
HEAP CORRUPTION DETECTED: after Normal block (#183) at 0x00129330 CRT detected that the application wrote to memory after the end of heap buffer.
I need to delete the charTime in the getTime function to plug the memory leak but I can't figure out how to do it. Any help is GREATLY appreciated!
std::vector. Please avoid alloperator new[]business.std::stringand if someone asks where the dynamically sized array is, then show themstd::string::data()