user-0 提问: 停车场的一段代码，感觉是队列的插入问题，大神们来看看呀

回复:
user: user-0
content: 引用 17 楼 Sniper_Pan的回复:大佬不敢当，调过了就好。 未初始化变量调试起来确实比较头疼，所以Effective系列都推荐 使用前定义，定义伴随初始化 我都有声明的时候初始化的习惯的，只是这里赋的是枚举值所以就没初始化嘿嘿嘿

user: user-1
content: 引用 12 楼 u010808402 的回复:呃，使用1000降低sleep值后重现到，你可以搜下VS数据断点的打法。另外，调试过程中，发现有个除0错误。    代码较多，有时间再看吧  对了 你这根源应该是在empty[8]值变为-1，具体调试时由于rand可能位置不一样，需要重点查看什么地方修改了导致整个内存被修改。

user: user-1
content: 呃，使用1000降低sleep值后重现到，你可以搜下VS数据断点的打法。另外，调试过程中，发现有个除0错误。    代码较多，有时间再看吧

user: user-1
content: 大数据是指多大？有无其他重现条件？最好提供下具体数值

user: user-2
content: 之前才看过停车场的代码，现在又来一个

user: user-1
content: 大佬不敢当，调过了就好。 未初始化变量调试起来确实比较头疼，所以Effective系列都推荐 使用前定义，定义伴随初始化

user: user-0
content: #include<stdio.h> #include<Windows.h> #include<stdlib.h> #include<conio.h> #include<time.h>  enum operation{ 	enter = 1, 	inspot, 	leave };//车的状态，1为进入，2为在停车位中，3为离开  #define OK 0 #define ERROR 1 #define OVERFLOW -2 #define TRUE 1 #define FALSE 0  typedef int Status; typedef int ElemType;  typedef struct Car{ 	ElemType number; 	clock_t entertime; 	clock_t leavetime; 	ElemType spotnum; 	Car * next; } *Queueptr; typedef struct LinkedQueue{ 	Queueptr rear; 	Queueptr front; }LinkedQueue; typedef struct SqList{ 	ElemType *number; }SqList; //外部变量 SqList List; ElemType inspotcar; ElemType waitingcar; char output[12][2] = { 0 }; char empty[12] = { 0 }; int pos_i = 13;  Status InitList(SqList &List); Status EnQueue(LinkedQueue &queue); Status DeQueue(LinkedQueue &queue); Status Enter(LinkedQueue &queue, LinkedQueue &linkedqueue_leave); Status EnterQueue(LinkedQueue &queue); ElemType Findusefulspot(void); Status Leave(LinkedQueue &queue); Status Checkempty(ElemType number); Status Check(ElemType number); Status InitQueue(LinkedQueue &queue); Status InputInformation(LinkedQueue &linkedqueue, LinkedQueue &linkedqueue_leave); Status Screen(LinkedQueue &linkedqueue, LinkedQueue &linkedqueue_leave); Status Print(LinkedQueue &linkedqueue_leave); Status EnterOrLeave(LinkedQueue &linkedqueue, LinkedQueue &linkedqueue_leave); Status Information(LinkedQueue &linkedqueue); Status Enterlie(LinkedQueue &queue); Status Operter(ElemType num, LinkedQueue &linkedqueue, LinkedQueue &linkedqueue_leave); Status Read(void); Status EXIT(LinkedQueue &linkedqueue, LinkedQueue &linkedqueue_leave, SqList &List); Status Pict(void); int main(void) { 	LinkedQueue linkedqueue; 	LinkedQueue linkedqueue_leave; 	srand((unsigned)time(NULL)); 	HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE); 	COORD pos = { 32, 11 }; 	SetConsoleTitle(TEXT("停车场管理")); 	SetConsoleCursorPosition(hOut, pos); 	printf("停车场管理\n"); 	pos.X = 40; 	pos.Y = 20; 	SetConsoleCursorPosition(hOut, pos); 	printf("姓名：杨锐彬\n"); 	pos.Y = 21; 	SetConsoleCursorPosition(hOut, pos); 	printf("学号：2015051599\n"); 	Sleep(3000); 	Read(); 	InitList(List); 	InitQueue(linkedqueue); 	InitQueue(linkedqueue_leave); 	InputInformation(linkedqueue, linkedqueue_leave); 	EXIT(linkedqueue, linkedqueue_leave, List); 	system("pause"); 	return 0; } Status EXIT(LinkedQueue &linkedqueue, LinkedQueue &linkedqueue_leave, SqList &List) { 	free(List.number); 	List.number = NULL; 	linkedqueue.front = linkedqueue.rear = NULL; 	linkedqueue_leave.front = linkedqueue.rear = NULL;  	return OK; } Status Read(void) { 	Pict(); 	printf("停车场说明：\n1、停车场总共有12个车位，当进入车位被挡住时将继续等待，比如4，5，6号车位都有车则无法进入1，2，3号车位\n2、车辆停放时间用的是系统时间（clock()函数取的系统时间），所以停放时间都比较小\n"); 	Sleep(6000); 	return OK; } Status InitList(SqList &List) { 	List.number = (ElemType*)malloc(sizeof(ElemType)* 12); 	return OK; } Status InitQueue(LinkedQueue &queue) { 	queue.rear = (Queueptr)malloc(sizeof(Car)); 	queue.front = queue.rear; 	if (!queue.front){ 		exit(OVERFLOW); 	} 	queue.front->next = NULL; 	return OK; } Status InputInformation(LinkedQueue &linkedqueue, LinkedQueue &linkedqueue_leave) { 	system("cls"); 	printf("共有多少车辆在排队中:"); 	fflush(stdout); 	rewind(stdin); 	scanf("%d", &waitingcar); 	EnterQueue(linkedqueue); 	Screen(linkedqueue, linkedqueue_leave); 	return OK; } Status Screen(LinkedQueue &linkedqueue, LinkedQueue &linkedqueue_leave) { 	system("cls"); 	srand((unsigned)time(NULL)); 	inspotcar = (rand() % 12) + 1; 	printf("停车场中已有%d辆车\n", inspotcar); 	Information(linkedqueue_leave); 	ElemType num = waitingcar; 	for (; waitingcar != 0 || inspotcar != 0;){ 		Pict(); 		Print(linkedqueue_leave); 		Sleep(2000); 		EnterOrLeave(linkedqueue, linkedqueue_leave);  	} 	system("cls"); 	Pict(); 	Print(linkedqueue_leave); 	system("pause"); 	return OK; } Status Pict(void) { 	system("cls"); 	printf("          *************************          \n"); 	printf("          *   1       2       3   *          \n"); 	printf("          *                       *          \n"); 	printf("          *   4       5       6   *          \n"); 	printf("***********                       ***********\n"); 	printf("                                             \n"); 	printf("                                             \n"); 	printf("***********                       ***********\n"); 	printf("          *   7       8       9   *          \n"); 	printf("          *                       *          \n"); 	printf("          *   10      11      12  *          \n"); 	printf("          *************************          \n"); 	return OK; } Status Print(LinkedQueue &linkedqueue_leave) { 	HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE); 	COORD pos; 	pos.X = 0; pos.Y = 13; 	SetConsoleCursorPosition(hOut, pos); 	printf("正有%d辆车在排队", waitingcar); 	fflush(stdout); 	pos.Y = 14; 	SetConsoleCursorPosition(hOut, pos); 	printf("正有%d辆车在车场中", inspotcar); 	fflush(stdout); 	pos.Y = 15; 	SetConsoleCursorPosition(hOut, pos); 	for (int i = 0; i < 12; i++){ 		printf("%d号车位有：", i + 1); 		fflush(stdout); 		if (output[i][1] == 0){ 			printf(" \n"); 		} 		else{ 			printf("粤A%d\n", List.number[i]); 		}  	} 	return OK; } Status Information(LinkedQueue &linkedqueue_leave) { 	for (int i = 0; i < inspotcar; i++){ 		Enterlie(linkedqueue_leave); 	} 	return OK; } Status Enterlie(LinkedQueue &linkedqueue_leave) { 	Queueptr point = (Queueptr)malloc(sizeof(Car)); 	if (!point){ 		exit(OVERFLOW); 	} 	point->entertime = clock(); 	point->number = (rand() % 9999) + 1; 	//分配车位 	ElemType num; 	while (1){ 		num = rand() % 12; 		if (Check(num) == OK){ 			output[num][0] = num + 1; 			output[num][1] = 1; 			point->spotnum = num; 			List.number[num] = point->number; 			break; 		} 	} 	linkedqueue_leave.rear->next = point; 	linkedqueue_leave.rear = point; 	return OK; } Status Check(ElemType number) { 	if (output[number][1] == 0){ 		return OK; 	} 	else{ 		return ERROR; 	} } Status EnterOrLeave(LinkedQueue &linkedqueue, LinkedQueue &linkedqueue_leave) { 	if (waitingcar == 0){ 		Operter(leave, linkedqueue, linkedqueue_leave); 		return OK; 	} 	if (inspotcar == 0){ 		Operter(enter, linkedqueue, linkedqueue_leave); 	} 	else{ 		ElemType num; 		if ((rand() % 2) == 0){ 			num = leave; 		} 		else{ 			num = enter; 		} 		Operter(num, linkedqueue, linkedqueue_leave); 		return OK; 	} } Status Operter(ElemType num, LinkedQueue &linkedqueue, LinkedQueue &linkedqueue_leave) { 	if (num == enter){ 		Enter(linkedqueue, linkedqueue_leave); 	} 	else{ 		Leave(linkedqueue_leave); 	} 	return OK; } Status Enter(LinkedQueue &queue, LinkedQueue &linkedqueue_leave) { 	ElemType num = Findusefulspot(); 	if (num == 0){ 		return OK; 	} 	ElemType spot = rand() % num; 	DeQueue(queue); 	EnQueue(linkedqueue_leave); 	linkedqueue_leave.front->entertime = clock(); 	linkedqueue_leave.front->spotnum = empty[spot] + 1; 	linkedqueue_leave.front->number = rand() % 9999; 	output[empty[spot]][1] = 1; 	List.number[empty[spot]] = linkedqueue_leave.front->number; 	inspotcar += 1; 	waitingcar -= 1; 	return OK; } Status DeQueue(LinkedQueue &queue) { 	Queueptr point = queue.front; 	if (!point){ 		exit(OVERFLOW); 	} 	queue.front = queue.front->next; 	free(point); 	return OK; } Status EnQueue(LinkedQueue &queue) { 	Queueptr point = (Queueptr)malloc(sizeof(Car)); 	if (!point){ 		exit(OVERFLOW); 	} 	queue.rear->next = point; 	queue.rear = point;  	return OK; }

