user3395991
user3395991

Reputation: 11

crt detected that the application wrote to memory after end of heap buffer

char* SequenceTokenAnalizer::NextToken(char delim) {

int main()
{
SequenceTokenAnalizer st1("This is a test");
    char* helpSequence;
    helpSequence = st1.NextToken();
    cout << helpSequence << endl;
    delete[]  helpSequence;
}

int i = currentindex, i2 = currentindex, cnt = 0, j = 0;
char *token=NULL;

if (Sequence[i2] == delim)
{
    while (Sequence[i2] == delim&& Sequence[i2] != '\0')
    {
        i2++;

    }
    while (Sequence[i2] != delim&& Sequence[i2]!='\0')
    {
        cnt++;
        i2++;
    }
    token = new char[cnt];
    if (Sequence[i] == delim)
    {
        while (Sequence[i] == delim)
        {
            i++;

        }
        while (Sequence[i] != delim&& Sequence[i2] != '\0')
        {
            token[j] = Sequence[i];
            i++;
            j++;
        }
        token[j] = '\0';
        currentindex = i;
        return token;
    }
}
    else
    {
        while (Sequence[i2] != delim)
        {
            cnt++;
            i2++;
        }
        token = new char[cnt];


        if (Sequence[i] == delim)
        {
            while (Sequence[i] == delim)
            {
                i++;

            }
            while (Sequence[i] != delim)
            {
                token[j] = Sequence[i];
                i++;
                j++;
            }
            token[j] = '\0';
            currentindex = i;
            return token;
        }
        else
        {
            while (Sequence[i] != delim)
            {
                token[j] = Sequence[i];
                i++;
                j++;
            }
            token[j] = '\0';
            currentindex = i;
            return token;
        }
    }

class:

#include

#include

using namespace std;

const int SIZE = 80;

class SequenceTokenAnalizer {

char Sequence[SIZE];
char delimiter;
int currentindex;

public:

SequenceTokenAnalizer(char str[]);

SequenceTokenAnalizer(char str[], char delim);

int LengthSequence();

int CountAllTokens();
void ResetTokens();
int CountTokens();
bool HasMoreTokens();
bool HasMoreTokens(char delim);
char* NextToken();
char* NextToken(char delim);
bool Equals(SequenceTokenAnalizer other);
bool NotEquals(SequenceTokenAnalizer other);
bool isCommonToken(SequenceTokenAnalizer other);
void PrintCommonTokens(SequenceTokenAnalizer other);

};

after using delete[] in main I get crt detected that the application wrote to memory after end of heap buffer please helppppp here i have sequences and have to return next tokken from current index after using delete[] in main I get crt detected that the application wrote to memory after end of heap buffer please helppppp here i have sequences and have to return next tokken from current index

Upvotes: 1

Views: 4098

Answers (1)

wimh
wimh

Reputation: 15232

You did not post the constructor, but I asume "This is a test" is the input and ' ' is the delimiter and currentindex=0.

Now follow the code

if (Sequence[i2] == delim)

This is false, because i2=0 and Sequence[0] contains 'T'. So skip to the else part.

    while (Sequence[i2] != delim)
    {
        cnt++;
        i2++;
    }
    token = new char[cnt];

i2 must be increased 4 times to reach the space. That means cnt will also be 4 and you allocate 4 bytes.

    if (Sequence[i] == delim)

Because i starts at the save value as i2 this is always false. So again move to the else part

        while (Sequence[i] != delim)
        {
            token[j] = Sequence[i];
            i++;
            j++;
        }
        token[j] = '\0';

Now you are copying "This" into the 4-byte buffer, but also write '\0' at the 5th position.


Before your edit your code contained this part which was also wrong:

token2 = new char[strlen(token)];
strcpy(token2, token);

strlen returns the length without the '\0', but strcpy uses it.

Also, this looks like a copy/paste mistake:

while (Sequence[i] != delim&& Sequence[i2] != '\0')

I would expect i instead of i2

Upvotes: 1

Related Questions