c - 添加的值不会正确保存到 malloc 数组。我忽略了内存泄漏吗?

标签 c memory-leaks struct malloc realloc

我正在尝试将值保存到动态分配的结构数组中。该程序在调用 malloc 的 for 循环内运行良好并正确打印值,但是一旦 for 循环结束并且我尝试再次打印它会出现段错误。

这是带有结构定义的 header

 typedef struct{
  char student_name[100];
  int stdnt_id;
  int courses[4];
  int grades[4][10];
  int number_courses;
  }students;

typedef struct{
  char course_name[100];
  int course_id;
  int course_students[100];
  int number_students;
  }courses;

typedef struct{             
  int number_courses;                           
  int number_students;
  }useful_numbers;

这是代码。

#include <stdio.h>
#include <stdlib.h>
#include "structures.h"

useful_numbers numbers;
students **student;
courses **course;

void add_course(void)
{
putchar('\n');
int x, y, z, new, tmp;
printf("How many courses are you adding? ");
scanf("%d", &new);
getchar();
tmp=numbers.number_courses;
if(numbers.number_courses>0)
    {
    courses **course=(courses **)realloc(course, sizeof(courses *)*(new+numbers.number_courses));
    if(NULL==course)
        {
        printf("There was a problem in malloc\n");
        exit(1);
        }
    for(x=tmp; x<new+tmp; x++)
        {
        course[x]=(courses *)malloc(sizeof(courses));
        if(NULL==course[x])
            {
            printf("There was a problem in malloc\n");
            exit(2);
            }
        printf("Print the name of course number %d: ", x+1);
        gets(course[x]->course_name);
        printf("Print the id number for %s: ", course[x]->course_name);             
        scanf("%d", &course[x]->course_id);                                     
        getchar();
        numbers.number_courses++;                                                                       
        }
    }
if(numbers.number_courses==0)
    {
    courses **course=(courses **)malloc(new*sizeof(courses *));
    if(NULL==course)
        {
        printf("There was a problem in malloc\n");
        exit(3);
        }
    for(x=0; x<new; x++)
        {
        course[x]=(courses *)malloc(sizeof(courses));
        if(NULL==course[x])
            {
            printf("There was a problem in malloc\n");
            exit(4);
            }
        printf("Print the name of course number %d: ", x+1);
        scanf("%s", &course[x]->course_name);
        getchar();
        printf("Print the id for %s: ", course[x]->course_name);
        scanf("%d", &course[x]->course_id);
        getchar();
        printf("%s\t%d\t%d\t%d\n", course[x]->course_name, course[x]->course_id, numbers.number_courses, x);
        numbers.number_courses++;
        }
    }
for(y=0; y<numbers.number_courses; y++)
    {
    printf("%d\t%d\n", course[y]->course_id, numbers.number_courses);
    }
return;

最佳答案

if 语句中,您声明了一个新的局部变量 courses,它隐藏了同名的全局变量:

courses **course=(courses **)malloc(....);

最后的打印使用了全局变量,但它仍然是NULL。您反而想设置现有的全局变量:

course=(courses **)malloc(....);

关于c - 添加的值不会正确保存到 malloc 数组。我忽略了内存泄漏吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26017642/

相关文章:

调用 zmq_socket 返回 NULL 和 errno 14(错误地址)

c - 如何将数组项传递给函数?

c++ - 内存泄漏和多态性

c - fgets 无法正常工作

go - 在函数中返回结构的值并在另一个包中使用它

c - C 中函数参数的范围

编译后的 C 数据类型大小

iphone - 在我的 iPhone 应用程序项目中出现内存泄漏我应该如何解决它?

c++ - 通过类析构函数中重置成员shared_ptrs解决C++11shared_ptr循环引用?

objective-c - 从 NSMutableArray 中的 C-Struct 获取值