user: user-0
content: 贴上改好的代码嘿嘿嘿

user: user-0
content: 引用 13 楼 u010808402 的回复:[quote=引用 12 楼 u010808402 的回复:] 呃，使用1000降低sleep值后重现到，你可以搜下VS数据断点的打法。另外，调试过程中，发现有个除0错误。    代码较多，有时间再看吧  对了 你这根源应该是在empty[8]值变为-1，具体调试时由于rand可能位置不一样，需要重点查看什么地方修改了导致整个内存被修改。[/quote]嘿嘿谢谢大佬的调试，不过empty[8]的值是正确的，那是为了判断是否可行所故意设置的，然后VS我在可能出错的地方都打了断点，用2000的数据测试的，其实昨天下午就测试完成了，问题主要是赋值上，Operer()里面对num的赋值不正确，因为局部变量出来后就没了所以用的是未初始化的值，所以导致了错误，代码我已经改好啦，也用10000测试过没问题的～除0错误主要是因为判断出来的num = 0，这里在前面加一个等于0的情况跳转到某一特殊函数就可以了～谢谢大佬啦

user: user-0
content: 引用 3 楼 paschen的回复:问题不是去看出来的，是调试出来的，出错时点中断，通过调用堆栈、变量窗口观察分析原因  帮你调试时，printf("粤A%d\n", linkedqueue_leave.front->next->number); 这句中linkedqueue_leave.front->next还没有初始化已经使用了，导致出错 对呀对呀我也是这样一步一步过来的，可是我都赋值了呀，不应该不存在未初始化的问题呀

user: user-0
content: 引用 2 楼 赵4老师的回复:仅供参考：#include <windows.h> #include <stdio.h>  void ConPrint(char *CharBuffer, int len); void ConPrintAt(int x, int y, char *CharBuffer, int len); void gotoXY(int x, int y); void ClearConsole(void); void ClearConsoleToColors(int ForgC, int BackC); void SetColorAndBackground(int ForgC, int BackC); void SetColor(int ForgC); void HideTheCursor(void); void ShowTheCursor(void);  int main(int argc, char* argv[]) {    HideTheCursor();    ClearConsoleToColors(15, 1);    ClearConsole();    gotoXY(1, 1);    SetColor(14);    printf("This is a test...\n");    Sleep(5000);    ShowTheCursor();    SetColorAndBackground(15, 12);    ConPrint("This is also a test...\n", 23);    SetColorAndBackground(1, 7);    ConPrintAt(22, 15, "This is also a test...\n", 23);    gotoXY(0, 24);    SetColorAndBackground(7, 1);    return 0; }  //This will clear the console while setting the forground and //background colors. void ClearConsoleToColors(int ForgC, int BackC) {    WORD wColor = ((BackC & 0x0F) << 4) + (ForgC & 0x0F);    //Get the handle to the current output buffer...    HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);    //This is used to reset the carat/cursor to the top left.    COORD coord = {0, 0};    //A return value... indicating how many chars were written    //not used but we need to capture this since it will be    //written anyway (passing NULL causes an access violation).    DWORD count;     //This is a structure containing all of the console info    // it is used here to find the size of the console.    CONSOLE_SCREEN_BUFFER_INFO csbi;    //Here we will set the current color    SetConsoleTextAttribute(hStdOut, wColor);    if(GetConsoleScreenBufferInfo(hStdOut, &csbi))    {       //This fills the buffer with a given character (in this case 32=space).       FillConsoleOutputCharacter(hStdOut, (TCHAR) 32, csbi.dwSize.X * csbi.dwSize.Y, coord, &count);        FillConsoleOutputAttribute(hStdOut, csbi.wAttributes, csbi.dwSize.X * csbi.dwSize.Y, coord, &count);       //This will set our cursor position for the next print statement.       SetConsoleCursorPosition(hStdOut, coord);    } }  //This will clear the console. void ClearConsole() {    //Get the handle to the current output buffer...    HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);    //This is used to reset the carat/cursor to the top left.    COORD coord = {0, 0};    //A return value... indicating how many chars were written    //   not used but we need to capture this since it will be    //   written anyway (passing NULL causes an access violation).    DWORD count;    //This is a structure containing all of the console info    // it is used here to find the size of the console.    CONSOLE_SCREEN_BUFFER_INFO csbi;    //Here we will set the current color    if(GetConsoleScreenBufferInfo(hStdOut, &csbi))    {       //This fills the buffer with a given character (in this case 32=space).       FillConsoleOutputCharacter(hStdOut, (TCHAR) 32, csbi.dwSize.X * csbi.dwSize.Y, coord, &count);       FillConsoleOutputAttribute(hStdOut, csbi.wAttributes, csbi.dwSize.X * csbi.dwSize.Y, coord, &count);       //This will set our cursor position for the next print statement.       SetConsoleCursorPosition(hStdOut, coord);    } }  //This will set the position of the cursor void gotoXY(int x, int y) {    //Initialize the coordinates    COORD coord = {x, y};    //Set the position    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord); }  //This will set the forground color for printing in a console window. void SetColor(int ForgC) {    WORD wColor;    //We will need this handle to get the current background attribute    HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);    CONSOLE_SCREEN_BUFFER_INFO csbi;     //We use csbi for the wAttributes word.    if(GetConsoleScreenBufferInfo(hStdOut, &csbi))    {       //Mask out all but the background attribute, and add in the forgournd color       wColor = (csbi.wAttributes & 0xF0) + (ForgC & 0x0F);       SetConsoleTextAttribute(hStdOut, wColor);    } }  //This will set the forground and background color for printing in a console window. void SetColorAndBackground(int ForgC, int BackC) {    WORD wColor = ((BackC & 0x0F) << 4) + (ForgC & 0x0F);;    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), wColor); }  //Direct console output void ConPrint(char *CharBuffer, int len) {    DWORD count;    WriteConsole(GetStdHandle(STD_OUTPUT_HANDLE), CharBuffer, len, &count, NULL); }  //Direct Console output at a particular coordinate. void ConPrintAt(int x, int y, char *CharBuffer, int len) {    DWORD count;    COORD coord = {x, y};    HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);    SetConsoleCursorPosition(hStdOut, coord);    WriteConsole(hStdOut, CharBuffer, len, &count, NULL); }  //Hides the console cursor void HideTheCursor() {    CONSOLE_CURSOR_INFO cciCursor;    HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);     if(GetConsoleCursorInfo(hStdOut, &cciCursor))    {       cciCursor.bVisible = FALSE; 	  SetConsoleCursorInfo(hStdOut, &cciCursor);    } }  //Shows the console cursor void ShowTheCursor() {    CONSOLE_CURSOR_INFO cciCursor;    HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);     if(GetConsoleCursorInfo(hStdOut, &cciCursor))    {       cciCursor.bVisible = TRUE; 	  SetConsoleCursorInfo(hStdOut, &cciCursor);    } }   老师好我是知道可以写gotoxy函数来实现的啦，只是这是个小作业不敢写这个，老师会说我侧重点不对的我们老师就这么死板

user: user-3
content: 问题不是去看出来的，是调试出来的，出错时点中断，通过调用堆栈、变量窗口观察分析原因  帮你调试时，printf("粤A%d\n", linkedqueue_leave.front->next->number); 这句中linkedqueue_leave.front->next还没有初始化已经使用了，导致出错

