Fellas, why is this happening, especially sometimes but not other times?

Here, I run the program three times, getting the exact same correct output. On the fourth, it outputs broken numbers. The function prnStats what’s being executed.

  • Arthur Besse
    link
    fedilink
    21 year ago

    There is a lot going on here, including parts of the program not even visible in your screenshot.

    You should try to make a much smaller minimal reproducible example - in the iterative process of removing things and then re-running it to test if the bug is still present (write a shell script to automate step 2, to run it as many times as are currently necessary observe the bug) you might find the cause yourself. And if you don’t, your MRE will make it easier for others to help you.

    • @lofenyyOP
      link
      English
      2
      edit-2
      1 year ago

      Thanks for the suggestion! I managed to squish the program down into 105 lines. This is my MRE.

      #include<stdlib.h>
      #include<confuse.h>
      
      struct entry
      	{
      	int Day;
      	long int Date;
      	float C;
      	float F;
      	float P;
      	float L;
      	};
      
      int DiaryNum;
      struct entry  *Diary;
      char DiaryPath[] = "/home/lofenyy/.config/Calorimeter/Diary";
      
      cfg_opt_t entry_opts[] = 
      	{
      	CFG_INT("Date", 0, CFGF_NONE),
      	CFG_INT("Day", 0, CFGF_NONE),
      	CFG_FLOAT("C", 0, CFGF_NONE),
      	CFG_FLOAT("F", 0, CFGF_NONE),
      	CFG_FLOAT("P", 0, CFGF_NONE),
      	CFG_FLOAT("L", 0, CFGF_NONE),
      	CFG_END()
      	};
      
      cfg_opt_t entry_base_opts[] =
      	{
      	CFG_SEC("Entry", entry_opts, CFGF_MULTI),
      	CFG_END()
      	};
      
      int lodEntry()
      	{
      	//Initialization
      	cfg_t *cfg;
      	cfg_t *cfg_entry;
      	cfg = cfg_init(entry_base_opts, CFGF_NONE);
      	
      	//Open our file and check for errors
      	if(cfg_parse(cfg, DiaryPath) == CFG_PARSE_ERROR)
      		printf("Err!\n");
      	
      	//Allocate enough memory for our internal diary
      	DiaryNum = cfg_size(cfg, "Entry");
      	Diary = malloc(DiaryNum * sizeof(struct entry));	
      	
      	//For every entry in our external diary
      	for(int C = 0;C < cfg_size(cfg, "Entry");C++)
      		{
      		//Copy our entries to our internal diary
      		cfg_entry = cfg_getnsec(cfg, "Entry", C);
      		Diary[C].Date	= cfg_getint(cfg_entry, "Date");
      		Diary[C].Day	= cfg_getint(cfg_entry, "Day");
      		Diary[C].C	= cfg_getfloat(cfg_entry, "C");
      		Diary[C].F	= cfg_getfloat(cfg_entry, "F");
      		Diary[C].P	= cfg_getfloat(cfg_entry, "P");
      		Diary[C].L	= cfg_getfloat(cfg_entry, "L");
      		}
      	
      	cfg_free(cfg);
      	}
      
      double getEntryCalories(int C)
      	{
      	//4 calories per carb and protein. 9 per lipid. 
      	return 4*(Diary[C].C+Diary[C].P)+9*(Diary[C].L);
      	}
      
      int prnStats()
      	{
      	float C,F,P,L,Calories = 0;
      	int day0 = Diary[0].Day;
      	int day1 = Diary[DiaryNum -1].Day;
      	int days = (day1 - day0) +1;
      	
      	//For every diary entry
      	for(int i = 0;i < DiaryNum;i++)
      		{
      		//Count up our nutrients
      		C = C + Diary[i].C;
      		F = F + Diary[i].F;
      		P = P + Diary[i].P;
      		L = L + Diary[i].L;
      		Calories = Calories + getEntryCalories(i);
      		}
      	
      	//Print them out
      	printf("Average Calories: %2.2f\n", (float)(Calories/days));
      	printf("Average Carbs: %2.2f\n", (float)(C/days));
      	printf("Average Fibre: %2.2f\n", (float)(F/days));
      	printf("Average Proteins: %2.2f\n", (float)(P/days));
      	printf("Average Lipids: %2.2f\n", (float)(L/days));
      	}
      
      int main(int argc, char  *argv[])
      	{
      	// Load our internal food diary from our external food diary
      	// print out our statistics
      	lodEntry();
      	prnStats();
      	}
      

      This is the contents of Diary

      Entry
      {
      	Date = 1679542784
      	Day = 0
      	C = 6.000000
      	F = 5.000000
      	P = 4.000000
      	L = 3.000000
      }
      
      Entry
      {
      	Date = 1679546112
      	Day = 0
      	C = 60.000000
      	F = 50.000000
      	P = 20.000000
      	L = 1.000000
      }
      
      Entry
      {
      	Date = 1679547008
      	Day = 0
      	C = 4.000000
      	F = 4.000000
      	P = 4.000000
      	L = 4.000000
      }
      
      Entry
      {
      	Date = 1679547136
      	Day = 1
      	C = 4.000000
      	F = 4.000000
      	P = 4.000000
      	L = 4.000000
      }
      
      Entry
      {
      	Date = 1679547136
      	Day = 5
      	C = 60.000000
      	F = 50.000000
      	P = 20.000000
      	L = 1.000000
      }