这段代码哪里内存溢出了呢?

代码问题太多,不建议继续观看。。。
 
 
//============================================================================
// Name        : DoubleBuffer.cpp
// Author      : Vit
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

using namespace std;

#define MIDBUFFER 1023
#define WHOLEBUFFER 2048
#define MAXWORD 10000

//Double buffer
struct bibuffer
{
	char *buffer;            		//缓冲区空间
	char *forward;     	            //后向指针
	int count;                   	//前向指针记数
	int state;						//当前的状态
};

//lexical analysis
//scheme
int State[8][5];

//set final status
const bool Acstatus[7] =
{ 0, 1, 1, 0, 0, 1, 0};

//for JudgeNum
int JudgeNum(char &ch)
{
	if (ch >= '0' && ch <= '9')
		return 0;
	else if (ch == '.')
		return 1;
	else if (ch == 'E')
		return 2;
	else if (ch == '+' || ch == '-')
		return 3;
	else
		return 4;
}

//initial the scheme
void init()
{
	//set error state
	for (int i = 0; i < 8; i++)
		for (int j = 0; j < 5; j++)
		{
			State[i][j] = 7;
		}

	//set table
	State[0][0] = 1;
	State[0][1] = 6;
	State[1][0] = 1;
	State[1][1] = 2;
	State[1][2] = 5;
	State[2][0] = 2;
	State[2][2] = 3;
	State[3][3] = 4;
	State[4][0] = 5;
	State[5][0] = 5;
	State[6][0] = 2;
}

//parse, change state due to char;
void parse(int &state, char &ch, char *cur)
{
	state = State[state][JudgeNum(ch)];
	if (state != 7)		//可以在此考虑拼接问题
		*cur = ch;
}

bool FinalState(int &state)
{
	return state == 7;

}

void InitBuff(bibuffer *bbuf, FILE *fp, char *buffer)
{

	fread(buffer, MIDBUFFER, 1, fp);					//first buffer
	buffer[MIDBUFFER] = EOF;							    //put the end
	fread(buffer + MIDBUFFER + 1, MIDBUFFER, 1, fp);	//next buffer
	buffer[WHOLEBUFFER - 1] = EOF;						    //put the end

    bbuf->buffer = buffer;
	bbuf->forward = bbuf->buffer;
	bbuf->count = 0;
	bbuf->state = 0;

}

//分析源文件,每次返回一个单词
void Analysis( FILE *fp)
{
    int i;
	bibuffer *bbuf;
	char *temp, *buffer;        //用于记录当前的位置


    bbuf = new bibuffer();
    temp = new char[MAXWORD];
    buffer= new char[MAXWORD];
    i = 0;

    memset(temp, '\0', MAXWORD);

	while (1)
	{
        InitBuff(bbuf, fp, buffer);
		//count值位于第一缓冲区或者第二缓冲区中
		if ((bbuf->count < MIDBUFFER || ((bbuf->count > MIDBUFFER) && (bbuf->count < WHOLEBUFFER)))
				&& !FinalState(bbuf->state))
		{
			//parse state, char
			bbuf->state = State[bbuf->state][JudgeNum(*(bbuf->forward))];
            if (bbuf->state != 7)		//可以在此考虑拼接问题
                temp[i] = *(bbuf->forward);
            i++;

			bbuf->forward++;
			bbuf->count++;
		}
		//count值超出第一缓冲区
		else if (bbuf->count == MIDBUFFER && !FinalState(bbuf->state))
		{
			//read next buffer
			fread((bbuf->buffer) + MIDBUFFER + 1, MIDBUFFER, 1, fp);

            bbuf->forward++;
			bbuf->count++;

            bbuf->state = State[bbuf->state][JudgeNum(*(bbuf->forward))];
            if (bbuf->state != 7)		//可以在此考虑拼接问题
                temp[i] = *(bbuf->forward);
            i++;
		}
		//count值超出第二缓冲区
		else if (bbuf->count >= WHOLEBUFFER && !FinalState(bbuf->state))
		{
			//read first buffer
			fread((bbuf->buffer) + MIDBUFFER + 1, MIDBUFFER, 1, fp);
            bbuf->forward = bbuf->buffer;
			bbuf->count = 0;

            bbuf->state = State[bbuf->state][JudgeNum(*(bbuf->forward))];
            if (bbuf->state != 7)		//可以在此考虑拼接问题
                temp[i] = *(bbuf->forward);
            i++;
		}
		//识别出一个单词
		else if (FinalState(bbuf->state))				//当一个终态出现
		{
			if (temp[0] != '\0')
				printf("%s ", temp);
			memset(temp, '\0', MAXWORD);
			i = 0;
			bbuf->forward = bbuf->forward + 1;
			//count 不用改变
		}

		if (bbuf->count != MIDBUFFER && bbuf->count < WHOLEBUFFER
				&& *(bbuf->forward) == EOF)
		{
			break; 		//analysis end
		}

	}
	delete temp;
	delete bbuf;
	delete buffer;
}

int main()
{
	//Declare the variables
	FILE* fp;

	//initial
	init();
	fp = fopen("test", "r");

	Analysis(fp);

	return 0;
}

你可能感兴趣的:(C++)