user: user-4
content: 仅供参考：#include <windows.h> #include <stdio.h>  void ConPrint(char *CharBuffer, int len); void ConPrintAt(int x, int y, char *CharBuffer, int len); void gotoXY(int x, int y); void ClearConsole(void); void ClearConsoleToColors(int ForgC, int BackC); void SetColorAndBackground(int ForgC, int BackC); void SetColor(int ForgC); void HideTheCursor(void); void ShowTheCursor(void);  int main(int argc, char* argv[]) {    HideTheCursor();    ClearConsoleToColors(15, 1);    ClearConsole();    gotoXY(1, 1);    SetColor(14);    printf("This is a test...\n");    Sleep(5000);    ShowTheCursor();    SetColorAndBackground(15, 12);    ConPrint("This is also a test...\n", 23);    SetColorAndBackground(1, 7);    ConPrintAt(22, 15, "This is also a test...\n", 23);    gotoXY(0, 24);    SetColorAndBackground(7, 1);    return 0; }  //This will clear the console while setting the forground and //background colors. void ClearConsoleToColors(int ForgC, int BackC) {    WORD wColor = ((BackC & 0x0F) << 4) + (ForgC & 0x0F);    //Get the handle to the current output buffer...    HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);    //This is used to reset the carat/cursor to the top left.    COORD coord = {0, 0};    //A return value... indicating how many chars were written    //not used but we need to capture this since it will be    //written anyway (passing NULL causes an access violation).    DWORD count;     //This is a structure containing all of the console info    // it is used here to find the size of the console.    CONSOLE_SCREEN_BUFFER_INFO csbi;    //Here we will set the current color    SetConsoleTextAttribute(hStdOut, wColor);    if(GetConsoleScreenBufferInfo(hStdOut, &csbi))    {       //This fills the buffer with a given character (in this case 32=space).       FillConsoleOutputCharacter(hStdOut, (TCHAR) 32, csbi.dwSize.X * csbi.dwSize.Y, coord, &count);        FillConsoleOutputAttribute(hStdOut, csbi.wAttributes, csbi.dwSize.X * csbi.dwSize.Y, coord, &count);       //This will set our cursor position for the next print statement.       SetConsoleCursorPosition(hStdOut, coord);    } }  //This will clear the console. void ClearConsole() {    //Get the handle to the current output buffer...    HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);    //This is used to reset the carat/cursor to the top left.    COORD coord = {0, 0};    //A return value... indicating how many chars were written    //   not used but we need to capture this since it will be    //   written anyway (passing NULL causes an access violation).    DWORD count;    //This is a structure containing all of the console info    // it is used here to find the size of the console.    CONSOLE_SCREEN_BUFFER_INFO csbi;    //Here we will set the current color    if(GetConsoleScreenBufferInfo(hStdOut, &csbi))    {       //This fills the buffer with a given character (in this case 32=space).       FillConsoleOutputCharacter(hStdOut, (TCHAR) 32, csbi.dwSize.X * csbi.dwSize.Y, coord, &count);       FillConsoleOutputAttribute(hStdOut, csbi.wAttributes, csbi.dwSize.X * csbi.dwSize.Y, coord, &count);       //This will set our cursor position for the next print statement.       SetConsoleCursorPosition(hStdOut, coord);    } }  //This will set the position of the cursor void gotoXY(int x, int y) {    //Initialize the coordinates    COORD coord = {x, y};    //Set the position    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord); }  //This will set the forground color for printing in a console window. void SetColor(int ForgC) {    WORD wColor;    //We will need this handle to get the current background attribute    HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);    CONSOLE_SCREEN_BUFFER_INFO csbi;     //We use csbi for the wAttributes word.    if(GetConsoleScreenBufferInfo(hStdOut, &csbi))    {       //Mask out all but the background attribute, and add in the forgournd color       wColor = (csbi.wAttributes & 0xF0) + (ForgC & 0x0F);       SetConsoleTextAttribute(hStdOut, wColor);    } }  //This will set the forground and background color for printing in a console window. void SetColorAndBackground(int ForgC, int BackC) {    WORD wColor = ((BackC & 0x0F) << 4) + (ForgC & 0x0F);;    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), wColor); }  //Direct console output void ConPrint(char *CharBuffer, int len) {    DWORD count;    WriteConsole(GetStdHandle(STD_OUTPUT_HANDLE), CharBuffer, len, &count, NULL); }  //Direct Console output at a particular coordinate. void ConPrintAt(int x, int y, char *CharBuffer, int len) {    DWORD count;    COORD coord = {x, y};    HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);    SetConsoleCursorPosition(hStdOut, coord);    WriteConsole(hStdOut, CharBuffer, len, &count, NULL); }  //Hides the console cursor void HideTheCursor() {    CONSOLE_CURSOR_INFO cciCursor;    HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);     if(GetConsoleCursorInfo(hStdOut, &cciCursor))    {       cciCursor.bVisible = FALSE; 	  SetConsoleCursorInfo(hStdOut, &cciCursor);    } }  //Shows the console cursor void ShowTheCursor() {    CONSOLE_CURSOR_INFO cciCursor;    HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);     if(GetConsoleCursorInfo(hStdOut, &cciCursor))    {       cciCursor.bVisible = TRUE; 	  SetConsoleCursorInfo(hStdOut, &cciCursor);    } }

user: user-0
content: 光标的移动pos_i可以忽略，因为清屏了所以原本的输出没办法保留

