user-0 提问: C语言动态分配问题

回复:
user: user-0
content: 代码如下：//这是我的课程设计作业：飞机票系统。汗，好像有点多，不过我已经删减了大部分，第一次发帖，请大家见谅，监听到的错误是lol，当执行到红色代码时，指针值会变为0x1！ #include<stdio.h> #include<string.h> #include<stdlib.h> #include<time.h> #include<conio.h> #include"list.h" #include"queue.h" #pragma warning(disable:4996) struct route	//航线 { 	int plane;//飞机号 	int num;//航班号 	char finaltion[10];//终点站名 	char date[5];//飞行日期 	int people;//成员定额 	int ticket;//余票额 	struct finisheder* fclient;//已定票的客户名单 	struct unfinisheder* uclient;//预约登记的客户名单 }; void main() { 	int selection; 	int loop=0; 	int aa, bb, cc, dd; 	char ee[10], ff[5]; 	int length = sizeof(aa + bb + cc + dd) + sizeof(ee)+sizeof(ff);//用于读取文件fp的每一行数据 	struct route flight; 	tm t; 	flight.fclient = (struct finisheder*) malloc(sizeof(struct finisheder)); 	flight.uclient = (struct unfinisheder*) malloc(sizeof(struct unfinisheder)); 	struct snode** lol = (struct snode**)malloc(sizeof(struct snode*)); 	struct queue* wow = (struct queue*)malloc(sizeof(struct queue)); 	*lol = (struct snode*)malloc(sizeof(struct snode)); 	wow->front = (struct node*)malloc(sizeof(struct node)); 	wow->rear = (struct node*)malloc(sizeof(struct node)); 	FILE *fp; 	FILE *pf; 	bool Exit = false;//条件控制 	while (!Exit) 	{ 		system("cls"); 		Exit = true; 		printf("*********************\n"); 		printf("**  1.录入班次信息 **\n"); 		printf("**  2.浏览班次信息 **\n"); 		printf("**  3.查询路线     **\n"); 		printf("**  4.退票售票     **\n"); 		printf("**  5.修改航班信息 **\n"); 		printf("*********************\n"); 		printf("Please enter the selection:"); 		scanf("%d", &selection); 		switch (selection) 		{ 		case 1: 			system("cls"); 			fp = fopen("flights.txt", "a");	//a:追加;w:只写（原文件内容清空） 			pf = fopen("member.txt", "a");	//a:追加;w:只写（原文件内容清空） 			fprintf(pf, "航班号:"); 			printf("请输入班次:"); 			scanf("%d", &flight.num); 			while (flight.num != 0) 			{ 				system("cls"); 				printf("飞机号\t\t终点站名\t飞行日期\t成员定额\t余票额\n"); 				scanf("%d%s%s%d%d", &flight.plane, &flight.finaltion, &flight.date, &flight.people, &flight.ticket); 				fprintf(fp, "%d\t\t%d\t\t%-16s%-16s%d\t\t%d\n", flight.plane, flight.num, flight.finaltion, flight.date, flight.people, flight.ticket); 				fprintf(pf, "%d\n", flight.num); 				fprintf(pf, "已定票的客户名单:\n姓名\t\t订票额\n"); 				printf("该航线没人么?y/n\n"); 				getchar(); 				while ((getchar()) != 'y') 				{ 					printf("已定票的客户名单:\n"); 					printf("姓名\t\t订票额\n"); 					scanf("%s%d", flight.fclient->name, &(flight.fclient->ticket)); 					fprintf(pf, "%-16s%d\n", flight.fclient->name, flight.fclient->ticket);	 					printf("座位号:"); 					fprintf(pf, "座位号:"); 					for (int i = 0; i < flight.fclient->ticket; i++) 					{ 						scanf("%d", &flight.fclient->seat[i]); 						fprintf(pf, "%d\t", flight.fclient->seat[i]); 					} 					InsertLastlist(lol, *flight.fclient); 					printf("人员全部录入? y/n\n"); 					getchar(); 				} 				fprintf(pf, "\n预约的客户名单:\n姓名\t\t所需票额\n"); 				printf("该航线没人预约么?y/n\n"); 				getchar(); 				while ((getchar()) != 'y') 				{ 					printf("预约的客户名单:\n"); 					printf("姓名\t\t所需票额\n"); 					scanf("%s%d", &flight.uclient->name, &flight.uclient->need); 					fprintf(pf, "%-16s%d\n", flight.uclient->name, flight.uclient->need); 					Enqueue(wow, *flight.uclient); 					printf("人员全部录入? y/n\n"); 					getchar(); 				} 				system("cls"); 				printf("请输入班次:"); 				scanf("%d", &flight.num); 				fclose(fp); 				system("cls"); 				printf("是否返回上一步?y/n"); 				getchar(); 				if (getchar() == 'y') 					Exit = false; 				break; 			} list.h:          #include <stdio.h> #include<stdlib.h> #include<string.h> #pragma once struct finisheder	//已完成的客户 { 	char name[8];//姓名 	int ticket;//订票额 	int seat[10];//座位号 }; struct snode { 	struct finisheder data;//值域 	struct snode* next;//链接指针域 }; void InsertLastlist(struct snode** lol, struct finisheder x)		//向单链表的表尾插入元素 { 	struct snode* newp; 	newp = (struct snode*)malloc(sizeof(struct snode)); 	if (newp == NULL) 	{ 		printf("内存动态空间用完，退出运行!"); 		exit(1); 	} 	newp->data = x; 	newp->next = NULL; 	struct snode* p = *lol; 	while (p->next != NULL) p = p->next; 	p->next = newp; } queue.h: #pragma once #include <stdio.h> #include <stdlib.h> struct unfinisheder		//预约的客户 { 	char name[8];//姓名 	int need;//所需票额 }; struct node			 { 	struct unfinisheder data;//值域 	struct node* next;//链接指针域 }; struct queue { 	struct node* front;//队首指针 	struct node* rear;//队尾指针 }; void Enqueue(struct queue* q, struct unfinisheder x)		//入队列 { 	struct node* newp; 	newp =(struct node*)malloc(sizeof(struct node)); 	if (newp == NULL) 	{ 		printf("内存动态空间用完，退出运行!"); 		exit(1); 	} 	newp->data = x; 	newp->next = NULL; 	if (q->rear == NULL) 		q->front = q->rear = newp; 	else 	{ 		q->rear->next = newp; 		q->rear = newp; 	} }

user: user-1
content: 仅供参考：#include <time.h> #include <stdlib.h> #include <windows.h> int main() {     int a,b[11];//本来是b[10]，为判断哪句越界，故意声明为b[11]      srand((unsigned int)time(NULL));//按两次F11，等黄色右箭头指向本行时，调试、新建断点、新建数据断点，地址：&b[10]，字节计数：4，确定。     while (1) {//按F5，会停在下面某句，此时a的值为10，b[10]已经被修改为对应0..4之一。         b[(a=rand()%11)]=0;         Sleep(100);         b[(a=rand()%11)]=1;         Sleep(100);         b[(a=rand()%11)]=2;         Sleep(100);         b[(a=rand()%11)]=3;         Sleep(100);         b[(a=rand()%11)]=4;         Sleep(100);     }     return 0; }

user: user-2
content: 贴上代码吧，如果确定没有修改地址内容，那也可能是某个变量越界修改了这里