How can I free memory before returning the value in my function?
-
How can I free memory before returning the value in my function?. Here's the code, thank you very much. int FindString(char*s, char* what, int len, int len2) { char * x = s; for (int i = 0;i 0) { char * New = new char[i]; memcpy(New,s,i); // How can I use 'delete' here before returning New??? return New; } return NULL; }
-
How can I free memory before returning the value in my function?. Here's the code, thank you very much. int FindString(char*s, char* what, int len, int len2) { char * x = s; for (int i = 0;i 0) { char * New = new char[i]; memcpy(New,s,i); // How can I use 'delete' here before returning New??? return New; } return NULL; }
Why do you want to delete the array before returning the pointer to the caller? What are you trying to achieve? Mark
Mark Salsbery Microsoft MVP - Visual C++ :java:
-
How can I free memory before returning the value in my function?. Here's the code, thank you very much. int FindString(char*s, char* what, int len, int len2) { char * x = s; for (int i = 0;i 0) { char * New = new char[i]; memcpy(New,s,i); // How can I use 'delete' here before returning New??? return New; } return NULL; }
You don't delete it before returning the Pointer. The function that called FindString receives this pointer. This calling function can do it's thing with the pointer, and, if needed return it again. When you're done with the value of the pointer you must call delete, otherwise you leak the memory. As soon as you call delete, the value of the pointer points no longer to valid memory, and may not be used again. Using it's value again is an error, and will lead sooner or later to unpredictible behaviour or even crashes. regards
Bram van Kampen
-
How can I free memory before returning the value in my function?. Here's the code, thank you very much. int FindString(char*s, char* what, int len, int len2) { char * x = s; for (int i = 0;i 0) { char * New = new char[i]; memcpy(New,s,i); // How can I use 'delete' here before returning New??? return New; } return NULL; }
Thanks for your answer. I was wondering if I had to return that pointer how to remove it if it was my desired return value. Thanks for your answer, Bram van Kampen. This way i'd be doing it fine??
int FindString(char*s, char* what, int len, int len2) { char * x = s; for (int i = 0;i 0) { char * New = new char[i]; memcpy(New,s,i); memcpy(ReturnArray,s,i); delete[] New; return ReturnArray; } return NULL; }
-
Thanks for your answer. I was wondering if I had to return that pointer how to remove it if it was my desired return value. Thanks for your answer, Bram van Kampen. This way i'd be doing it fine??
int FindString(char*s, char* what, int len, int len2) { char * x = s; for (int i = 0;i 0) { char * New = new char[i]; memcpy(New,s,i); memcpy(ReturnArray,s,i); delete[] New; return ReturnArray; } return NULL; }
Hi, U declared ReturnArray as a local buffer. ie it's scope is only within the function rr. So after the execution of rr ,ReturnArray will be destroyed. i think u r going to get a "memory cannot be read error" . am i right?. thanks Nitheesh
-
Thanks for your answer. I was wondering if I had to return that pointer how to remove it if it was my desired return value. Thanks for your answer, Bram van Kampen. This way i'd be doing it fine??
int FindString(char*s, char* what, int len, int len2) { char * x = s; for (int i = 0;i 0) { char * New = new char[i]; memcpy(New,s,i); memcpy(ReturnArray,s,i); delete[] New; return ReturnArray; } return NULL; }
You could declare it as a global variable, outside the function instead:
char ReturnArray[1024] = {0}; // put it here. char *rr(char*s) { char cc[4] = {0x00,0x00,0x00,0x00}; // char ReturnArray[1024] = {0}; // move it up instead.