Thread: help with structs and malloc!

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    i saw something like 8 'malloc' calls, and still no 'free' calls. are you reading what im saying? fix your memory leaks.

    Quote Originally Posted by coni View Post
    why are my symbols getting replaced? when I just call the functions in table.c without reading from an input file, it adds and prints fine.
    if this is true, then the only reason is that youre reading the input file incorrectly. i havent read all of your code again so i dont really remember it, but say your input file is this:
    Quote Originally Posted by input.txt
    abc
    def
    123
    456
    then in your program, for every read you do, print out what value it was, surrounded by quotes. surrounding it by quotes makes it obvious as to the "boundaries" of the value. if you dont have quotes (or whatever delimiter) then you may not notice a leading/trailing space/newline or other characters you dont expect. output after reading each value would be something like:
    Code:
    line #1 from file: 'abc'
    i see you have some output, but i can assume you arent doing this (printing out the values read from the file). because if you were, you would have also given us the contents of the file you were reading, so we can also verify it.

    again, this is assuming your code is correct, as you say, and that its just a problem with reading values from file.

  2. #2
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    - it seems you have a lot of pointer arithmetic and looping which is huge potential for logic errors such as those your experiencing with the dynamic memory. i would review those algorithms/loops and trace it on paper. if you trace a single, just one, runthrough of your program, with adding just one or maybe two elements to your table, you will solve it. this is somewhat tedious, however it is very common to do (i.e. in algorithms courses and in general debugging) and it is a near-guaranteed way of solving your problem, down to the exact line. (i say near-guaranteed because it may be some external thing, such as OS, disk space, permissions, etc, that cant be verified with your code on paper).

    - you still do not have "free" calls, who knows, maybe your program is fine but due to all of the memory leaks its breaking something? fix this. otherwise i will stop donating my time giving you suggestions if you dont implement or even at least comment on them saying your not going to implement them for whatever reason.

    - you didnt post your "addUse" and "addDef" functions with your latest updated code (could the problem be in there?)

    - have you verified there is no logic error in your file IO? you should strip your code down to the minimum possible (file IO shouldnt be required to test your code, just use std/console IO), to reduce complexity and to minimize introducing problems. i.e., does the program run fine with STD IO and no calls to "printTable"? try and partition the problem like this.

    anyways, it seems you do some code, run into a problem, post here, get input or a solution and implement it (or not even implement it), and go on to adding more functionality/code. you should verify that your initial problem (whatever that even was, i dont remember) is indeed fixed. that is, go back to that initial "fixed" code here, and test it with adding 0, 1, 5, 100, 1000+ entries (though you may run out of memory due to all of your memory leaks). test it with weird input strings and numbers. then move on to working with files, or printing it, or whatever it is your trying to do. when you test an algorithm/program with one set of input, it means nothing. only when you test it with "sufficiently large" variety of inputs can you gain some confidence that it "works".

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Malloc for an Array of Structs
    By pseudonoma in forum C Programming
    Replies: 3
    Last Post: 03-26-2008, 01:55 PM
  2. Malloc with structs.
    By jrdoran in forum C Programming
    Replies: 4
    Last Post: 12-11-2006, 11:26 PM
  3. malloc for structs and arrays
    By rkooij in forum C Programming
    Replies: 15
    Last Post: 05-04-2006, 07:38 AM
  4. malloc
    By bazzano in forum C Programming
    Replies: 3
    Last Post: 04-19-2006, 09:53 AM
  5. Creating a pointer to an array of structs
    By Jonathan_ingram in forum C Programming
    Replies: 8
    Last Post: 12-31-2003, 08:49 AM