I'm doing an exercise from C++ Primer book, and I suspect that this bit of code I've written is leaking memory.
Now, the way I understand this is that every time set() is called new block of memory is allocated for t and then st.str is set to point to this block. My concern is that the block st.str previously pointed to is, to the best of my understanding, never released.Code:struct Stringy { char* str; int ct; // lenght w/o '\0'; }; void set(Stringy& st, const char s[]); // main() void set(Stringy& st, const char s[]) { using namespace std; int len = strlen(s); char* t = new char[len + 1]; st.ct = len; st.str = t; strcpy(st.str, s); }
So my questions are (1) do I understand the situation correctly and if yes (2) what can be done to remedy it?