What I meant was that you shouldand make getnum() return NULL when EOF was returned by any read functionCode:while (( number1 = getnum(infile, base)) != NULL)
With other words : Forget feof().
Kurt
This is a discussion on program adding numbers using desired base doesn't show any output within the C Programming forums, part of the General Programming Boards category; What I meant was that you should Code: while (( number1 = getnum(infile, base)) != NULL) and make getnum() return ...
What I meant was that you shouldand make getnum() return NULL when EOF was returned by any read functionCode:while (( number1 = getnum(infile, base)) != NULL)
With other words : Forget feof().
Kurt
And yet it's obviously a memory leak. Where do you free the previous result? Where do you free number_next before getting the next number? What do you think happens to that memory?
You're either not using Valgrind properly (e.g., static linking?) or it's missing this one for some other reason.
The moral of the story is that you still have to use your brain in addition to other tools.
The human mind treats a new idea the way the body treats a strange protein; it rejects it. - P.B.Medawar
After doing what You advised - I get a segfault
Here is the change (rest the same as on the previous page), only changed second while loop in main.
Shortened summary from valgrind -v:Code:while (((number_next = getnum(infile, base)) != NULL) ) { result = add(result, number_next, base); free(result); free(number_next); if (feof(infile) != 0) break; }
On an external server, didn;t want to put so many text here.
Ideone.com | Online C Compiler & Debugging Tool
Thanks in advance!
You cannot free result in that loop. It's still needed in the next iteration
KurtCode:while (((number_next = getnum(infile, base)) != NULL) ) { result = add(result, number_next, base); free(result); free(number_next); if (feof(infile) != 0) break; }