你如何在C中正确分配空间

时间:2017-11-30 22:28:23

标签: c memory-management segmentation-fault

我用C编写程序,我知道你必须为有效的代码分配适当的空间,但我不确定如何开始,我已经编写并完成了我的程序,它会重新开始是一种耻辱。如果有人可以指导我如何为我的程序正确malloc空间,将不胜感激。 这是我的代码如下:

的main.c

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include "courses.h"

int main (int argc, char *argv[]) {
    srand (time(NULL));
    createStudents ();
    createCourses ();
    regiserStudents ();
    printCourses ();

    return 1;
}

courses.c

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include "structs.h"

void createStudents () {
  int random, i;

  for (i = 0; i < 12; i++) {
    students[i].firstName = *firstName[i];
    students[i].lastName = *lastName[i];
    random = 10000 + rand() % 89999;
    students[i].num.studentNum = random;
    printf("%d - %s, %s \n", students[i].num.studentNum, students[i].lastName, students[i].firstName);
  }
}

void createCourses () {
  int numbers[999];
  int numbersLeft = 999;
  char courseCode[512];
  int numCourses = 3;
  int random, i, j;

  for (i = 0; i < 999; i++) {
    numbers[i] = i;
  }

  for (j = 0; j < numCourses; j++) {
    random = rand() % numbersLeft;
    if (random < 10) {
      snprintf(courseCode, sizeof courseCode, "CS00%d", random);
    }
    else if (random < 100 && random > 9) {
      snprintf(courseCode, sizeof courseCode, "CS0%d", random);
    }
    else {
      snprintf(courseCode, sizeof courseCode, "CS%d", random);
    }

    courses[j].cName = courseName[j];
    courses[j].cDescription = courseDescription[j];
    courses[j].cCode = courseCode;
    numbers[random] = numbers[numbersLeft-1];
    numbersLeft--;

    random = 4 + rand() % 4;
    courses[j].maxRegister = random;
  }
}

void regiserStudents () {
  int checkSum = 0, checkSum1 = 0, checkTemp = 0, count0 = 0, count1 = 0, count2 = 0;
  int v, i, j, random;

  for (i = 0; i < 2; i++) {
    checkTemp = count0;

    for (j = 0; j < 12; j++) {
      random = rand() % 3;
      if (random == 0) {
        if (count0 == 0) {
          courses[random].registered[count0] = &students[j];
          count0++;
        }
        else {
          checkSum1 = students[j].num.studentNum;

          for (v = 0; v < checkTemp; v++) {
            checkSum = courses[0].registered[v]->num.studentNum;
            if (checkSum == checkSum1) {
              /*Do Nothing*/
            }
            else {
              courses[random].registered[count0] = &students[j];
              count0++;
            }
          }
        }
      }
      if (random == 1) {
        if (count1 == 0) {
          courses[random].registered[count1] = &students[j];
          count1++;
        }
        else {
          checkSum1 = students[j].num.studentNum;

          for (v = 0; v < checkTemp; v++) {
            checkSum = courses[1].registered[v]->num.studentNum;
            if (checkSum == checkSum1) {
              /*Do Nothing*/
            }
            else {
              courses[random].registered[count1] = &students[j];
              count1++;
            }
          }
        }
      }
      if (random == 2) {
        if (count2 == 0) {
          courses[random].registered[count2] = &students[j];
          count2++;
        }
        else {
          checkSum1 = students[j].num.studentNum;

          for (v = 0; v < checkTemp; v++) {
            checkSum = courses[2].registered[v]->num.studentNum;
            if (checkSum == checkSum1) {
              /*Do Nothing*/
            }
            else {
              courses[random].registered[count2] = &students[j];
              count2++;
            }
          }
        }
      }
    }
  }
  courses[0].studentRegistered = count0;
  courses[1].studentRegistered = count1;
  courses[2].studentRegistered = count2;
}

void printCourses () {
  int i;
  printf("\n%s - %s\n%s\nRegistered Students (%d/%d):\n", courses[0].cCode, courses[0].cName, courses[0].cDescription, courses[0].studentRegistered, courses[0].maxRegister);

  for (i = 0; i < courses[0].studentRegistered; i++) {
    printf("%d - %s, %s \n", courses[0].registered[i]->num.studentNum, courses[0].registered[i]->lastName, courses[0].registered[i]->firstName);
  }
  printf("\n%s - %s\n%s\nRegistered Students (%d/%d):\n", courses[1].cCode, courses[1].cName, courses[1].cDescription, courses[1].studentRegistered, courses[1].maxRegister);

  for (i = 0; i < courses[1].studentRegistered; i++) {
    printf("%d - %s, %s \n", courses[1].registered[i]->num.studentNum, courses[1].registered[i]->lastName, courses[1].registered[i]->firstName);
  }
  printf("\n%s - %s\n%s\nRegistered Students (%d/%d):\n", courses[2].cCode, courses[2].cName, courses[2].cDescription, courses[2].studentRegistered, courses[2].maxRegister);

  for (i = 0; i < courses[2].studentRegistered; i++) {
    printf("%d - %s, %s \n", courses[2].registered[i]->num.studentNum, courses[2].registered[i]->lastName, courses[2].registered[i]->firstName);
  }
}

courses.h

#ifndef COURSES_H_
#define COURSES_H_

void createStudents();
void createCourses ();
void regiserStudents ();
void printCourses ();

#endif

structs.h

#ifndef STRUCTS_H_
#define STRUCTS_H_

char *firstName[] = {
  "Emma", "Liam", "Olivia",
  "Noah", "Ava", "Logan",
  "Sophia", "Lucas", "Isabella",
  "Mason", "Shaylyn", "Jack"
};

char *lastName[] = {
  "Smith", "Johnson", "Williams",
  "Brown", "Jones", "Miller",
  "Davis", "Garcia", "Rodriguez",
  "Wilson", "Seguin", "Loveday"
};

typedef struct{
  int studentNum;
}studentNumber;

typedef struct{
  char *firstName;
  char *lastName;
  studentNumber num;
}studentID;

studentID students[12];

char *courseName[] = {"Web Programming", "Technical Communication", "Processor Architecture"};
char *courseDescription[] = {"Learn to make websites!", "Learn the essentials of communication skills", "Learn the basics of circuits and Machine Language coding"};

typedef struct {
  int maxRegister;
  char *cCode;
  char *cName;
  char *cDescription;
  studentID *registered[8];
  studentID *waitlisted[12];
  int studentRegistered;
  int studentWaitlisted;
}course;

course courses[3];


#endif

基本上,程序应该运行为:创建学生,创建随机学校课程,将学生填入课程,然后在从头文件(structs.h)访问结构时打印所有内容

这是我收到的编译后运行程序的消息:

Segmentation fault (core dumped)

1 个答案:

答案 0 :(得分:3)

createStudents()中,您不需要取消引用firstName[i]lastName[i]

students[i].firstName = *firstName[i];
students[i].lastName = *lastName[i];

由于firstName[i]已经为您提供char *,您可以将该指针直接指定给students[i].firstName(同样适用于lastName):

students[i].firstName = firstName[i];
students[i].lastName = lastName[i];