I'm currently trying to test my code for adding a participant in a list, but I receive the error
Access violation reading location 0xFFFFFFFFFFFFFFFF.
when I try to free the memory.
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);
}
The test I wanna run is in repository. I tried using the debugger but I still cannot figure it out.
You have added the same part several times in test_r_add_part
.
You then destroy all the entries in the repo.
The second time you try to destroy the same part, that fails, because its already been destroyed
You must create 3 parts like this
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);
note that you are storing the scores in slots 1 to 10 rather tha 0 to 9. ie score[0] is not set