简体   繁体   English

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

[英]How do you properly allocate space in C

I'm writing a program in C and I'm aware you have to allocate appropriate space for efficient code but am unsure of how to start, I've written and completed my program and it would be a shame to restart. 我正在用C编写一个程序,我知道你必须为有效的代码分配适当的空间,但我不确定如何启动,我已经编写并完成了我的程序,重新启动将是一种耻辱。 If anyone can guide me how to properly malloc space for my program it would be appreciated. 如果有人可以指导我如何为我的程序正确malloc空间,将不胜感激。 Here is my code as follows: 这是我的代码如下:

main.c 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 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 courses.h

#ifndef COURSES_H_
#define COURSES_H_

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

#endif

structs.h 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

Essentially the program should run as: create students, create random school courses, fill the students into the courses, and then print everything while accessing the structs from a header file (structs.h) 本质上该程序应该运行:创建学生,创建随机学校课程,填写学生进入课程,然后打印所有内容,同时从头文件访问结构(structs.h)

Here's the message I receive of running the program after it compiles: 这是我编译后运行程序时收到的消息:

Segmentation fault (core dumped)

In createStudents() , you don't need to dereference firstName[i] nor lastName[i] : createStudents() ,您不需要取消引用firstName[i]lastName[i]

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

Since firstName[i] already gives you a char * , you can assign that pointer directly to students[i].firstName (same goes for lastName ): 由于firstName[i]已经为您提供了char * ,因此您可以将该指针直接分配给students[i].firstName (同样适用于lastName ):

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM