I am trying to write a simple program that stores events in C, initially I allocate memory for one single event with malloc()
(events are stored in an array of structs), each time I add a new event, from edit menu, I write event data in the previously allocated struct, then, using realloc()
, allocate memory for the next event to be added.
Here is my code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
typedef struct events{
char name[50];
//date:
int day;
int month;
int year;
int budget;
}events;
void add_event(struct events *p, int i){
printf("\n> Enter event details");
printf("\n name: ");
scanf(" %[^\n]%*c", p[i].name);
printf(" Date (dd.mm.yyyy): ");
scanf("%d.%d.%d", &p[i].day, &p[i].month, &p[i].year);
printf(" Budget(USD): ");
scanf("%d", &p[i].budget);
}
int edit_menu(struct events *p){
static int qtty=0;
struct events *tmp;
int select;
do{
system("clear");
printf("\n> Edit menu:\n 1) Add event\n 2) Delete event\n 0) Return\n-->");
scanf("%d", &select);
switch(select){
case 1:{
add_event(p, qtty);
tmp=(struct events*)realloc(p, sizeof(struct events)*(qtty+1));
qtty+=1;
if(tmp!=NULL)
p=tmp;
else
printf("\n Fail!");
}break;
}
}while(select);
return qtty;
}
void show_data(struct events *p, int i){
printf("\n> %d) ", i);
puts(p[i].name);
printf("\n Date: %d.%d.%d", p[i].day, p[i].month, p[i].year);
printf("\n Budget: %d USD", p[i].budget);
printf("\n");
}
int main(){
int select, qtty=0;
char search_input[50];
struct events *p=malloc(sizeof(events));
do{
system("clear");
printf("\n Event Menu:\n 1) Show all events[%d]\n 2) Search for event\n 3) Edit events\n 0) Quit\n-->", qtty);
scanf("%d", &select);
switch(select){
case 1:
for(int i=0; i<qtty; i++)
show_data(p, i);
sleep(3);
break;
case 2:{
system("clear");
printf("\n Enter event name: ");
scanf("%[^\n]%*c", search_input);
//srch_event(e, search_input);
}break;
case 3:{
qtty=edit_menu(p);
}
}
}while(select);
free(p);
return 0;
}
My issue is that whenever I use realloc()
, the name of the first "event" somehow gets corrupted, and the parameters of the second "event" are random long numbers.
The third call of realloc()
crashes the program, giving me free(): double free detected in tcache 2
error.
Shortly said: using (struct events*)realloc(p, sizeof(struct events)*(qtty+1));
is not working out to resize my array of structs to a bigger size to store one more "event", and crushes the program.
Changing how I sent my pointer to the function did not help nor what size realloc() reallocates to. The issue seems to lay somewhere in this function:
int edit_menu(struct events *p){
static int qtty=0;
struct events *tmp;
int select;
do{
system("clear");
printf("\n> Edit menu:\n 1) Add event\n 2) Delete event\n 0) Return\n-->");
scanf("%d", &select);
switch(select){
case 1:{
add_event(p, qtty);
tmp=(struct events*)realloc(p, sizeof(struct events)*(qtty+1));
qtty+=1;
if(tmp!=NULL)
p=tmp;
else
printf("\n Fail!");
}break;
}
}while(select);
return qtty;
}
Or in main, where I use malloc()
.