0

so I was writing a program which has to check if the input sting is a palindrome or not. And it's actually working but a problem with deleting the array I created during the course of it arouse.

#include <iostream>

using namespace std;

bool checkPalindrome(char* text);
char* clearString(char* src);


int main() 
{
    char buffer[1000];
    cin.getline(buffer, 1000);

    cout << boolalpha << checkPalindrome(buffer) << endl;

    return 0;
}


bool checkPalindrome(char* text)
{
    char* newStr = clearString(text);
    if (!newStr)
       return false;

    int newLen = strlen(newStr);

    for (int i = 0; i < newLen / 2; i++) {
        if (newStr[i] == newStr[newLen - i - 1])
            continue;
        else
            return false;
    }

    //delete[] newStr;

    return true;
}


char* clearString(char* src)
{
    unsigned len = strlen(src);
    unsigned counter = 0;

    for (int i = 0; i < len; i++) {
        if (src[i] == ' '||src[i] == '.'||src[i] == ','||src[i] == '!')
            counter++;
    }

    unsigned newSize = len - counter + 1;
    char* dest = new(nothrow) char[newSize];

    if (!dest) {
        cout << "not enoough memory\n";
        return NULL;
    }

    int i, j;

    for(i = j = 0; j < newSize; ++i, ++j) {
        if(src[i]==' '||src[i]=='.'||src[i]==','||src[i]=='!'||src[i]=='?')
            i++;
        else
            dest[j] = src[i];
    }

    dest[j] = '\0';

    return dest;
}

So the commentated delete in the checkPalindrome function causes a crash if executed and I get the "Heap corruption detected" error. I tried changing the function type to void and delete there and the same thing happened. Any ideas what causes it?

6
  • 3
    Please don't try using memory allocation yourself, that's just error prone and chances are high you don't get it right. Just use appropriate standard c++ containers or std::string, Commented Feb 3, 2016 at 21:03
  • You allocate newSize chars and then write newSize+1 chars. Commented Feb 3, 2016 at 21:05
  • 1
    It's not bad to manage your own memory if you know what you're doing, but it's often unnecessary, as it is in this case. Commented Feb 3, 2016 at 21:09
  • 2
    @Litty "It's not bad to manage your own memory" I think we should consider it really is bad. In most cases it's not necessary. There's a few advanced use cases that are far beyond what the OP's asking for. Commented Feb 3, 2016 at 21:13
  • I believe the "advanced" use cases are more than few, but point taken, it's irrelevant to the OP or the question. Commented Feb 3, 2016 at 21:20

1 Answer 1

2

Your loop copies the '\0' at the end of the string, but then you add another '\0', using one more byte of memory than you allocated.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.