Malloc,当输入太高时,它会失败但在较低时运行顺畅(例如3)为什么?

时间:2017-11-28 16:56:06

标签: c malloc

我的程序是允许用户输入no。学生和他们喜欢的课程,然后通过与大多数学生的会议对所有课程进行排序。

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

struct student {  //*** PART 2 STRUCTURE DEFINITION **********
    char studName[50];  // Name of student
    long long int studID;        // Student ID of student
    int session;      // Prefered session of each student
};

struct session {
    int sessionID;
    int NoofStudinSession;
};

FILE * fPointer;


void main(void)
{
    //  Definitions
    struct student *myStudents;
    struct session *sessionsPtr;
    int Noofsessions = 0;
    int NoofStud = 0;
    int i;

    //  User input for number of students and consultation sessions
    printf("Enter Number of Students:\n");
    scanf("%d", &NoofStud);
    printf("Enter number of consultation session:\n");
    scanf("%d", &Noofsessions);

    //  Dynamic allocation for struct
    myStudents = (struct student *) malloc(sizeof(struct student) * NoofStud);

    if (myStudents == NULL) {
        printf("Error, out of memory.\n");
        return;
    }

    sessionsPtr = (int *)malloc(sizeof(struct session) * Noofsessions);

    if (sessionsPtr == NULL) {
        printf("Error, out of memory.\n");
        return;
    }

    // Initializing struct session before using
    for (i = 0; i < Noofsessions; i++) {
        (sessionsPtr + i)->sessionID = i + 1;
        (sessionsPtr + i)->NoofStudinSession = 0;

    }

    //  User input display
    for (i = 0; i < NoofStud; i++) {
        printf("Enter Student %d ID: \n", i + 1); // Prompt user to enter the ID of students
        scanf("%d", &(myStudents + i)->studID); //**


        printf("Enter Student %d Name: \n", i + 1); // Prompt user to enter the Student name
        scanf(" %[^\t\n]s", &(myStudents + i)->studName); //**

        printf("Enter Student %d's prefered Session(Number of session(s) is %d):\n", i + 1, Noofsessions); // Prefered Session
        scanf("%d", &(myStudents + i)->session); //**

        (sessionsPtr + ((myStudents + i)->session - 1))->NoofStudinSession++;
    }


    //  Display summary of Part2 on screen
    printf("Student ID\t Student Name\t\t Chose Which Session\n");
    fprintf(fPointer, "Student ID\t Student Name\t\t Chose Which Session\n"); // Write into Text file
    for (i = 0; i < NoofStud; ++i) {

        printf("%d\t  %15s\t\t\t %d\n", (myStudents + i)->studID, (myStudents + i)->studName, (myStudents + i)->session);
        fprintf(fPointer, "%d\t  %15s\t\t\t %d\n", (myStudents + i)->studID, (myStudents + i)->studName, (myStudents + i)->session);
    }

    // Sorting of sessions from MOST students to LEAST students
    int total = 0, swapped, pos;

    //comparing and sort values
    while (1) {
        pos = 0;

        for (i = 0; i < Noofsessions - 1; ++i) {

            if ((sessionsPtr + i)->NoofStudinSession < (sessionsPtr + i + 1)->NoofStudinSession) {

                swapped = sessionsPtr;
                sessionsPtr = sessionsPtr + 1;
                swapped = sessionsPtr + 1;
                pos = 1;

            }
        }
        if (pos == 0) {
            break;
        }
    }

    //  Display the Sorted Table
    printf("\nSession\t\t\t No. of Students\n");
    fprintf(fPointer, "\nSession\t\t\t No. of Students\n"); // Write into Text file

    for (i = 0; i < Noofsessions; i++) {

        printf("%d\t\t\t    %d\n", (sessionsPtr + i)->sessionID, (sessionsPtr + i)->NoofStudinSession);
        fprintf(fPointer, "%d\t\t\t    %d\n", (sessionsPtr + i)->sessionID, (sessionsPtr + i)->NoofStudinSession); // Write into Text file
    }

    free(myStudents);
    free(sessionsPtr);

    system("pause");
    return 0;

}

Noofsession小于3时,它会顺利运行。 但是,当它变高时,会发生此错误(HEAP错误指定给RtlValidateHeap的地址无效)。

编辑: 为了更好地解释问题,这里是崩溃后立即显示。 Output 如果我错了,请纠正我,因此排序表中的输出实际上是地址?而不是排序的值。

1 个答案:

答案 0 :(得分:-1)

尽量避免将内部调用倍增到malloc。分配结构数组时,请使用calloc

那就是说,听取迈克尔沃尔兹的评论。摆脱所有警告。