user: user-0
content: Status Checkempty(ElemType number) { 	for (int i = 0; i <= number; i++){ 		if (empty[i] == 0){ 			if ((output[1][1] == 1) && (output[3][1] == 1)){ 				empty[i] = -1; 			} 			if ((output[3][1] == 1) && (output[4][1] == 1) && (output[5][1] == 1)){ 				empty[i] = -1; 			} 			if ((output[4][1] == 1) && (output[3][1] == 1) && (output[2][1] == 1)){ 				empty[i] = -1; 			} 			if ((output[1][1] == 1) && (output[3][1] == 1) && (output[4][1] == 1)){ 				empty[i] = -1; 			} 		} 		if (empty[i] == 1){ 			if ((output[3][1] == 1) && (output[4][1] == 1) && (output[5][1] == 1)){ 				empty[i] = -1; 			} 			if ((output[4][1] == 1) && (output[3][1] == 1) && (output[2][1] == 1)){ 				empty[i] = -1; 			} 			if ((output[0][1] == 1) && (output[4][1] == 1) && (output[5][1] == 1)){ 				empty[i] = -1; 			} 			if ((output[0][1] == 1) && (output[2][1] == 1) && (output[4][1] == 1)){ 				empty[i] = -1; 			} 		} 		if (empty[i] == 2){ 			if ((output[3][1] == 1) && (output[4][1] == 1) && (output[5][1] == 1)){ 				empty[i] = -1; 			} 			if ((output[1][1] == 1) && (output[5][1] == 1)){ 				empty[i] = -1; 			} 			if ((output[0][1] == 1) && (output[4][1] == 1) && (output[5][1] == 1)){ 				empty[i] = -1; 			} 			if ((output[1][1] == 1) && (output[4][1] == 1) && (output[5][1] == 1)){ 				empty[i] = -1; 			} 		} 		if (empty[i] == 9){ 			if ((output[6][1] == 1) && (output[10][1] == 1)){ 				empty[i] = -1; 			} 			if ((output[6][1] == 1) && (output[7][1] == 1) && (output[8][1] == 1)){ 				empty[i] = -1; 			} 			if ((output[6][1] == 1) && (output[7][1] == 1) && (output[11][1] == 1)){ 				empty[i] = -1; 			} 			if ((output[6][1] == 1) && (output[7][1] == 1) && (output[10][1] == 1)){ 				empty[i] = -1; 			} 		} 		if (empty[i] == 10){ 			if ((output[6][1] == 1) && (output[7][1] == 1) && (output[8][1] == 1)){ 				empty[i] = -1; 			} 			if ((output[6][1] == 1) && (output[7][1] == 1) && (output[11][1] == 1)){ 				empty[i] = -1; 			} 			if ((output[7][1] == 1) && (output[8][1] == 1) && (output[9][1] == 1)){ 				empty[i] = -1; 			} 			if ((output[7][1] == 1) && (output[9][1] == 1) && (output[11][1] == 1)){ 				empty[i] = -1; 			} 		} 		if (empty[i] == 11){ 			if ((output[6][1] == 1) && (output[7][1] == 1) && (output[8][1] == 1)){ 				empty[i] = -1; 			} 			if ((output[7][1] == 1) && (output[8][1] == 1) && (output[10][1] == 1)){ 				empty[i] = -1; 			} 			if ((output[7][1] == 1) && (output[8][1] == 1) && (output[9][1] == 1)){ 				empty[i] = -1; 			} 			if ((output[8][1] == 1) && (output[10][1] == 1)){ 				empty[i] = -1; 			} 		} 	} 	return OK; } Status Leave(LinkedQueue &queue) { 	ElemType em[12]; 	ElemType j = 0; 	for (int i = 0; i < 12; i++){ 		if (output[i][1] == 1){ 			em[j] = i; 			j += 1; 		} 	} 	ElemType num = rand() % j; 	output[em[num]][1] = 0; 	inspotcar -= 1;  	HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE); 	COORD pos = { 0, 27 }; 	SetConsoleCursorPosition(hOut, pos); 	printf("车牌为粤A%d的车出停车场，总停时间为：%.3lf秒", List.number[em[num]], ((double)(((queue.front->leavetime) - (queue.front->leavetime)) / CLOCKS_PER_SEC))); 	fflush(stdout); 	DeQueue(queue); 	Sleep(1000); 	return OK; }

