无法找到内存泄漏

时间:2018-01-27 23:07:33

标签: c memory-leaks

我从这段代码中得到了一些内存泄漏,而且我对c很缺乏经验。下面是泄漏内存的代码,下面是valgrind输出。我似乎无法缩小未被释放的确切内存块,请帮忙!很抱歉代码重载,我是新来的。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "students.h"

void init_student(struct student* student, char* name, int id, float gpa) {

  int name_size = strlen(name);
  student->name = malloc(sizeof(char) * (name_size + 1));

  strcpy(student->name, name);
  student->id = id;
  student->gpa = gpa;
}

void free_student(struct student* student) {
  free(student->name);
}

struct student* deep_copy_student(struct student* student) {

  struct student *copy;
  copy = (struct student*) malloc(sizeof(struct student));

  init_student(copy, student->name, student->id, student->gpa);

  return copy;
}

struct student* create_student_array(int num_students, char** names, int* ids,
    float* gpas) {

  struct student* students;
  students = malloc(sizeof(struct student) * num_students);

  for (int i = 0; i < num_students; ++i) {
    init_student(&students[i], names[i], ids[i], gpas[i]);
  }
  return students;
}

void destroy_student_array(struct student* students, int num_students) {
  for (int i = 0; i > num_students; i++) {
    free_student(&students[i]);
  }
  free(students);
}

void print_students(struct student* students, int num_students) {
  for (int i=0; i < num_students; ++i) {
    printf("Name: %s, ID: %d, GPA: %f\n" , students[i].name, students[i].id, students[i].gpa);
  }
}

struct student* find_max_gpa(struct student* students, int num_students) {
  struct student* best_in_school;
  best_in_school = &students[0];
  for (int i = 1; i < num_students; ++i) {
    if(students[i].gpa > best_in_school->gpa){
      best_in_school = &students[i];
    }
  }
  return best_in_school;
}

struct student* find_min_gpa(struct student* students, int num_students) {
  struct student* worst_in_school;
  worst_in_school = &students[0];
  for (int i = 1; i < num_students; ++i) {
    if(students[i].gpa < worst_in_school->gpa){
      worst_in_school = &students[i];
    }
  }
  return worst_in_school;
}

void sort_by_gpa(struct student* students, int num_students) {
  struct student temp;
  for (int i = 0; i < (num_students - 1); ++i) {
    for (int j = 0; j < (num_students - i); ++j) {
      if(students[j].gpa < students[j+1].gpa) {
        temp = students[j];
        students[j] = students[j+1];
        students[j+1] = temp;
      }
    }
  }
}

test.c只是一个测试所有功能的框架,是预制的,所以不是问题。它似乎源自前面代码中制作的8名学生。

==31704== HEAP SUMMARY:
==31704==     in use at exit: 88 bytes in 8 blocks
==31704==   total heap usage: 12 allocs, 4 frees, 262 bytes allocated
==31704== 
==31704== 88 bytes in 8 blocks are definitely lost in loss record 1 of 1
==31704==    at 0x4C29BE3: malloc (vg_replace_malloc.c:299)
==31704==    by 0x40092C: init_student (students.c:50)
==31704==    by 0x400A66: create_student_array (students.c:133)
==31704==    by 0x4007B5: main (test.c:92)
==31704== 
==31704== LEAK SUMMARY:
==31704==    definitely lost: 88 bytes in 8 blocks
==31704==    indirectly lost: 0 bytes in 0 blocks
==31704==      possibly lost: 0 bytes in 0 blocks
==31704==    still reachable: 0 bytes in 0 blocks
==31704==         suppressed: 0 bytes in 0 blocks
==31704== 
==31704== For counts of detected and suppressed errors, rerun with: -v
==31704== ERROR SUMMARY: 2 errors from 2 contexts (suppressed: 0 from 0)

这是主文件。就像我之前说的那样,它只是为我提供的测试台。希望它有所帮助。

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

#include "students.h"


/*
 * This is the total number of students in the testing data set.
 */
#define NUM_TESTING_STUDENTS 8


/*
 * These are the names of the students that'll be used for testing.
 */
char* TESTING_NAMES[] = {
  "Luke Skywalker",
  "Princes Leia",
  "Chewbacca",
  "Han Solo",
  "Lando Calrissian",
  "Darth Vader",
  "C-3PO",
  "R2-D2"
};


/*
 * These are the student IDs for the students in the array above that will be
 * used for testing.
 */
int TESTING_IDS[] = {
  933111111,
  933222222,
  933333333,
  933444444,
  933555555,
  933666666,
  933777777,
  933888888
};


/*
 * These are the GPAs of the students above that will be used for testing.
 */
float TESTING_GPAS[] = {
  3.75,
  4.0,
  3.0,
  2.5,
  3.67,
  1.33,
  3.25,
  3.9
};


int main(int argc, char** argv) {
  struct student student;
  struct student* copy = NULL, * max_gpa, * min_gpa, * students = NULL;
  int i;

  /*
   * Initialize a student using init_student() and print the results.  The
   * power of pointers lets us use print_students() this way!
   */
  init_student(&student, TESTING_NAMES[0], TESTING_IDS[0], TESTING_GPAS[0]);
  printf("\n== Here are the results of init_student():\n");
  print_students(&student, 1);

  /*
   * Make a copy of student using deep_copy_student() and compare the results.
   */
  copy = deep_copy_student(&student);
  printf("\n== Here's that student (left) and its deep copy (right):\n");
  if (copy) {
    printf("name (value):   %s\t%s\n", student.name, copy->name);
    printf("name (pointer): %p\t%p\n", student.name, copy->name);
    printf("id:             %d\t%d\n", student.id, copy->id);
    printf("gpa:            %f\t%f\n", student.gpa, copy->gpa);
  }

  /*
   * Create an array of students using create_student_array() and print the
   * results.
   */
  students = create_student_array(NUM_TESTING_STUDENTS, TESTING_NAMES,
    TESTING_IDS, TESTING_GPAS);
  printf("\n== Here are the results of create_student_array():\n");
  print_students(students, NUM_TESTING_STUDENTS);

  /*
   * Use find_max_gpa() to find the student with the highest GPA and print
   * the result.
   */
  max_gpa = find_max_gpa(students, NUM_TESTING_STUDENTS);
  printf("\n== Here's the student with the highest GPA:\n");
  print_students(max_gpa, 1);

  /*
   * Use find_min_gpa() to find the student with the lowest GPA and print
   * the result.
   */
  min_gpa = find_min_gpa(students, NUM_TESTING_STUDENTS);
  printf("\n== Here's the student with the lowest GPA:\n");
  print_students(min_gpa, 1);

  /*
   * Use sort_by_gpa() to order the students by decreasing GPA and print the
   * results.
   */
  sort_by_gpa(students, NUM_TESTING_STUDENTS);
  printf("\n== Here are the students ordered by decreasing GPA:\n");
  print_students(students, NUM_TESTING_STUDENTS);

  /*
   * Free all of the memory we allocated here.  You should use valgrind to
   * verify that you don't have memory leaks.
   */
  free_student(&student);
  free_student(copy);
  free(copy);
  destroy_student_array(students, NUM_TESTING_STUDENTS);

  return 0;
}

1 个答案:

答案 0 :(得分:6)

for函数中将i > num_students结束表达式从i < num_students更改为destroy_student_array。循环永远不会进入正数学生 - 这可能是泄漏的来源。