本文介绍了为什么我会收到“glibc Detected error” ?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

?*这是一个存储学生数据记录的程序。名字姓氏和得分。函数add应该在三个数组中的每个数组之前创建更多内存以添加一组数据。第一个名称和分数是正确添加的,但是姓氏虽然与第一个名称的格式完全相同,但却出现错误glibc detected



?* This is a program to store records of student data. First name last name and score. The function add is supposed to create more memory ahead of each of the three arrays to add one more set of data. The first name as well as score is added correctly but the last name though it has exact same format as first name gives an error "glibc detected"

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
char **first_name, **last_name;
float *score;
int entry;
void print();
void search_lastname(char search_last_name[21]);
void median();
void sortbyscore();
void sortbylastname();
void add();
int main()
{
system("clear");
int i,option;
char search_last_name[21];
do
	{
	printf("\nPlease indicate the number of records you want to enter (The minimum number of entries is 5) :\n");						/*Input of number of records. */
	scanf("%d",&entry);
	}
while(entry<=4);
score=(float*)malloc(entry*sizeof(float));
first_name=(char**)malloc(entry*sizeof(char*));
last_name=(char**)malloc(entry*sizeof(char*));
for(i=0;i<entry;i++)>
	{
	first_name[i]=(char*)malloc(21*sizeof(char));
	last_name[i]=(char*)malloc(21*sizeof(char));
	}
printf("Please input records of students (enter a new line after each record), with following format first name last name score \n");
for(i=0;i<entry;i++)>
	{
	scanf("%s%s%f",first_name[i],last_name[i],&score[i]);																/*Input of records itself*/
	}
do
{
	printf("\nPlease choose the appropriate options :");												/*Switch statement for choosing options.*/
	printf("\nPrint records (press 1)\nAdd a new record (press 2)\nDelete records (press 3)\nSearch by last name (press 4)\nSort by score (press 5)\nSort by last name (press 6)\nFind Median score (press 7)\nExit the Program (press 0)");
	scanf("%d",&option);
	switch(option)
		{
		case 1  :print();
			break;
		case 2  :
			add();
			break;
		case 3  :
			break;
		case 4  :
			printf("\n Please enter the last name you want to search: ");
			scanf("%s",search_last_name);
			search_lastname(search_last_name);
			break;
		case 5  :
			sortbyscore();
			break;
		case 6  :
			sortbylastname();
			break;
		case 7	:
			median();
			break;
		case 0	:
			break;
		default :
			option=2;
			printf("\n Invalid option");
		}
}
while(option>=1 && option<=7);
return 0;
}

void print()
{
int i;
printf("\n The records of students are as follows :");
for(i=0;i<entry;i++)>
	{
	printf("\nFirst name:%s, Last name:%s, Score:%f\n",first_name[i],last_name[i],score[i]);
	}
}


void search_lastname(char search_last_name[21])										/*Funtion to search by last name*/
{
int i;
for(i=0;i<entry;i++)>
	{
	if(strcmp(search_last_name,last_name[i])==0)
		{
		printf("\nFirst name:%s, Last name:%s, Score:%f\n",first_name[i],last_name[i],score[i]);
		}
	}

}

void median()
{
int i,j,pos;
float med;
char templast[entry][21],tempfirst[entry][21],tempfirststr[21],templaststr[21];
float temp[entry],tempvar;
for(i=0;i<entry;i++)>
	{
	temp[i]=score[i];
	strcpy(templast[i],last_name[i]);
	strcpy(tempfirst[i],first_name[i]);
	}
for(i=0;i<entry-1;i++)>
	{
	for(j=0;j<entry-i-1;j++)>
		{
		if(temp[j]>temp[j+1])
			{
			tempvar=temp[j];													/*Sorting using Bubble Sort*/
			strcpy(templaststr,templast[j]);
			strcpy(tempfirststr,tempfirst[j]);
			temp[j]=temp[j+1];
			strcpy(templast[j],templast[j+1]);
			strcpy(tempfirst[j],tempfirst[j+1]);
			temp[j+1]=tempvar;
			strcpy(tempfirst[j+1],tempfirststr);
			strcpy(templast[j+1],templaststr);
			}
		}
	}
if(entry%2==0)
	{
	pos=entry/2;
	med=(temp[pos]+temp[pos-1])/2.0;
	}
else
	{
	pos=(entry/2);
	med=temp[pos];
	}
printf("\n The median of all the students is : %f",med);
printf("\n The students having a score above the median are :");
for(i=0;i<entry;i++)>
	{
	if(temp[i]>=med)
		{
		printf("\nFirst name:%s, Last name:%s, Score:%f\n",tempfirst[i],templast[i],temp[i]);
		}
	}
}



