crt检测到应用程序在堆缓冲区结束后写入内存

时间:2014-03-08 12:10:36

标签: c++

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;
        }
    }

课程:

#include

#include

使用namespace std;

const int SIZE = 80;

class SequenceTokenAnalizer {

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

公共:

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);

};

在main中使用delete []之后我得到crt检测到应用程序在堆缓冲结束后写入内存请helppppp这里我有序列并且必须从当前索引返回到下一个tokken 在主要使用delete []之后我得到crt检测到应用程序在堆缓冲结束后写入内存请helppppp在这里我有序列并且必须从当前索引返回到下一个kk

1 个答案:

答案 0 :(得分:1)

您没有发布构造函数,但我认为"This is a test"是输入,' '是分隔符,currentindex = 0。

现在按照代码

if (Sequence[i2] == delim)

这是错误的,因为i2=0Sequence[0]包含'T'。所以跳到其他部分。

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

i2必须增加4倍才能到达空间。这意味着cnt也将是4,你分配4个字节。

    if (Sequence[i] == delim)

由于i从保存值开始为i2,因此始终为false。所以再次移动到else部分

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

现在您将"This"复制到4字节缓冲区中,但也在第5位写入'\0'


在您编辑之前,您的代码包含了这部分也是错误的:

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

strlen返回没有'\0'的长度,但是strcpy使用它。

此外,这看起来像是一个复制/粘贴错误:

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

我希望i代替i2