我目前正在尝试测试我的代码以在列表中添加参与者,但我收到错误消息
Access violation reading location 0xFFFFFFFFFFFFFFFF.
当我尝试释放内存时。
参与者.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);
参与者.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);
}
存储库.h
#pragma once
#include "participant.h"
typedef struct {
participant** parts;
int dim, capacity;
}repo;
repo* create_repo();
void destroy_repo(repo* part_lst);
存储库.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);
}
我想运行的测试在存储库中。我尝试使用调试器,但我仍然无法弄清楚。
您在
test_r_add_part
中多次添加了相同的部分。
然后销毁存储库中的所有条目。
你第二次尝试销毁同一个部分,失败了,因为它已经被销毁了
您必须创建 3 个部分 像这样
participant *part1 = create_part("111", "Ana", "Popescu", sc);
participant* part2 = create_part("222", "Joe", "Popescu", sc);
participant* part3 = create_part("333", "Dave", "Popescu", sc);
assert(r_add_part(l, part1) == 1);
assert(r_add_part(l, part2) == 1);
assert(r_add_part(l, part3) == 1);
请注意,您将分数存储在插槽 1 到 10 而不是 0 到 9 中。即未设置分数 [0]