As per my previous thread I went away and came up with a set of functions to help with data input. Unfortunately a segmentation fault is now showing up, apparently due to one of these functions, and I can't work out why.
Here's the code:
(FYI, [tt]MALLOC[/tt] and [tt]REALLOC[/tt] refer to functions I wrote to make memory allocation safer. I don't think the problem is with them.)
The problem appears to be a result of using the [tt]getDOUBLE()[/tt] function. The aim here is that if someone types more than one number (or a number followed by anything else) just the first number will be registered by the program.
What appears to happen is that if I call [tt]getDOUBLE()[/tt] and, in entering input type something of the form
[tt]number otherstuff[/tt]
Where [tt]number[/tt] is a double (e.g. 1.35) and [tt]otherstuff[/tt] can be literally anything, then the following happens: if the total number of characters entered is exactly 20, then the program falls over. This can be prevented if I comment out the [tt]free()[/tt] command in [tt]getDOUBLE()[/tt].
I'm really quite stuck as to why this should happen, and why it should be so specific in when it occurs---it doesn't happen with getINT(), and it only happens with getDOUBLE() in the specific circumstance of entering exactly 20 characters.
Any ideas?
Here's the code:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <joealloc.h>
#define MAXLINESIZE 2*BUFSIZ
char *readLINE(void) {
unsigned int linesize = 0;
char query[BUFSIZ],*line = NULL,*check;
do {
fgets(query,BUFSIZ,stdin);
check = strchr(query,'\n');
if(check)
*check = '\0';
linesize += strlen(query);
if(linesize < MAXLINESIZE) {
if(linesize>strlen(query)) {
REALLOC(line,line,linesize," REALLOC in readLINE in joeio.c");
strcat(line,query);
} else {
MALLOC(line,linesize," MALLOC in readLINE in joeio.c");
strcpy(line,query);
}
}
} while(check==NULL);
return line;
}
char *getSTRING(const unsigned int STRINGLENGTH) {
char query[BUFSIZ], *s, *check;
MALLOC(s,STRINGLENGTH," MALLOC in getSTRING in joeio.c");
fgets(s,STRINGLENGTH,stdin);
check = strchr(s,'\n');
if(check)
*check = '\0';
else do {
fgets(query,BUFSIZ,stdin);
check = strchr(query,'\n');
} while(check==NULL);
return s;
}
char getCHAR(void) {
char c = getchar(),d = getchar();
while(d!='\n')
d = getchar();
return c;
}
int getINT(void) {
int i;
char *input = readLINE();
sscanf(input,"%d",&i);
free(input);
return i;
}
double getDOUBLE(void) {
double x;
char *input = readLINE();
sscanf(input,"%le",&x);
free(input);
return x;
}
(FYI, [tt]MALLOC[/tt] and [tt]REALLOC[/tt] refer to functions I wrote to make memory allocation safer. I don't think the problem is with them.)
The problem appears to be a result of using the [tt]getDOUBLE()[/tt] function. The aim here is that if someone types more than one number (or a number followed by anything else) just the first number will be registered by the program.
What appears to happen is that if I call [tt]getDOUBLE()[/tt] and, in entering input type something of the form
[tt]number otherstuff[/tt]
Where [tt]number[/tt] is a double (e.g. 1.35) and [tt]otherstuff[/tt] can be literally anything, then the following happens: if the total number of characters entered is exactly 20, then the program falls over. This can be prevented if I comment out the [tt]free()[/tt] command in [tt]getDOUBLE()[/tt].
I'm really quite stuck as to why this should happen, and why it should be so specific in when it occurs---it doesn't happen with getINT(), and it only happens with getDOUBLE() in the specific circumstance of entering exactly 20 characters.
Any ideas?