Like Tree5Likes

program adding numbers using desired base doesn't show any output

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 ...

  1. #16
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    What I meant was that you should
    Code:
    while (( number1 = getnum(infile, base)) != NULL)
    and make getnum() return NULL when EOF was returned by any read function

    With other words : Forget feof().

    Kurt
    Pole likes this.

  2. #17
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Quote Originally Posted by Pole View Post
    valgrind -v gives me no errors, so I guess no memory leak there.
    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.
    AndiPersti and Pole like this.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  3. #18
    Registered User
    Join Date
    Nov 2011
    Posts
    39
    Quote Originally Posted by oogabooga View Post
    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.
    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.
    Code:
    while (((number_next = getnum(infile, base)) != NULL) ) {
            result = add(result, number_next, base);
            free(result);
            free(number_next);
            if (feof(infile) != 0)
                break;
        }
    Shortened summary from valgrind -v:
    On an external server, didn;t want to put so many text here.

    Ideone.com | Online C Compiler & Debugging Tool

    Thanks in advance!

  4. #19
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    You cannot free result in that loop. It's still needed in the next iteration
    Code:
    while (((number_next = getnum(infile, base)) != NULL) ) {
            result = add(result, number_next, base);
            free(result);
            free(number_next);
            if (feof(infile) != 0)
                break;
        }
    Kurt

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Not Getting Desired Output
    By DevoAjit in forum C Programming
    Replies: 2
    Last Post: 03-09-2012, 05:46 AM
  2. Program doesn't show up
    By Krasimir11 in forum C Programming
    Replies: 7
    Last Post: 01-05-2006, 06:21 PM
  3. Replies: 3
    Last Post: 01-08-2004, 09:43 PM
  4. not getting the desired output. WHY???
    By KristTlove in forum C++ Programming
    Replies: 4
    Last Post: 11-06-2003, 02:08 PM
  5. adding base n numbers
    By doogle in forum C++ Programming
    Replies: 4
    Last Post: 11-11-2002, 11:23 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21