C - double free or corruption - c

Code Purpose
The code is supposed to simulate CPU scheduling algorithms. At present only FCFS (First Come First Served) and SJF (Shortest Job First have been written)
Problem
When running the code I receive the following error when using the FCFS 'pathway'
*** Error in `./test2': double free or corruption (!prev): 0x000055f54ecc7830 ***
From what I have found online this relates to memory overflow issues, although running this on my personal PC I receive no errors for the pathway. I have checked my loops and array decelerations yet cannot find the issue.
I believe my issue may relate to the following for loops
//compare values in at to find earliest arrival time. Basically loops through dataset values and sorts them into the arrival order
for(i=0; i<processes; i++)
{
for(j=0; j<processes; j++)
{
if(at[i]<at[j]) //if the value of i is smaller than j (basically gets the smallest value in array)
{
temp=at[i]; //temp int equals arrival time of i
at[i]=at[j]; //arrival time i changes to value of arrival time j
at[j]=temp; //arrival time j becomes original value of arrival time i (basically switching the values of i and j)
temp=bt[i]; //temp becomes value of burst time i
bt[i]=bt[j]; //burst time i becomes values of burst time j
bt[j]=temp; //burst time j becomes the original value of burst time i (basically switching the values of i and j)
temp=pid[i]; //t changes to value of pid i
pid[i]=pid[j]; //pid i becomes value of pid j
pid[j]=temp; //pid j becomes value of t (basically switching the values of i and j)
}
}
}
The main dataset I have been testing is dataset 3 which contains the following data
PID AT BT
0 3 4
1 1 5
2 2 20
3 0 25
4 6 14
5 8 6
All files relating to this code can be located at the following link (Apologies I know links are not preferred but its the easiest way to share the datasets with correct formatting)
Full code and file location
Full Code
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char filename[100];
char *buffer = NULL;
int schedtoken;
char entries[10];
int fcfs()
{
int pid[10],at[10],bt[10],st[10],ft[10],tat[10],wt[10],i=0,j=0,processes=6,temp,n1,n2,n3;
int totwt=0,tottat=0;
char c1,c2,c3,fcfsselection;
printf("\n\n\nPlease select the dataset you would like to use\n\n");
printf(" 1. Dataset1\n 2. Dataset2\n 3. Dataset3\n 4. Quit\n\nSelection:\n");
scanf(" %c",&fcfsselection);
// Get data set user wants and amend filename based on selection
switch(fcfsselection)
{
case '1':
printf("\nYou have selected Dataset1\n");
strcpy(filename, "datasets/dataset1.txt");
break;
case '2':
printf("\nYou have selected Dataset2\n");
strcpy(filename, "datasets/dataset2.txt");
break;
case '3':
printf("\nYou have selected Dataset3\n");
strcpy(filename, "datasets/dataset3.txt");
break;
case '4':
printf("\nThank you for using this tool!");
break;
default:
printf("\nERROR!: Incorrect selection - Returning to Menu\n");
fcfs();
}
//Import dataset file, store the first line of char's (crashes if only checking for ints) and the rest of the ints
FILE *fp;
fp=fopen(filename,"r");
if (fp == NULL)
{
printf("Cannot open file at %s, try again.", filename);
fcfs();
}
else
{
fscanf(fp,"%s%s%s",&c1,&c2,&c3);
while(fscanf(fp,"%d%d%d",&n1,&n2,&n3)!=EOF)
{
pid[i]=n1;
at[i]=n2;
bt[i]=n3;
i++;
}
}
fclose(fp);
//compare values in arr time to find earliest arrival time. Basically loops through dataset values and sorts them into the arrival order
for(i=0; i<processes; i++)
{
for(j=0; j<processes; j++)
{
if(at[i]<at[j]) //if the value of i is smaller than j (basically gets the smallest value in array)
{
temp=at[i]; //temp int equals arrival time of i
at[i]=at[j]; //arrival time i changes to value of arrival time j
at[j]=temp; //arrival time j becomes original value of arrival time i (basically switching the values of i and j)
temp=bt[i]; //temp becomes value of burst time i
bt[i]=bt[j]; //burst time i becomes values of burst time j
bt[j]=temp; //burst time j becomes the original value of burst time i (basically switching the values of i and j)
temp=pid[i]; //t changes to value of pid i
pid[i]=pid[j]; //pid i becomes value of pid j
pid[j]=temp; //pid j becomes value of t (basically switching the values of i and j)
}
}
}
//complete calculations
for(i=0; i<processes; i++)
{
if(i==0)
st[i]=at[i]; //if i equals 0 (basically the beggining of the sim) then the start time equals the arrival time of the first entry (so 0)
else
st[i]=ft[i-1]; //otherwise the start value equals the finish value of the last entry run -1
wt[i]=st[i]-at[i]; //wait time equals the start time of the process minus the arrival time
ft[i]=st[i]+bt[i]; //finish time equals start time plus run time
tat[i]=ft[i]-at[i]; // turn around time equals finish time minus arrival time
}
tottat=0;
//print results
printf("\nPID\t AT\t BT\t WT\t ST\t TAT\t CT");
for(i=0; i<processes; i++)
{
printf("\n%3d\t%3d\t%3d\t%3d\t%3d\t%3d\t%3d",pid[i],at[i],bt[i],wt[i],st[i],tat[i],ft[i]);
totwt+=wt[i];
tottat+=tat[i];
}
printf("\n\nAverage Waiting Time:%f",(float)totwt/processes);
printf("\nAverage Turn Around Time:%f",(float)tottat/processes);
fclose(fp);
//Open new file to print output
FILE *f = fopen("datasets/output.txt", "w");
if (f == NULL)
{
printf("Error opening file!\n");
exit(1);
}
//Print output to file
fprintf(f, "PID\t AT\t BT\t WT\t ST\t TAT\t CT");
for(i=0; i<processes; i++)
{
fprintf(f,"\n%3d\t%3d\t%3d\t%3d\t%3d\t%3d\t%3d",pid[i],at[i],bt[i],wt[i],st[i],tat[i],ft[i]);
}
fprintf(f,"\n\nAverage Waiting Time:%f",(float)totwt/processes);
fprintf(f,"\nAverage Turn Around Time:%f",(float)tottat/processes);
printf("\n\nThe results for the dataset simulated are stored in: datasets/output.txt");
fclose(f);
return 0;
}
char* getfile(char *filename)
{
int string_size, read_size;
FILE *file = fopen(filename, "r");
if (file)
{
// Seek the last byte of the file
fseek(file, 0, SEEK_END);
// Offset from the first to the last byte, or in other words, filesize
string_size = ftell(file);
// go back to the start of the file
rewind(file);
// Allocate a string that can hold it all
buffer = (char*) malloc(sizeof(char) * (string_size + 1) );
// Read it all in one operation
read_size = fread(buffer, sizeof(char), string_size, file);
// fread doesn't set it so put a \0 in the last position
// and buffer is now officially a string
buffer[string_size] = '\0';
if (string_size != read_size)
{
// Something went wrong, throw away the memory and set
// the buffer to NULL
free(buffer);
buffer = NULL;
}
// Always remember to close the file.
fclose(file);
}
// printf("%s",buffer);
return buffer;
}
void schedintro(int schedtoken)
{
//take the value of schedtoken, change the value of filename to relavent file path and use getfile to open and then print the file. Call relavent scheduling function to do calculations
if(schedtoken==1)
{
strcpy(filename, "headers/fcfsheader.txt");
getfile(filename);
printf("%s",buffer);
fcfs();
}
else if(schedtoken==2)
{
strcpy(filename, "headers/sjfheader.txt");
getfile(filename);
printf("%s",buffer);
}
else if(schedtoken==3)
{
strcpy(filename, "headers/rrheader.txt");
getfile(filename);
printf("%s",buffer);
}
}
int schedselect()
{
//function to display what algorithms can be selected. User input
// obtained based on these options and user filtered based on switch
// cases to relevant function path
char selection;
// print users options and take their input for switch
printf("%s",buffer);
printf("\n\n\nPlease select the Scheduling Algorithm you would like to use\n\n");
printf(" 1. First Come First Served (FCFS)\n 2. Shortest Job First (SJF)\n 3. Round Robin (RR)\n 4. Quit\n\nSelection:\n");
scanf("%c",&selection);
// direct user to specific algorithm function path
switch(selection)
{
case '1':
//printf("\nYou have selected First Come First Served (FCFS)\n");
schedintro(schedtoken=1);
break;
case '2':
//printf("\nYou have selected Shortest Job First (SJF)\n");
schedintro(schedtoken=2);
break;
case '3':
//printf("\nYou have selected Round Robin (RR)\n");
schedintro(schedtoken=3);
break;
case '4':
printf("\nThank you for using this tool!");
break;
default:
printf("\nERROR!: Incorrect selection - Returning to Menu\n");
schedselect();
}
//printf("%d", schedoption);
return schedtoken;
}
int main()
{
strcpy(filename, "headers/introheader.txt");
getfile(filename);
schedselect();
return 0;
}

In fcfs() you close two times the same file
fclose(fp);
//compare values in arr time to find earliest arrival time. Basically loops through dataset values and sorts them into the arrival order
and
fclose(fp);
//Open new file to print output
remove the second fclose
without the second fclose nothing is signaled by valgrind for -Dataset3_ :
valgrind ./a.out
==22836== Memcheck, a memory error detector
==22836== Copyright (C) 2002-2012, and GNU GPL'd, by Julian Seward et al.
==22836== Using Valgrind-3.8.1 and LibVEX; rerun with -h for copyright info
==22836== Command: ./a.out
==22836==
(null)
Please select the Scheduling Algorithm you would like to use
1. First Come First Served (FCFS)
2. Shortest Job First (SJF)
3. Round Robin (RR)
4. Quit
Selection:
1
(null)
Please select the dataset you would like to use
1. Dataset1
2. Dataset2
3. Dataset3
4. Quit
Selection:
3
You have selected Dataset3
PID AT BT WT ST TAT CT
3 0 25 0 0 25 25
1 1 5 24 25 29 30
2 2 20 28 30 48 50
0 3 4 47 50 51 54
4 6 14 48 54 62 68
5 8 6 60 68 66 74
Average Waiting Time:34.500000
Average Turn Around Time:46.833332
The results for the dataset simulated are stored in: datasets/output.txt==22836==
==22836== HEAP SUMMARY:
==22836== in use at exit: 0 bytes in 0 blocks
==22836== total heap usage: 4 allocs, 4 frees, 2,272 bytes allocated
==22836==
==22836== All heap blocks were freed -- no leaks are possible
==22836==
==22836== For counts of detected and suppressed errors, rerun with: -v
==22836== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 6 from 6)

Besides the double free issue pointed out by bruno, your code has an out-of-bounds write bug.
char c1,c2,c3,fcfsselection;
...
fscanf(fp,"%s%s%s",&c1,&c2,&c3); // out-of-bounds write.
Here is a live test of your code, which reports this bug.

Related

Conditional statement in loop checks against variable set when fscanf returns a value different than 1 causes an infinite loop

I'm expecting the loop to end when the end of the file is reached. I know that when it reaches it the value returned from fscanf will be different than 1, as it returns 1 whenever it reads something.
If I set i=3 the loop is infinite, but if I set i=2 the loop ends, which I find very weird, as the controlling expression (i!=3) is supposed to be evaluated before the adjustment one, so when I set i=3, it breaks and should test than indeed i!=3 so the loop would end (which it doesn't). When I set it to 2, it ends, so it must be incrementing it one more time and then checking.
So, my first question is why is this happening?
My second question is that with the %[^\n]s it only saves the start of the file, but with only %s it saves the whole file, but wrongly as it only scans until the space, but I want it to scanf until the new line.
My file has 1 element per line (some with spaces)
for(int i=0;i!=3;i++){
switch(i){
case 0:
if(fscanf(recordsRegistry,"%[^\n]s", (recordsArray[recordsArrayPosition]).author)!=1){
i=3;//stop condition
}
break;
case 1:
if(fscanf(recordsRegistry,"%[^\n]s", (recordsArray[recordsArrayPosition]).title)!=1){
i=3;//stop condition
}
break;
case 2:
if(fscanf(recordsRegistry,"%hu", &((recordsArray[recordsArrayPosition]).numberOfSales))!=1){
i=3;//stop condition
}
i=-1;
recordsArrayPosition++;
totalRecords++;
recordsArray=realloc(recordsArray, totalRecords*recordStructSize) ;
if(recordsArray==NULL){
fprintf(stderr, "Could not reallocate memory at line %d.\n", __LINE__);
return 3;
}
break;
}
}
Example of the file being read:
LEANN RIMES
WHAT A WONDERFUL WORLD
4628
BLUE CHEER
WHAT DOESNT KILL YOU
9664
WITHIN TEMPTATION & KEITH CAPUTO
WHAT HAVE YOU DONE
3226
WITHIN TEMPATION
WHAT HAVE YOU DONE
8093
KOKO TAYLOR
WHAT IT TAKES (THE CHESS YEARS)
7160
DOOBIE BROTHERS
WHAT WERE ONCE VICES ARE NOW HABITS
2972
LIL'ED & THE BLUES IMPERIALS
WHAT YOU SEE IS WHAT YOU GET
9443
VARIOUS ARTISTS
WHAT'S SHAKIN
4473
The struct:
typedef struct{
char author[20], title[50];
short unsigned int numberOfSales;
} RECORD;
New for looop:
for(int i=0;i!=3;i++){
switch(i){
case 0:
if(fgets(recordsArray[recordsArrayPosition].author, totalRecords, recordsRegistry)==NULL){
//printf("aa\n");
i=2;//stop condition
}
break;
case 1:
if(fgets(recordsArray[recordsArrayPosition].title, totalRecords, recordsRegistry)==NULL){
//printf("aaa\n");
i=2;//stop condition
}
break;
case 2:
if(fscanf(recordsRegistry,"%hu", &((recordsArray[recordsArrayPosition]).numberOfSales))!=1){
//printf("aaaa\n");
i=2;//stop condition
}
i=-1;
recordsArrayPosition++;
totalRecords++;
recordsArray=realloc(recordsArray, totalRecords*recordStructSize) ;
if(recordsArray==NULL){
fprintf(stderr, "Could not reallocate memory at line %d.\n", __LINE__);
return 3;
}
break;
}
}
... when fscanf returns a value different than 1 causes an infinite loop
when you set i to 3 that value will not be tested in i!=3 because before the test the i++ will be done
set i to 2
with only %s ... it only scans until the space
I want it to scanf until the new line.
if you want to read line per line use fgets rather than fscanf, do not forget to remove the probable newline
in the scanf family 's' matches a sequence of non-white-space characters, spaces are separator
man scanf says :
s Matches a sequence of non-white-space characters; the next
pointer must be a pointer to the initial element of a character
array that is long enough to hold the input sequence and the
terminating null byte ('\0'), which is added automatically. The
input string stops at white space or at the maximum field width,
whichever occurs first.
warning you mix to read line and value, when you read the value the newline is not read, replace "%hu" by "%hu\n" or much secure read the line then extract the number from it (I do that in my proposal)
from your remark
why will i++ test before i!=3?
your :
for(int i=0;i!=3;i++){
<body without continue>
}
is equivalent to
{ int i = 0;
while (i != 3) {
<body without continue>
i++;
}
}
Here a proposal :
#include <stdio.h>
#include <ctype.h>
#include <string.h>
typedef struct{
char author[20], title[50];
short unsigned int numberOfSales;
} RECORD;
#define MAXRECORDS 100
void removeEndSpaces(char * s)
{
char * p = s + strlen(s);
while ((s != p) && isspace((unsigned char) *--p))
*p = 0;
}
int main()
{
FILE * fp = fopen("f", "r");
RECORD records[MAXRECORDS];
int nrecords;
char line[32];
if (fp == NULL){
perror("cannot read f");
return -1;
}
for (nrecords = 0; nrecords != MAXRECORDS; nrecords += 1) {
if (fgets(records[nrecords].author, sizeof(records[nrecords].author), fp) == NULL)
break;
removeEndSpaces(records[nrecords].author);
if (fgets(records[nrecords].title, sizeof(records[nrecords].title), fp) == NULL) {
fprintf(stderr, "invalid input file\n");
break;
}
removeEndSpaces(records[nrecords].title);
/* the more secure way to read the number is first to read the line then read the enumber in that line */
if ((fgets(line, sizeof(line), fp) == NULL) ||
(sscanf(line, "%hu", &records[nrecords].numberOfSales) != 1)) {
fprintf(stderr, "invalid input file\n");
break;
}
}
/* nrecords values the number of records read without error */
for (int i = 0; i != nrecords; i += 1)
printf("%s : %s / %hu\n",
records[i].author, records[i].title, records[i].numberOfSales);
return 0;
}
As you see it is useless to do your stuff with the index and the code is more clear
Supposing the file f contains your input, compilation and execution :
pi#raspberrypi:/tmp $ gcc -Wall -Werror -pedantic a.c -g
pi#raspberrypi:/tmp $ ./a.out
invalid input file
LEANN RIMES : WHAT A WONDERFUL WORLD / 4628
BLUE CHEER : WHAT DOESNT KILL YOU / 9664
pi#raspberrypi:/tmp $
As you see the file is invalid, the reason is the author "WITHIN TEMPTATION & KEITH CAPUTO" more the newline is too long to be saved in 20 characters, this is why you always need to check what happens and never suppose all is ok : in your initial code out of your other problems fscanf write out of the items with an undefined behavior. To read for instance up to 20 characters including the null character in a string with (f/s)scanf use the format "%20s"
If I resize the field author to 40 all is ok :
pi#raspberrypi:/tmp $ gcc -Wall -Werror -pedantic a.c -g
pi#raspberrypi:/tmp $ ./a.out
LEANN RIMES : WHAT A WONDERFUL WORLD / 4628
BLUE CHEER : WHAT DOESNT KILL YOU / 9664
WITHIN TEMPTATION & KEITH CAPUTO : WHAT HAVE YOU DONE / 3226
WITHIN TEMPATION : WHAT HAVE YOU DONE / 8093
KOKO TAYLOR : WHAT IT TAKES (THE CHESS YEARS) / 7160
DOOBIE BROTHERS : WHAT WERE ONCE VICES ARE NOW HABITS / 2972
LIL'ED & THE BLUES IMPERIALS : WHAT YOU SEE IS WHAT YOU GET / 9443
VARIOUS ARTISTS : WHAT'S SHAKIN / 4473
pi#raspberrypi:/tmp $

C reading file using ./a.out<filename and how to stop reading

In my class today we were assigned a project that involves reading in a file using the ./a.out"<"filename command. The contents of the file look like this
16915 46.25 32 32
10492 34.05 56 52
10027 98.53 94 44
13926 32.94 19 65
15736 87.67 5 1
16429 31.00 58 25
15123 49.93 65 38
19802 37.89 10 20
-1
but larger
My issue is that any scanf used afterwards is completely ignored and just scans in what looks like garbage when printed out, rather than taking in user input. In my actual program this is causing an issue with a menu that requires input.
How do I get the program to stop reading the file provided by the ./a.out"<"filename command?
also I stop searching at -1 rather than EOF for the sake of not having an extra set of array data starting with -1
ex
-1 0 0 0
in my real program the class size is a constant that is adjustable and is used to calculate class averages, I'd rather not have a set of 0's skewing that data.
#include <stdio.h>
int main(void)
{
int i = 0,j = 1,d,euid[200],num;
int tester = 0;
float hw[200],ex1[200],ex2[200];
while(j)
{
scanf("%d",&tester);
if( tester == -1)
{
j = 0;
}
else
{
euid[i] = tester;
}
scanf("%f",hw+i);
scanf("%f",ex1+i);
scanf("%f",ex2+i);
i++;
}
for(d = 0;d < 50;d++) /*50 because the actual file size contains much more than example*/
{
printf("euid = %d\n",euid[d]);
printf("hw = %f\n",hw[d]);
printf("ex1 = %f\n",ex1[d]);
printf("ex2 = %f\n",ex2[d]);
}
printf("input something user\n");
scanf("%d",&num);
printf("This is what is being printed out -> %d\n",num);
return 0;
}
I'm having the exact same problem. Tried every method I could find to eat the remaining input in the buffer, but it never ends.
Got it to work using fopen and fscanf, but the prof. said he prefers the code using a.out < filename
Turns out this is in fact not possible.

Use fscanf to read two lines of integers

I want to ask something that I write in C.
I use the fopen() command to open and read a text file that contains only two lines. in
first line is an integer N number, and in the second line is the N integer numbers that the first line says.
Eg.
-------------- nubmers.txt --------------
8 <-- we want 8 numbers for the 2nd line
16 8 96 46 8 213 5 16 <-- and we have 8 numbers! :)
but I want to take restrictions when the file openend.
the number N should be between 1 ≤ Ν ≤ 1.000.000. If not then show an error message. If the file is ok then the programm continue to run with another code.
Here is what I done until now:
int num;
....
fscanf(fp,"%d",&num); // here goes the fscanf() command
if(num<1 || num>1000000) // set restrictions to integer
{
printf("The number must be 1<= N <= 1.000.000",strerror(errno)); // error with the integer number
getchar(); // wait the user press a key
return 0; // returning an int of 0, exit the program
}
else // if everything works.....
{
printf("work until now"); // Everything works until now! :)
getchar(); // wait the user press a key
return 0; // returning an int of 0, exit the program
}
But the problem is that the restriction checks only for the first line number , it's correct though, but don't read the numbers in the second line.
What I mean is that :
Lets say that I have the number 10 in the first line.
The code will analyze the number, will check for restrictions and will proceed to the 'else' part
else // if everything works.....
{
printf("work until now"); // Everything works until now! :)
getchar(); // wait the user press a key
return 0; // returning an int of 0, exit the program
}
..and it will said that everything is working.
But what if I have 20 numbers in the second line? -when I need only 10
Eg.
-------------- nubmers.txt --------------
10
16 8 96 46 8 213 5 16 8 9 21 5 69 64 58 10 1 7 3 6
So I hoped be as cleared as I could. My question is that I need a code in the program, besides the 1st restriction, that have also another one restriction under the first that will read the second line of the txt file with the numbers and check if there are as many numbers as the first line says!
How do I do that?
If you guys want any other declarations feel free to ask!
Hope I was clear with my problem :)
This will check the number of integers and report too many or not enough. The integers are not saved except for each one being read into the value. Do you want to store each integer?
fscanf(fp,"%d",&num); // here goes the fscanf() command
if(num<1 || num>1000000) // set restrictions to integer
{
printf("The number must be 1<= N <= 1.000.000",strerror(errno)); // error with the integer number
getchar(); // wait the user press a key
return 0; // returning an int of 0, exit the program
}
else // if everything works.....
{
int i = 0;
int value = 0;
while ( fscanf ( fp, "%d", &value) == 1) { // read one integer
i++; // this loop will continue until EOF or non-integer input
}
if ( i > num) {
printf ( "too many integers\n");
}
if ( i < num) {
printf ( "not enough integers\n");
}
getchar(); // wait the user press a key
return 0; // returning an int of 0, exit the program
}
use a loop that takes the first num and checks is is the number of integers in next line:
int z = num;
while(z--){
if (getchar() == EOF)
printf("err")
}
Do it like this:
fscanf(fp,"%d",&num);
// next lines of code (restrictions). Then place the below code before getchar in the else
int temp[num+1];// space to store num integers to temp and 1 space to check for extra number
for(i=0;i<num;i++)
{
if(fscanf(fp,"%d",&temp[i]) != 1)// fscanf will automatically read 2nd line and store them in temp array
//error ! Less numbers in file !
}
if(fscanf(fp,"%d",&temp[num]==1) //if still numbers can be scanned
//Extra numbers found in line 2

C Primer 5th - Task 14-6

A text file holds information about a softball team. Each line has data arranged as follows:
4 Jessie Joybat 5 2 1 1
The first item is the player's number, conveniently in the range 0–18. The second item is the player's first name, and the third is the player's last name. Each name is a single word. The next item is the player's official times at bat, followed by the number of hits, walks, and runs batted in (RBIs). The file may contain data for more than one game, so the same player may have more than one line of data, and there may be data for other players between those lines. Write a program that stores the data into an array of structures. The structure should have members to represent the first and last names, the at bats, hits, walks, and RBIs (runs batted in), and the batting average (to be calculated later). You can use the player number as an array index. The program should read to end-of-file, and it should keep cumulative totals for each player.
The world of baseball statistics is an involved one. For example, a walk or reaching base on an error doesn't count as an at-bat but could possibly produce an RBI. But all this program has to do is read and process the data file, as described next, without worrying about how realistic the data is.
The simplest way for the program to proceed is to initialize the structure contents to zeros, read the file data into temporary variables, and then add them to the contents of the corresponding structure. After the program has finished reading the file, it should then calculate the batting average for each player and store it in the corresponding structure member. The batting average is calculated by dividing the cumulative number of hits for a player by the cumulative number of at-bats; it should be a floating-point calculation. The program should then display the cumulative data for each player along with a line showing the combined statistics for the entire team.
team.txt (text file I'm working with):
4 Jessie Joybat 5 2 1 1
4 Jessie Joybat 7 3 5 3
7 Jack Donner 6 3 1 2
11 Martin Garder 4 3 2 1
15 Jaime Curtis 7 4 1 2
2 Curtis Michel 3 2 2 3
9 Gillan Morthim 9 6 6 7
12 Brett Tyler 8 7 4 3
8 Hans Gunner 7 7 2 3
14 Jessie James 11 2 3 4
12 Brett Tyler 4 3 1 3
Since I'm a beginner in C, either I misinterpreted the task from what was asked originally or it's unfairly complex (I believe the former is the case). I'm so lost that I can't think of the way how could I fill in by the criteria of index (player number) every piece of data, keep track of whether he has more than one game, calculate and fetch bat average and then print.
What I have so far is:
#define LGT 30
struct profile {
int pl_num;
char name[LGT];
char lname[LGT];
int atbat[LGT/3];
int hits[LGT/3];
int walks[LGT/3];
int runs[LGT/3];
float batavg;
};
//It's wrong obviously but it's a starting point
int main(void)
{
FILE *flx;
int i,jc,flow=0;
struct profile stat[LGT]={{0}};
if((flx=fopen("team.txt","r"))==NULL) {
fprintf(stderr,"Can't read file team!\n");
exit(1);
}
for( jc = 0; jc < 11; jc++) {
fscanf(flx,"%d",&i);
stat[i].pl_num=i;
fscanf(flx,"%s",&stat[i].name);
fscanf(flx,"%s",&stat[i].lname);
fscanf(flx,"%d",&stat[i].atbat[flow]);
fscanf(flx,"%d",&stat[i].hits[flow]);
fscanf(flx,"%d",&stat[i].walks[flow]);
fscanf(flx,"%d",&stat[i].runs[flow]);
flow++;
}
}
Advice 1: don't declare arrays like atbat[LGT/3].
Advice 2: Instead of multiple fscanf you could read the whole line in a shot.
Advice 3: Since the number of players is limited and the player number has a good range (0-18), using that player number as an index into the struct array is a good idea.
Advice 4: Since you need cumulative data for each player (no need to store his history points), then you don't need arrays of integers, just an integer to represent the total.
So:
#include <stdio.h>
#define PLAYERS_NO 19
typedef struct
{
char name[20+1];
char lastName[25+1];
int atbat;
int hits;
int walks;
int runs;
float batavg;
} Profile;
int main(int argc, char** argv)
{
Profile stats[PLAYERS_NO];
int i;
FILE* dataFile;
int playerNo;
Profile tmpProfile;
int games = 0;
for(i=0; i<PLAYERS_NO; ++i)
{
stats[i].name[0] = '\0';
stats[i].lastName[0] = '\0';
stats[i].atbat = 0;
stats[i].hits = 0;
stats[i].walks = 0;
stats[i].runs = 0;
}
dataFile = fopen("team.txt", "r");
if ( dataFile == NULL )
{
fprintf(stderr, "Can't read file team!\n");
exit(1);
}
for(i=0; i<PLAYERS_NO && !feof(dataFile); ++i, ++games)
{
fscanf(dataFile, "%d", &playerNo);
if ( playerNo <0 || playerNo > PLAYERS_NO )
{
fprintf(stderr, "Player number out of range\n");
continue;
}
fscanf(dataFile, "%s %s %d %d %d %d",
&tmpProfile.name,
&tmpProfile.lastName,
&tmpProfile.atbat,
&tmpProfile.hits,
&tmpProfile.walks,
&tmpProfile.runs);
printf("READ: %d %s %s %d %d %d %d\n",
playerNo,
tmpProfile.name,
tmpProfile.lastName,
tmpProfile.atbat,
tmpProfile.hits,
tmpProfile.walks,
tmpProfile.runs);
strcpy(stats[playerNo].name, tmpProfile.name);
strcpy(stats[playerNo].lastName, tmpProfile.lastName);
stats[playerNo].atbat += tmpProfile.atbat;
stats[playerNo].hits += tmpProfile.hits;
stats[playerNo].walks += tmpProfile.walks;
stats[playerNo].runs += tmpProfile.runs;
}
/* exercise: compute the average */
fclose(dataFile);
for(i=0; i<PLAYERS_NO; ++i)
{
if ( stats[i].name[0] == '\0' )
continue;
printf("%d %s %s %d %d %d %d\n",
i,
stats[i].name,
stats[i].lastName,
stats[i].atbat,
stats[i].hits,
stats[i].walks,
stats[i].runs);
}
return 0;
}
The first rule of programming: Divide and conquer.
So you need to identify individual operations. One such operation is "load one row of input", another is "look up a player". If you have some of those operations (more will come up as you go), you can start building your program:
while( more_input ) {
row = load_one_row()
player = find_player( row.name )
if( !player ) {
player = create_player( row.name )
add_player( player )
}
... do something with row and player ...
}
when you have that, you can start to write all the functions.
An important point here is to write test cases. Start with a simple input and test the code to read a row. Do you get the correct results?
If so, test the code to find/create players.
The test cases make sure that you can forget about code that already works.
Use a framework like Check for this.
If I were doing this, I'd start with a structure that only held one "set" of data, then create an array of those structs:
struct profile {
char name[NAMELEN];
char lname[NAMELEN];
int atbat;
int hits;
int walks;
int runs;
float batavg;
};
Since you're using the player's number as the index into an array, you don't need to store it into the structure too.
I think that will simplify the problem a little bit. You don't need to store multiple data items for a single player -- when you get a duplicate, you just ignore some of the new data (like the names, which should be identical) and sum up the others (e.g., at-bats, hits).

Trying to debug a c program segmentation fault using output from valgrind

A CLI program of mine compiles and runs fine on windows. Compiles fine on linux, but causes a segmentation fault when running.
I turned to stackoverflow for help, and found a few questions similar to what I was going to ask that suggested valgrind, which I just happen to have installed (woo!).
So I ran my program through valgrind, and got a depressingly large amount of output, but I shall start with the first error message:
==11951== Command: ./vt
==11951==
Loading...
Load default database? (y/n)y
Opened input file vtdb.~sv, reading contents...
==11951== Invalid write of size 1
==11951== at 0x400FA9: readnumberfromfile (in /home/rob/Documents/programming/c/vocabtest/vt)
==11951== by 0x400C21: getrecordsfromfile (in /home/rob/Documents/programming/c/vocabtest/vt)
==11951== by 0x401FFD: main (in /home/rob/Documents/programming/c/vocabtest/vt)
==11951== Address 0x53b05bb is 0 bytes after a block of size 11 alloc'd
==11951== at 0x4C28FAC: malloc (vg_replace_malloc.c:236)
==11951== by 0x400EAC: readnumberfromfile (in /home/rob/Documents/programming/c/vocabtest/vt)
==11951== by 0x400C21: getrecordsfromfile (in /home/rob/Documents/programming/c/vocabtest/vt)
==11951== by 0x401FFD: main (in /home/rob/Documents/programming/c/vocabtest/vt)
==11951==
...finished.
1180 entries read from vtdb.~sv.
The problem seems to be in readnumberfromfile, and I've looked through it, and I can't seem to find what's wrong with it!
Can anyone shed some light?
int readnumberfromfile (int maxvalue,char separator)
{
int number, i=0;
char ch;
char * buff = (char *)malloc(11);//allocate enough space for an 10-digit number and a terminating null
if (!buff) {printf("Memory allocation failed!\n");return 0;}//return 0 and print error if alloc failed
if (!maxvalue) maxvalue=MAXINTVALUE;
ch=getc(inputfile);
while (!isdigit(ch))
{
if (ch == separator||ch=='\n'||ch==EOF) {fprintf(stderr,"Format error in file\n");return 0;}//if no number found(reached separator before digit), print error and return 0
ch = getc(inputfile);//cycle forward until you reach a digit
}
while (i<11 && ch!=separator && ch!='\n')//stop when you reach '~', end of line, or when number too long
{
buff[i++]=ch;
ch = getc(inputfile); //copy number from file to buff, one char at a time
}
buff[i] = '\0';//terminate string
number = atoi(buff)<=maxvalue ? atoi(buff) : maxvalue;//convert string to number and make sure it's in range
free(buff);
return number;
}
This is called from getrecordsfromfile if that's of any use:
void getrecordsfromfile(char * inputfilename,char separator)
{
int counter = 0;
struct vocab * newvocab;
struct listinfo * newvocablist;
if (!(inputfile = fopen(inputfilename, "r")))
{
printf("Unable to read input file. File does not exist or is in use.\n");
}
else
{
printf("Opened input file %s, reading contents...\n",inputfilename);
while (!feof(inputfile))
{
newvocab = (struct vocab *)malloc(sizeof(struct vocab));
if (!newvocab)
{
printf("Memory allocation failed!\n");
return;
}
else
{
newvocab->question=readtextfromfile(MAXTEXTLENGTH,separator);
newvocab->answer=readtextfromfile(MAXTEXTLENGTH,separator);
newvocab->info=readtextfromfile(MAXTEXTLENGTH,separator);
newvocab->hint=readtextfromfile(MAXTEXTLENGTH,separator);
newvocab->right=readnumberfromfile(1,separator);
newvocab->counter=readnumberfromfile(0,separator);
newvocab->known=readnumberfromfile(3,separator);
switch (newvocab->known)
{
case 0: newvocablist = &n2l;break;
case 1: newvocablist = &norm;break;
case 2: newvocablist = &known;break;
case 3: newvocablist = &old;break;
}
addtolist(newvocab,newvocablist);
if (newvocab->question==NULL||newvocab->answer==NULL)
{
printf("Removing empty vocab record created from faulty input file...\n");
removefromlist(newvocab,newvocablist,1);
}
else counter++;
}
}
fclose(inputfile);
printf("...finished.\n%i entries read from %s.\n\n",counter,inputfilename);
}
return;
}
Full source can be gitted from https://github.com/megamasha/Vocab-Tester
A couple of notes: I am trying to help myself, I have done my research, looked at similar questions and found out about valgrind myself.
I am still a relative beginner though, and while I appreciate solutions (WHAT to do to fix it), yet more useful is knowledge (HOW to fix or avoid it myself next time). I am here (and very keen) to learn.
buff[i] = '\0';//terminate string
in here i == 11, since you allocated only 11 chars, and while condition ends when i=11.
so, you access a memory you did not allocate.
the behavior for this situation is not defined.
you can solve this by allocating one extra character on your malloc.
int number, i=0;
...
while (i<11 ...
You are reading up to eleven digits for i = 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 and 10. And then trying to stick the \0 in twelfth slot buff[11].
It's called an "off by one error".
So the fix depends on what you want to change. If you want to accept 11 characters, change the malloc of buff. If you want to only accept 10, then change the while condition.
Invalid write of size 1
You're probably writing a char
Address 0x53b05bb is 0 bytes after a block of size 11 alloc'd
You've only just overflowed something of size 11
Both in readnumberfromfile
This is suspiciously related (by the sizes):
char * buff = (char *)malloc(11);
This will be done with i = 11 after the loop, which is past the end of the allocation:
buff[i] = '\0'
As wormsparty says you can get valgrind to be more helpful, by getting debug symbols in your binary.
For later, if you compile with -g, valgrind will show you exactly at which line the segfault happened.

Resources