Thread: Program works for stdin but fails for a file.

  1. #1
    Registered User
    Join Date
    Mar 2020
    Posts
    10

    Program works for stdin but fails for a file.

    I am studying data structures and was asked to build a program that uses a stack to reverse lines of text. Here it is: https://pastebin.com/raw/CGq6ZaHM

    The problem is: it works for the stdin (when I redirect some input into it), but when I call it with a file as argument, the following error appears:

    Code:
    free(): invalid pointer
    I could not find how free gets a invalid pointer from using getc(fp) instead of getc(stdin).
    Can someone help me find the error?
    Last edited by felixthunder; 03-21-2020 at 02:21 PM.

  2. #2
    Registered User
    Join Date
    Dec 2017
    Posts
    931
    It's pretty obvious you didn't write that code.
    If you want the truth to stand clear before you, never be for or against. - Sent-ts'an

  3. #3
    Registered User
    Join Date
    Mar 2020
    Posts
    10
    Quote Originally Posted by john.c View Post
    It's pretty obvious you didn't write that code.
    Why do you say that?
    I did wrote that code.

    Do you know why it doesn't work for text given by argument?

  4. #4
    Registered User
    Join Date
    Dec 2017
    Posts
    931
    It's the arbitrary actions of UB stemming from the uninitialized "top" pointer in "rev". It needs to be initialized to NULL.
    If you want the truth to stand clear before you, never be for or against. - Sent-ts'an

  5. #5
    Registered User
    Join Date
    Mar 2020
    Posts
    10
    Thanks,
    Such a dumb error.

    But hey, why did you say it's obvious I didn't write it?

  6. #6
    Registered User
    Join Date
    Dec 2017
    Posts
    931
    Quote Originally Posted by felixthunder View Post
    But hey, why did you say it's obvious I didn't write it?
    It struck me as an old-fashioned style. Maybe you're using an old book. Or have an old teacher. Whatever.
    If you want the truth to stand clear before you, never be for or against. - Sent-ts'an

  7. #7
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    718
    Quote Originally Posted by john.c View Post
    It struck me as an old-fashioned style. Maybe you're using an old book. Or have an old teacher. Whatever.
    @felixthunder

    The program could be written much simpler. getopt() is not needed. gcc seems to being used, assuming argv[0] does contain the program name.
    argc == 1, use stdin
    argc == 2, attempt to open the alleged filename provided as argv[1]
    argc > 2, too many arguments, print an error message and exit

    For a one .c file program, I would not bother with static functions, not wrong, but not needed

    @john.c
    Maybe you're using an old book. Or have an old teacher.
    "Old teacher"???

    The age of the teacher is not the problem! Bad, so-called, teachers, instructors, professors, etc... can be of any age, young or old!

    Teachers teaching using Turbo C, using K&R as the primary textbook, etc... Yes, it is still being done!

    An individual attempting to learn C using painfully inadequate resources, such as, online tutorials, YouTube videos, poorly written online code, etc..., rather than studying from an up to date textbook. Seen this too many times.

    BTW, yes, I am an older teacher! ;^)

  8. #8
    Registered User
    Join Date
    Mar 2020
    Posts
    10
    Yes, I am using old books, most from Kernighan and Rob Pike.
    I wrote this program for practicing with stacks, but I have another version that uses arrays: https://pastebin.com/raw/mmi0CCZR

    I am also learning about BSD extensions, such as err(3).

  9. #9
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    718
    Quote Originally Posted by felixthunder View Post
    Yes, I am using old books, most from Kernighan and Rob Pike.
    I wrote this program for practicing with stacks, but I have another version that uses arrays: https://pastebin.com/raw/mmi0CCZR

    I am also learning about BSD extensions, such as err(3).
    In addition to my comments above, rather than using an old book, I would recommend one of the following up to date books on the C Programming Language:

    C Programming, A Modern Approach
    Author: K. N. King

    C Primer Plus, 6th Edition
    Stephen Prata

    C How to Program, 8/e
    Deitel & Deitel

    Choose one, study cover to cover, and do all the exercises at the end of each chapter!
    I am also learning about BSD extensions, such as err(3).
    I also would recommend sticking to Standard C, and avoid any Non-Standard extensions.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 07-20-2014, 03:04 PM
  2. Compile under gcc fails, but VS2010 works
    By Sorin in forum C++ Programming
    Replies: 11
    Last Post: 01-20-2012, 02:45 AM
  3. This works fine with IP 127.0.0.1 but fails with my REAL IP.
    By azjherben in forum Networking/Device Communication
    Replies: 15
    Last Post: 05-19-2009, 10:28 PM
  4. Replies: 6
    Last Post: 05-08-2009, 08:51 PM
  5. Replies: 5
    Last Post: 04-13-2009, 09:55 AM

Tags for this Thread