Hi,
I am working on a project for school and I am trying to get my head
around something weird. I have a function setup which is used to get
an input from stdin and a piece of memory is created on the heap using
malloc. This input is then run through a regex and another test. The
result is passed back as a char pointer to the program for further
processing
The first time I call this function, it performs normally. However,
after that if I call it in succession, the contents of the char array
behave strangely. Example, if I input a set of 3 numbers like "345" on
the first try, the strlen function returns the correct number 3 once
main has control of the program flow. But then if I go through the
loop again and type in 3 more numbers, the resulting array now has a
strlen of 4. Before I get the new number, I free the memory of the
char pointer that was used to hold the string. If I loop through this
over and over, the size of the strlen result changes and when I print
out the ASCII values of the string, it shows alot of weird characters
are showing up in there.
I need some help, I'm stuck. Below is the entire code I'm working with
(including printf statements for troubleshooting purposes).
David
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <regex.h>
void jsw_flush ( void ) /*from http://c-for-dummies.com/faq/*/
{
int ch; /* getchar returns an int */
/* Read characters until there are none left */
do
ch = getchar();
while ( ch != EOF && ch != '\n') ;
clearerr ( stdin ); /* Clear EOF state */
}
/* match input against regex */
int match(const char *string, char *pattern)
{
int status;
regex_t re;
if(regcomp(&re, pattern, REG_EXTENDED|REG_NOSUB) != 0)
{ return 0; }
status = regexec(&re, string, (size_t)0, NULL, 0);
regfree(&re);
if(status != 0)
{ return 0; }
return 1;
}
/* get input from user, discard excess input */
char * inputFlush()
{
char temp[52];
char * input = NULL;
int len = 0;
fgets(temp, sizeof(temp), stdin);
temp[strlen(temp)-1] = '\0'; /* trim off last character */
len = strlen(temp);
printf("len: %i\n", len);
input = (char *)malloc(sizeof(char) * len);
strncpy(input, temp, len);
printf("input is %s\n", input);
jsw_flush();
return input;
}
int main()
{
int error = 1, number = 0, i = 0;
char * numInput1;
char * numPattern = "^-?[0-9]{1,5}$";
/* input - numbers */
while (number != -1)
{
printf("numInput1 is: %p\n", numInput1);
printf("Enter a number: ");
numInput1 = inputFlush();
printf("numInput1 is: %p\n", numInput1);
for (i=0; i < strlen(numInput1); i++)
printf("%x\n", numInput1[i]);
printf("Strlen(numInput1) %i\n", strlen(numInput1));
printf("numInput1 contains: %s\n", numInput1);
error = match(numInput1, numPattern); /* run regex against number
input */
printf("error: %i\n", error);
if (!error) /* num contains illegal characters */
printf("number has illegal characters\n");
if (error)
{
number = atol(numInput1); /* convert string to int */
printf("number value: %i\n", number);
if (number <= 32767 && number >= -32768) /* check to see if in
valid range */
printf("number is in valid range\n"); /* int OK move on */
else
printf("number is not in range\n"); /* bad int, get another */
}
free(numInput1);
numInput1 = NULL;
}
}