user: user-0
content: #include<stdio.h> #include<Windows.h> #include<stdlib.h> #include<conio.h> #include<time.h>  enum operation{ 	enter = 1, 	inspot, 	leave };//车的状态，1为进入，2为在停车位中，3为离开  #define OK 0 #define ERROR 1 #define OVERFLOW -2 #define TRUE 1 #define FALSE 0  typedef int Status; typedef int ElemType;  typedef struct Car{ 	ElemType number = 0; 	clock_t entertime; 	clock_t leavetime; 	ElemType spotnum; 	Car * next; } *Queueptr; typedef struct LinkedQueue{ 	Queueptr rear; 	Queueptr front; 	int length = 0; }LinkedQueue; typedef struct SqList{ 	ElemType *number; }; //外部变量 SqList List; ElemType inspotcar; ElemType waitingcar; char output[12][2] = { 0 }; char empty[12] = { 0 }; int pos_i = 13;  Status InitList(SqList &List); Status EnQueue(LinkedQueue &queue); Status DeQueue(LinkedQueue &queue); Status Enter(LinkedQueue &queue,LinkedQueue &linkedqueue_leave); Status EnterQueue(LinkedQueue &queue); ElemType Findusefulspot(void); Status Leave(LinkedQueue &queue); Status Checkempty(ElemType number); Status Check(ElemType number); Status InitQueue(LinkedQueue &queue); Status InputInformation(LinkedQueue &linkedqueue, LinkedQueue &linkedqueue_leave); Status Screen(LinkedQueue &linkedqueue, LinkedQueue &linkedqueue_leave); Status Print(LinkedQueue &linkedqueue_leave); Status EnterOrLeave(LinkedQueue &linkedqueue, LinkedQueue &linkedqueue_leave); Status Information(LinkedQueue &linkedqueue); Status Enterlie(LinkedQueue &queue); Status Operter(ElemType num, LinkedQueue &linkedqueue, LinkedQueue &linkedqueue_leave); Status Read(void); Status EXIT(LinkedQueue &linkedqueue, LinkedQueue &linkedqueue_leave, SqList &List); int main(void) { 	LinkedQueue linkedqueue; 	LinkedQueue linkedqueue_leave; 	srand((unsigned)time(NULL)); 	HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE); 	COORD pos = { 32, 11 }; 	SetConsoleTitle(TEXT("停车场管理")); 	SetConsoleCursorPosition(hOut, pos); 	printf("停车场管理\n"); 	Sleep(1000); 	Read(); 	InitList(List); 	InitQueue(linkedqueue); 	InitQueue(linkedqueue_leave); 	InputInformation(linkedqueue, linkedqueue_leave); 	EXIT(linkedqueue,linkedqueue_leave,List); 	system("pause"); 	return 0; } Status EXIT(LinkedQueue &linkedqueue, LinkedQueue &linkedqueue_leave, SqList &List) { 	return OK; } Status Read(void) { 	system("cls"); 	printf("          *************************          \n"); 	printf("          *   1       2       3   *          \n"); 	printf("          *                       *          \n"); 	printf("          *   4       5       6   *          \n"); 	printf("***********                       ***********\n"); 	printf("                                             \n"); 	printf("                                             \n"); 	printf("***********                       ***********\n"); 	printf("          *   7       8       9   *          \n"); 	printf("          *                       *          \n"); 	printf("          *   10      11      12  *          \n"); 	printf("          *************************          \n"); 	printf("停车场说明：\n停车场总共有12个车位，当进入车位被挡住时将继续等待，比如4，5，6号车位都有车则无法进入1，2，3号车位\n"); 	Sleep(3000); 	return OK; } Status InitList(SqList &List) { 	List.number = (ElemType*)malloc(sizeof(ElemType)* 12); 	return OK; } Status InitQueue(LinkedQueue &queue) { 	queue.rear= (Queueptr)malloc(sizeof(Car)); 	queue.front = queue.rear; 	if (!queue.front){ 		exit(OVERFLOW); 	} 	queue.front->next = NULL; 	return OK; } Status InputInformation(LinkedQueue &linkedqueue, LinkedQueue &linkedqueue_leave) { 	system("cls"); 	printf("共有多少车辆在排队中:"); 	fflush(stdout); 	rewind(stdin); 	scanf_s("%d", &waitingcar); 	EnterQueue(linkedqueue); 	Screen(linkedqueue, linkedqueue_leave); 	return OK; } Status Screen(LinkedQueue &linkedqueue, LinkedQueue &linkedqueue_leave) { 	system("cls"); 	srand((unsigned)time(NULL)); 	inspotcar = (rand() % 12) + 1; 	printf("停车场中已有%d辆车\n", inspotcar); 	Information(linkedqueue_leave); 	ElemType num = waitingcar; 	for (int i = 0; i <num ||(inspotcar>0); i++){ 		system("cls"); 		printf("          *************************          \n"); 		printf("          *   1       2       3   *          \n"); 		printf("          *                       *          \n"); 		printf("          *   4       5       6   *          \n"); 		printf("***********                       ***********\n"); 		printf("                                             \n"); 		printf("                                             \n"); 		printf("***********                       ***********\n"); 		printf("          *   7       8       9   *          \n"); 		printf("          *                       *          \n"); 		printf("          *   10      11      12  *          \n"); 		printf("          *************************          \n"); 		Print(linkedqueue_leave); 		Sleep(2000); 		EnterOrLeave(linkedqueue, linkedqueue_leave); 		 	} 	system("cls"); 	Print(linkedqueue_leave); 	system("pause"); 	return OK; } Status Print(LinkedQueue &linkedqueue_leave) { 	HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE); 	COORD pos; 	pos = { 0, 13 }; 	SetConsoleCursorPosition(hOut, pos); 	printf("正有%d辆车在排队", waitingcar); 	fflush(stdout); 	pos = { 0, 14 }; 	SetConsoleCursorPosition(hOut, pos); 	printf("正有%d辆车在车场中", inspotcar); 	fflush(stdout); 	pos = { 0, 15 }; 	SetConsoleCursorPosition(hOut, pos); 	for (int i = 0; i < 12; i++){ 		printf("%d号车位有：", i + 1); 		fflush(stdout); 		if (output[i][1] == 0){ 			printf(" \n"); 		} 		else{ 			printf("粤A%d\n",List.number[i]); 		} 		 	} 	return OK; } Status Information(LinkedQueue &linkedqueue_leave) { 	for (int i = 0; i < inspotcar; i++){ 		Enterlie(linkedqueue_leave); 	} 	return OK; } Status Enterlie(LinkedQueue &linkedqueue_leave) { 	Queueptr point = (Queueptr)malloc(sizeof(Car)); 	if (!point){ 		exit(OVERFLOW); 	} 	point->entertime = clock(); 	point->number = (rand() % 9999) + 1; 	//分配车位 	ElemType num; 	while (1){ 		num = rand() % 12; 		if (Check(num) == OK){ 			output[num][0] = num + 1; 			output[num][1] = 1; 			point->spotnum = num; 			List.number[num] = point->number; 			break; 		} 	} 	linkedqueue_leave.rear->next = point; 	linkedqueue_leave.rear = point; 	return OK; } Status Check(ElemType number) { 	if (output[number][1] == 0){ 		return OK; 	} 	else{ 		return ERROR; 	} } Status EnterOrLeave(LinkedQueue &linkedqueue, LinkedQueue &linkedqueue_leave) { 	ElemType num; 	if (waitingcar == 0){ 		num = leave; 	} 	if (inspotcar == 0){ 		num = enter; 	} 	else{ 		if ((rand() % 2) == 0){ 			num = leave; 		} 		else{ 			num = enter; 		} 	} 	Operter(num, linkedqueue, linkedqueue_leave); 	return OK; } Status Operter(ElemType num, LinkedQueue &linkedqueue, LinkedQueue &linkedqueue_leave) { 	if (num == enter){ 		Enter(linkedqueue,linkedqueue_leave); 	} 	else{ 		Leave(linkedqueue_leave); 	} 	return OK; } Status Enter(LinkedQueue &queue,LinkedQueue &linkedqueue_leave) { 	ElemType num = Findusefulspot(); 	if (num == 0){ 		return OK; 	} 	ElemType spot = (rand() % (num + 1)); 	DeQueue(queue); 	EnQueue(linkedqueue_leave); 	linkedqueue_leave.front->entertime = clock(); 	linkedqueue_leave.front->spotnum = empty[spot] + 1; 	linkedqueue_leave.front->number = rand() % 9999; 	output[empty[spot]][1] = 1; 	List.number[empty[spot]] = linkedqueue_leave.front->number; 	inspotcar += 1; 	waitingcar -= 1; 	return OK; } Status DeQueue(LinkedQueue &queue) { 	Queueptr point = queue.front; 	if (!point){ 		exit(OVERFLOW); 	} 	queue.front = queue.front->next; 	free(point); 	return OK; } Status EnQueue(LinkedQueue &queue) { 	Queueptr point = (Queueptr)malloc(sizeof(Car)); 	if (!point){ 		exit(OVERFLOW); 	} 	queue.rear->next = point; 	queue.rear = point;  	return OK; } Status EnterQueue(LinkedQueue &linkedqueue) { 	for (int i = 0; i < waitingcar; i++){ 		EnQueue(linkedqueue); 	} 	return OK; } ElemType Findusefulspot(void) { 	if (inspotcar == 12){ 		return 0; 	} 	int j = 0; 	for (int i = 0; i < 12; i++){ 		if (output[i][1] == 0){ 			empty[j] = i; 			j += 1; 		} 	} 	Checkempty(j); 	ElemType em[13]; 	ElemType k = 0; 	for (int i = 0; i < j; i++){ 		if (empty[i] == -1){ 			; 		} 		else{ 			em[k] = empty[i]; 			k += 1; 		} 	} 	for (int i = 0; i < k; i++){ 		empty[i] = em[i]; 	} 	return k; }  }

user: user-0
content: 引用 6 楼 qq423399099 的回复:出现错误是：0xC0000005:  读取位置 0xCDCDCDDD 时发生访问冲突。 这是对未初始化过的堆上申请的变量进行了操作。 printf("粤A%d\n", linkedqueue_leave.front->next->number);你这句的linkedqueue_leave.front->next并没有对其赋过值嘿嘿这个我解决啦，明白啦～不过还有一个新的问题，我把新的代码放在下一楼，这段代码会在大数据测试的时候，外部变量waitingcar突然一次性增大16的6次方，这个我断点找了好久还是没找出来原因。

user: user-5
content: 出现错误是：0xC0000005:  读取位置 0xCDCDCDDD 时发生访问冲突。 这是对未初始化过的堆上申请的变量进行了操作。 printf("粤A%d\n", linkedqueue_leave.front->next->number);你这句的linkedqueue_leave.front->next并没有对其赋过值