首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >C++中的UnHandled异常错误

C++中的UnHandled异常错误
EN

Stack Overflow用户
提问于 2013-11-06 16:46:07
回答 3查看 172关注 0票数 0

我刚刚完成了构建我的类作业,但当我运行代码时,我得到了一个错误。第一个错误出现在第52行:

代码语言:javascript
复制
size = GetAndSetStd(x);

然后我按下continue,程序一直运行到第88行:

代码语言:javascript
复制
 *x = (student *)malloc( size * sizeof(student) );

我得到的最后一个错误是这个:

Unhandled exception at 0x011f162b in Test01.exe: 0xC0000005: Access violation writing location 0xcccccccc.

正如您所看到的,我已经为它分配了足够的内存,所以我不知道代码中有什么问题。

代码语言:javascript
复制
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>

typedef struct{
    int id;
    char lname[16];
    char fname[11];
    unsigned a[3];
    int flag;
}student;

int GetAndSetStd (student **x);
/*Function name:GetAndSetStd
Description : Function asks the user the number of students receiving data from the students.
Input:  A double pointer to a student struct and  size of the array
Output: size - size of the students in the array
Algorithm: First we get the size of the stuedents array from the user,Second Allocate memory for the array and by a for loop getting the info from the soruce text file..*/
void PrintArray (student *x,int size);
/*Function name:PrintArray
Description : The function prints the students on the screen array
Input:  A pointer to a student struct and  size of the array
Output: none
Algorithm: By a for loop the function prints the array of students.*/
student * MaxAvg(student *x,int size);
/*Function name:MaxAvg
Description : Students function accepts an array returns the address structure of the student with the highest average score.
Input:  A pointer to a student struct and  size of the array
Output: Address the structure of the student with the highest average score.
Algorithm: By a for loop the function calculates the avg score of the student and checks if it's higher than the next score .if it is higher it's save the index of the score and when the FOR loop is finished return the address of the index.*/
float StdAvg(student *x); 
/*Function name:StdAvg
Description : The function gets a pointer to the student and calculate the grades average score.
Input:  A pointer to a student in the struct
Output: returns the average grade score.
Algorithm: The function calculates the sum of the three grades of the student and then divide by the number of grades .the function returns the average score of the student.*/
void ChangeRandStd (student *x,int size);
/*Function name:ChangeRandStd
Description : The function gets the array of students and length of it, taking a random student and replaces the flag field value from 1 to 0.
Input: A pointer to a student struct and  size of the array
Output: none
Algorithm: By DO WHILE loop, using rand function  in the loop. we pick a random student and checks if the flag field is 1,unless keep searching for flag field value=1. after finding it change the value of flag field from 1 to 0.*/
int CopyToAnotherGroup (student *x,int size,student **arr);
void Get_Lost(char* str);
int main()
 { student **x,**y,*p,temp;
   int size,n=1,len;
     size=GetAndSetStd(x);
     while(n)
     {
         printf("Press 1 to see all students\n");
         printf("Press 2 to see two students with biggest average\n");
         printf("Press 3 to change flag for random student\n");
         printf("Press 4 to see all students from the new group\n");
         printf("Press 0 to exit\n");
         printf("enter your choise:");
         scanf("%d",&n);
         switch(n)
         {
         case 1: PrintArray(*x,size);break;
         case 2: PrintArray(p=MaxAvg(*x,size),1);
             temp=*p;
             *p=*x[size-1];
             *x[size-1]=temp;
             PrintArray(MaxAvg(*x,size-1),1);
             break;
         case 3: ChangeRandStd(*x,size);
             break;
         case 4: len=CopyToAnotherGroup(*x,size,y);
             PrintArray(*y,len);
         }
     }
     free(*x);
     free(*y);
     system("pause");
     return 0;
 }
 int GetAndSetStd (student **x)
 { int size,i;
    FILE *f;
    f=fopen("list.txt","r");
     printf("Please enter the number of the students:");
     scanf("%d",&size);
     *x=(student *)malloc(size*sizeof(student));
     if(!x)
         Get_Lost("no memmory");
     for (i=0 ; i<size ; i++)
     {
         fscanf(f,"%d%s%s%u%u%u",(*x)[i].id,(*x)[i].lname,(*x)[i].fname,(*x)[i].a[0],(*x)[i].a[1],(*x)[i].a[2]);
         (*x)[i].flag=1;
     }
     fclose(f);
     return size;
 }
 void PrintArray (student *x,int size)
 {
     int i;
     for (i=0 ; i<size ; i++ )
     {
         printf("%d,%s,%s,%d,%d,%d,%d",x[i].id,x[i].lname,x[i].fname,x[i].flag,x[i].a[0],x[i].a[1],x[i].a[2]);
     }
 }
 float StdAvg(student *x)
 {
     int sum=0;
     float avg=0;
     sum=(*x).a[0]+(*x).a[1]+(*x).a[2];
     avg=sum/3.0;
     return avg;

 }
 student * MaxAvg(student *x,int size)
 {
     float max_avg=StdAvg(x);
     int i,index=0;
     for(i=1 ; i<size ; i++ )
        {
            if(StdAvg(x+i)>max_avg)
            {
                max_avg=StdAvg(x+i);
                index=i;

            }
        }
     return x+i;
 }
 void ChangeRandStd (student *x,int size)
 {
     int temp;
     do
     {
         temp=0+rand()%(size);
     }while(x[temp].flag!=0);
     x[temp].flag=0;

 }
 int CopyToAnotherGroup (student *x,int size,student **arr)
 {
     int i,count=0;
     for ( i=0 ; i< size ; i++ )
     {
         if(x[i].flag==0)
         {
             count++;
             *arr=(student*)realloc(arr,count*sizeof(student));
             if(!*arr)
                 Get_Lost("no memmory");
             (*arr)[count-1]=x[i];
         }
     }
     return count;
 }
 void Get_Lost(char* str)
{
    printf("\n%s",str);
        exit(1);
}
EN

回答 3

Stack Overflow用户

发布于 2013-11-06 16:49:45

您的问题是在main()中声明和使用x。您应该声明一个单间接指针并传递它的地址:

代码语言:javascript
复制
student *x = NULL;

GetAndSetStd(&x);

并相应地更新剩余的代码(这将会有很多更改,但这是编写这么多代码而不查看它是否真正正确的代价)。如上所述,您正在传递一个不确定的指针值,并在被调用的函数中取消对它的引用。在你问之前,是的,y也有类似的问题。

剩下的就留给你了( Aniket已经指出了你对fscanf的错误调用)。

票数 2
EN

Stack Overflow用户

发布于 2013-11-06 16:48:48

scanf一样,fscanf()也希望您传入变量的地址来存储输入数据。你在fscanf()线路上没有它..这可能是您的段错误的来源。

该错误基本上意味着您正在访问未经授权的内存位置。

代码语言:javascript
复制
     fscanf(f,"%d%s%s%u%u%u",(*x)[i].id,(*x)[i].lname,(*x)[i].fname,(*x)[i].a[0],(*x)[i].a[1],(*x)[i].a[2]);

应该是:

代码语言:javascript
复制
     fscanf(f,"%d%s%s%u%u%u",(*x)[i].id,(*x)[i].lname,(*x)[i].fname,&((*x)[i].a[0]),&((*x)[i].a[1]),&((*x)[i].a[2])));
票数 1
EN

Stack Overflow用户

发布于 2013-11-07 03:26:00

谢谢你们的帮助。WhozCraig:我必须发送一个指向某些函数的双指针,而不是我们老师要求的单个pointer.This :|

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19807467

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档