0

Why I cant' release the memory?

inline void reverseWord( char* input, const int inputLen) { char *temp = new char[inputLen]; int end = inputLen, start = 0; while (end-- > 0) temp[start++] = input[end]; delete[] input; input = temp; }

26th Jul 2018, 10:16 PM
Petros Simidyan
Petros Simidyan - avatar
9 Antworten
+ 3
Thanks Jamie, u were right: I had to use instance of char*. There was one mistake too: the pointer ''temp'' have started with '\0' after reverse function and it is a huge bug. So I've fixed everything and it's looks like this:
28th Jul 2018, 11:30 AM
Petros Simidyan
Petros Simidyan - avatar
+ 2
So, the problem is, that dynamically allocated char array have to end with '\0' and my "input" doesn't. I've fixed that problem and after that I haven't occurring error by releasing the memory. The problem is, it still doesn't work.
27th Jul 2018, 1:12 PM
Petros Simidyan
Petros Simidyan - avatar
+ 1
#include <iostream> #include <string> #include <random> #define MAX 21 inline void reverseWord( char* input, const int inputLen) { char *temp = new char[inputLen]; int end = inputLen, start = 0; while (end-- > 0) temp[start++] = input[end]; delete[] input; input = temp; } inline void printString(const char* input) { int i = 0; while(input[i]!='\0') std::cout << input[i++]; std::cout << std::endl; } inline void strInit(char* input, const int len) { for (int i = 0; i < len-1; i++) input[i] = rand()%2?'a':'b'; input[len - 1] = '\0'; } //inline char* countSort(char* wordToSort, const int& wordLen) //{ // char * tempWord = new char[wordLen]; // if (wordLen>0) // { // int *count = new int[CHAR]; // for (int i = 0; i <wordLen-1; i++) // count[(int)wordToSort[i]]++; // int j = 0; // for (int i = 0; i < CHAR; i++) // { // if (count[i] > 0) // while (count[i]-- > 0) // tempWord[j++] = (char)i; // } // tempWord[j] = '\0'; // printString(tempWord); // delete[] wordToSort; // wordToSort = tempWord; // } // return wordToSort; //} int main() { char* input = new char[MAX]; strInit(input, MAX); printString(input); reverseWord(input, MAX); printString(input); std::cin.get(); return 0; }
27th Jul 2018, 10:00 PM
Petros Simidyan
Petros Simidyan - avatar
+ 1
No error, but still doesn't work. Pointer input points to something wrong, after reverseWord function
27th Jul 2018, 10:01 PM
Petros Simidyan
Petros Simidyan - avatar
+ 1
#include <iostream> #include <string> #include <random> #define MAX 21 inline void reverseWord( char* &input, const int inputLen) { char *temp = new char[inputLen]; int end = inputLen-1, start = 0; while (end-- > 0) temp[start++] = input[end]; temp[start]='\0'; delete[] input; input = temp; } inline void printString(const char* input) { int i = 0; while(input[i]!='\0') std::cout << input[i++]; std::cout << std::endl; } inline void strInit(char* input, const int len) { for (int i = 0; i < len-1; i++) input[i] = rand()%2?'a':'b'; input[len - 1] = '\0'; } //inline char* countSort(char* wordToSort, const int& wordLen) //{ // char * tempWord = new char[wordLen]; // if (wordLen>0) // { // int *count = new int[CHAR]; // for (int i = 0; i <wordLen-1; i++) // count[(int)wordToSort[i]]++; // int j = 0; // for (int i = 0; i < CHAR; i++) // { // if (count[i] > 0) // while (count[i]-- > 0) // tempWord[j++] = (char)i; // } // tempWord[j] =
28th Jul 2018, 11:30 AM
Petros Simidyan
Petros Simidyan - avatar
0
The pointer that must be deallocated is "temp", isn't it?
26th Jul 2018, 10:36 PM
Disvolviĝo;
Disvolviĝo; - avatar