0

So i have this program that has a structure and an array. The array is conj_jogos which is an array of a structure called jogo with MAX_SIZE (MAX_SIZE being 5).

Structure:

typedef struct
{
    int id;
    char equipas[2][1024];
    int pont[2];
    char nome[MAX_CHARS];
} jogo;

So to create this array i allocated memory in my main function like this:

int main()
{
    char nome_jg[MAX_CHARS], team1[MAX_CHARS], team2[MAX_CHARS];
    int score1;
    int score2;
    int i;
    conj_jogos = (jogo*)calloc(MAX_SIZE,sizeof(jogo));
    while ((c = getchar()) != x)
            scanf("%1023[^:\n]:%1023[^:\n]:%1023[^:\n]:%d:%d",nome_jg,team1,team2,&score1,&score2);
            remove_esp(nome_jg); /*removes the 1st char if its a space*/
            a(nome_jg,team1,team2,score1,score2);
            ident++;
    }
    free(conj_jogos);
   return 0;
}

The problem is that valgrind is saying that i have a heap overflow on the "a" function and i dont know why so if someone can help i would appreciate it a lot.

Program:

#include<stdlib.h> 
#include<stdio.h>
#include <string.h>
#define MAX_CHARS 1024 /* Max chars for a word*/
#define MAX_SIZE 5 /*Max size for an array*/

jogo *conj_jogos; /*array that saves the jogos*/
static int size_until2 = 0; /*count the size of conj_jogos*/

void a(char nome_jg[],char team1[],char team2[],int score1,int score2)
{
    if (jogo_in(nome_jg) == 1) //confirms if the string nome_jg is in conj_jogos
    {
        printf("%d Jogo existente.\n",line);
        line++;
    }
    else if ((nome_in_sis(team1) == 0) || (nome_in_sis(team2) == 0)) //confirms if the strings team1 or team2 are in sistem_eq 
        {
        printf("%d Equipa inexistente.\n",line);
        line++;
    }
    else
    {
        if (size_until2 < MAX_SIZE)
        {
            conj_jogos[size_until2] = cria_jogo(nome_jg,team1,team2,score1,score2);
            size_until2++;
            line++;
        }
        else
        {
            jogo *temp;
            size_until2++;
            temp = realloc(conj_jogos,sizeof(jogo)*(size_until2+1));
            free(conj_jogos);
            conj_jogos = temp;
            conj_jogos[size_until2] = cria_jogo(nome_jg,team1,team2,score1,score2);
            size_until2++;
            line++;
            free(temp);
        }
    }
}
Martim Correia
  • 483
  • 5
  • 16

1 Answers1

0

I cannot re-run the code because so much relevant functions are not included in your code. But I would try :

conj_jogos = realloc(conj_jogos,sizeof(jogo)*(size_until2+1));

instead of :

temp = realloc(conj_jogos,sizeof(jogo)*(size_until2+1));

And you may also try:

*(conj_jogos + (size_until2 * sizeof(jogo))) = cria_jogo(nome_jg,team1,team2,score1,score2);
            size_until2++;

Instead of:

conj_jogos[size_until2] = cria_jogo(nome_jg,team1,team2,score1,score2);
            size_until2++;
maaeab
  • 326
  • 1
  • 7
  • 2
    Avoid `ptr = realloc(ptr, ...)` logic unless you're prepared to terminate the process on a failed reallocation (i.e. return NULL). The original pointer in a fail-case will be irrecoverable unless you stored it somewhere else. And this: `*(conj_jogos + (size_until2 * sizeof(jogo))) = ...` is *completely wrong*. That sizeof shouldn't be there. pointer arithmetic takes care of proper offsets. – WhozCraig May 16 '20 at 20:33
  • This question is seemingly a follow-up to the answer I gave [here](https://stackoverflow.com/a/61782791/10871073). There has clearly been a degree of misunderstanding about the use of the `temp` pointer. However, I stand by the main substance of my earlier answer. – Adrian Mole May 16 '20 at 20:52