• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • paul wheaton
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Liutauras Vilda
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Piet Souris
Bartenders:
  • salvin francis
  • Mikalai Zaikin
  • Himai Minh

loading LL from binary file, last node always corrupt

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I can't figure out what is wrong with this function.
I have tried multiple input files, but with every one the last node is corrupt.
I'm certain it is not the input file because I printed out the "input" before the last node is added to the linked list.
Someone please see if they can see why this code is not working correclty.
All other nodes are valid and correct.

Thanks!



[/CODE]
 
Rancher
Posts: 301
11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Lancelot

If I understand correctly, ptr points to the last record. But you free(ptr) near the end of the function, which might be what's corrupting your last record.
 
Marshal
Posts: 72612
317
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why are you maybe allocating input with malloc(...) repeateedly?
 
John Matthews
Rancher
Posts: 301
11
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Lancelot can correct me if I'm wrong, but that's allocating memory for the next record to be read in. Hence that does need to be freed at the end (because a record wasn't read into it), unlike ptr.
 
Campbell Ritchie
Marshal
Posts: 72612
317
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Right. Thank you.
 
L Link
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you John and Ritchie.
I am so grateful for the quick replies and the solution to my problem.
My forehead and display are also very grateful.

I haven't really written any code since college and the information you've shared is huge - this is my first LL since then.
Ritchie hit on the first bug that killed me, I kept getting "only" the last record from the input file until I realized I had to allocate a new block of memory to "input" for each node as it is pulled out of the file and then passed to "ptr" (which I think I will rename "tail" for clarity).  Then I started getting all my nodes but the last was corrupt.  So to make sure I understand the solution correctly (I believe just making code work is not the full solution if you don't understand why it works).

John -
I should free "input" since it is allocated but unused at the end of the program.  That way it is returned to the heap and does not create a memory leak eating up my RAM.
"ptr" however is still being used for the last node in my linked list, so when I free it, I corrupt my address to the memory location of my last node.

Do I understand the solution correctly?

Yay and thank you again!
Now I need to convert it to a double linked list.
 
John Matthews
Rancher
Posts: 301
11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Lancelot Link wrote:Do I understand the solution correctly?

Yes, if I understand you correctly

Some observations/suggestions:
  • Allocate input memory inside the loop before doing the fread(). Then you don't need 2 input malloc()s.
  • The ptr malloc - I think that is a bug, you shouldn't be doing that. I guess that's why you added the free(ptr) at the end. ptr should just be pointing to the last record, but input is where you malloc() the memory.
  • Use sizeof(*input) instead of sizeof(struct Record) to make the code independent of input's type.
  • You can use a double-pointer to avoid having the special case for the empty list. That is, input is a Record**, which initially points to &head, but subsequently points to the &next of the last record read in. Then you don't need the 'if' inside the for() loop. I'll write some code later if you like and I get time, but you could try it yourself.
  •  
    L Link
    Greenhorn
    Posts: 5
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    I will try it myself but if you post some sample code I would also appreciate it.
     
    John Matthews
    Rancher
    Posts: 301
    11
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Apologies - it's C.


     
    L Link
    Greenhorn
    Posts: 5
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    My code is C, it is incorrectly marked as C++
     
    John Matthews
    Rancher
    Posts: 301
    11
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Right - then you shouldn't be casting those malloc() return values
     
    L Link
    Greenhorn
    Posts: 5
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Ok, tnx - will save some keystrokes in the future
     
    You showed up just in time for the waffles! And this tiny ad:
    Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
    https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    reply
      Bookmark Topic Watch Topic
    • New Topic