我目前正在尝试测试在列表中添加参与者的代码,但收到了错误
Access violation reading location 0xFFFFFFFFFFFFFFFF.
当我试图释放记忆时。
participant.h
#pragma once
typedef struct {
char *forename, *surname, *id;
int *score;
}participant;
participant* create_part(char *id, char *forename, char *surname, int *score);
void destroy_part(participant* part);
participant.c
#include "participant.h"
participant* create_part(char *id, char* forename, char* surname, int *score) {
/// Create a new participant
/// : param forename : the participant's forename
/// : param surname : the participant's surname
/// : param score: the participant's scores
/// : return: the participant
participant* part = (participant*)malloc(sizeof(participant));
part->id = malloc(sizeof(char) * 4);
strcpy(part->id, id);
part->forename = malloc(sizeof(char) * (strlen(forename) + 1));
strcpy(part->forename, forename);
part->surname = malloc(sizeof(char) * (strlen(surname) + 1));
strcpy(part->surname, surname);
part->score = malloc(sizeof(int) * 11);
for (int i = 1; i <= 10; i++)
part->score[i] = score[i];
return part;
}
void destroy_part(participant* part) {
/// Destroy a participant - free the memory
/// :param part: the participant
free(part->forename);
free(part->surname);
free(part->id);
free(part->score);
free(part);
}
repository.h
#pragma once
#include "participant.h"
typedef struct {
participant** parts;
int dim, capacity;
}repo;
repo* create_repo();
void destroy_repo(repo* part_lst);
repository.c
#include "repository.h"
#include "participant.h"
repo* create_repo() {
// Create a list of participants
// : return: the repo (list, dimension and capacity)
repo* part_lst = (repo*)malloc(sizeof(repo));
part_lst->dim = 0;
part_lst->capacity = 100;
part_lst->parts = (participant**)malloc(sizeof(participant*) * (part_lst->capacity));
return part_lst;
}
void destroy_repo(repo* part_lst) {
// Destroy the list of participants - free the memory
// :param part_lst: the list of participants
for (int i = 0; i < get_dim(part_lst); i++)
destroy_part(get_participant(part_lst, i));
free(part_lst->parts);
free(part_lst);
}
int r_add_part(repo* part_lst, participant* part) {
// Add a participant to the list
// : param part_lst: the list of participants
// : param part: the participant
// : return: 1 - the participant was added, 0 - it wasn't
if (get_dim(part_lst) < get_capacity(part_lst)) {
(part_lst->parts)[part_lst->dim] = part;
inc_dim(part_lst);
return 1;
}
return 0;
}
void test_r_add_part() {
// Test for function: r_add_part
repo* l = create_repo();
int sc[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
participant *part1 = create_part("111", "Ana", "Popescu", sc);
assert(r_add_part(l, part1) == 1);
for (int i = 1; i < 3; i++)
assert(r_add_part(l, part1) == 1);
/// assert(r_add_part(l, part1) == 0);
destroy_repo(l);
}
void test_r_add_part() {
// Test for function: r_add_part
repo* l = create_repo();
int sc[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
participant *part1 = create_part("111", "Ana", "Popescu", sc);
assert(r_add_part(l, part1) == 1);
for (int i = 1; i < 3; i++)
assert(r_add_part(l, part1) == 1);
/// assert(r_add_part(l, part1) == 0);
destroy_repo(l);
}
我想运行的测试在仓库中。我试着使用调试器,但我仍然无法弄清楚。
1条答案
按热度按时间kwvwclae1#
您在
test_r_add_part
中多次添加了相同的部件。然后销毁存储库中的所有条目。
当你第二次试图销毁同一部分时,那就失败了,因为它已经被销毁了
您必须创建3个这样的部分
注意你正在存储这分数在插槽1到10而不是tha0到9. ie score[0]不被设置