void sortbyscore()																/*Function to sort by score*/
{
int i,j;
char templast[entry][21],tempfirst[entry][21],tempfirststr[21],templaststr[21];
float temp[entry],tempvar;
for(i=0;i<entry;i++)>
	{
	temp[i]=score[i];
	strcpy(templast[i],last_name[i]);
	strcpy(tempfirst[i],first_name[i]);
	}
for(i=0;i<entry-1;i++)>
	{
	for(j=0;j<entry-i-1;j++)>
		{
		if(temp[j]>temp[j+1])
			{
			tempvar=temp[j];													/*Sorting using Bubble Sort*/
			strcpy(templaststr,templast[j]);
			strcpy(tempfirststr,tempfirst[j]);
			temp[j]=temp[j+1];
			strcpy(templast[j],templast[j+1]);
			strcpy(tempfirst[j],tempfirst[j+1]);
			temp[j+1]=tempvar;
			strcpy(tempfirst[j+1],tempfirststr);
			strcpy(templast[j+1],templaststr);
			}
		}
	}
for(i=0;i<entry;i++)>
	{
	printf("\nFirst name:%s, Last name:%s, Score:%f\n",tempfirst[i],templast[i],temp[i]);
	}
}

void sortbylastname()																/*Sort using last name*/
{
char templast[entry][21],tempfirst[entry][21],tempfirststr[21],templaststr[21];
float tempscore[entry],tempscr;
int i,j;
for(i=0;i<entry;i++)>
	{
	strcpy(templast[i],last_name[i]);
	strcpy(tempfirst[i],first_name[i]);
	tempscore[i]=score[i];
	}
for(i=0;i<entry;i++)>
	{
	for(j=i+1;j<entry;j++)>
		{
		if(strcmp(templast[i],templast[j])>0)
			{
			strcpy(templaststr,templast[i]);										/*Swapping all the values*/
			strcpy(tempfirststr,tempfirst[i]);
			tempscr=tempscore[i];
			strcpy(templast[i],templast[j]);
			strcpy(tempfirst[i],tempfirst[j]);
			tempscore[i]=tempscore[j];
			strcpy(templast[j],templaststr);
			strcpy(tempfirst[j],tempfirststr);
			tempscore[j]=tempscr;
			}
		}
	}
for(i=0;i<entry;i++)>
	{
	printf("\nFirst name:%s, Last name:%s, Score:%f\n",tempfirst[i],templast[i],tempscore[i]);
	}
}


void add()
{
entry=entry+1;
char tempfirststr[21],templaststr[21];
float tempscore;
printf("\nPlease input record of the student in the following format first name last name score :");
scanf("%s%s%f",&tempfirststr,&templaststr,&tempscore);


char **b=(char**)realloc(first_name,entry);
first_name[entry-1]=(char*)malloc(21*sizeof(char));
strcpy(first_name[entry-1],tempfirststr);
printf("%s",first_name[entry-1]);


float *m=(float*)realloc(score,entry*sizeof(float));
score[entry-1]=tempscore;
printf("%f", score[entry-1]);


char **c=(char**)realloc(last_name,entry);
last_name[entry-1]=(char*)malloc(21*sizeof(char));
strcpy(last_name[entry-1],templaststr);
printf("%s",last_name[entry-1]);
}





[edit]已添加代码块 - OriginalGriff [/ edit]



[edit]Code block added - OriginalGriff[/edit]

推荐答案

char **b=(char**)realloc(first_name,entry);
// ...
char **c=(char**)realloc(last_name,entry);



with


with

char **b=(char**)realloc(first_name,entry * sizeof(char*));
first_name = b;
// ...
char **c=(char**)realloc(last_name,entry * sizeof(char*));
last_name = c;


这篇关于为什么我会收到“glibc Detected error” ?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-16 02:22