Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. linked list problem

linked list problem

Scheduled Pinned Locked Moved C / C++ / MFC
helpc++javacomdata-structures
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    K Shaffer
    wrote on last edited by
    #1

    I wrote a linked list to hande some object that I created. When I try to print the list, it prints garbage. I made a temporary project to test my class and it works fine, so the problem is how I am using it in my project. I think that it is a memory issue and that the objects are being written over after I store them in my list. This is how I am using the list. //funtion to create object that will be stored in list. CApproach ApproachPage::ParseApproach(CString string) { ...... CApproach approach(Chart_Name, Chart_Num, Chart_Rev_Date); return approach; } //Insert class in List.cpp int PrintList::Insert(CApproach* approach ) { ..... } //main.h PrintList* list; //contructor of main list = new PrintList(); //main list->Insert(&ParseApproach(codestring)); //prints garbage to the outputfile, but works in another project list->print() I learned to program in java so pointers and c++ memory are far from my specialty. Any help would be great. Thanks. Kevin Shaffer kshaff03@msn.com

    P J 2 Replies Last reply
    0
    • K K Shaffer

      I wrote a linked list to hande some object that I created. When I try to print the list, it prints garbage. I made a temporary project to test my class and it works fine, so the problem is how I am using it in my project. I think that it is a memory issue and that the objects are being written over after I store them in my list. This is how I am using the list. //funtion to create object that will be stored in list. CApproach ApproachPage::ParseApproach(CString string) { ...... CApproach approach(Chart_Name, Chart_Num, Chart_Rev_Date); return approach; } //Insert class in List.cpp int PrintList::Insert(CApproach* approach ) { ..... } //main.h PrintList* list; //contructor of main list = new PrintList(); //main list->Insert(&ParseApproach(codestring)); //prints garbage to the outputfile, but works in another project list->print() I learned to program in java so pointers and c++ memory are far from my specialty. Any help would be great. Thanks. Kevin Shaffer kshaff03@msn.com

      P Offline
      P Offline
      Phil Hamer
      wrote on last edited by
      #2

      The CApproach object returned from ParseApproach is a temporary object. You're passing the address of this temporary to Insert. When the temporary object is destroyed automatically the pointer to it points to garbage. You can either let Insert take a copy of a CApproach object instead of a pointer: int PrintList::Insert(CApproach approach) or you can assign the result of ParseApproach to a local variable, but keep in mind that when that variable goes out of scope, the pointer to it will point to garbage: CApproach result = ParseApproach(codestring); list->Insert(&result); list->print(); You could also pass Insert a pointer to an object allocated on the heap, but you have to remember to delete it when you're done with it.

      1 Reply Last reply
      0
      • K K Shaffer

        I wrote a linked list to hande some object that I created. When I try to print the list, it prints garbage. I made a temporary project to test my class and it works fine, so the problem is how I am using it in my project. I think that it is a memory issue and that the objects are being written over after I store them in my list. This is how I am using the list. //funtion to create object that will be stored in list. CApproach ApproachPage::ParseApproach(CString string) { ...... CApproach approach(Chart_Name, Chart_Num, Chart_Rev_Date); return approach; } //Insert class in List.cpp int PrintList::Insert(CApproach* approach ) { ..... } //main.h PrintList* list; //contructor of main list = new PrintList(); //main list->Insert(&ParseApproach(codestring)); //prints garbage to the outputfile, but works in another project list->print() I learned to program in java so pointers and c++ memory are far from my specialty. Any help would be great. Thanks. Kevin Shaffer kshaff03@msn.com

        J Offline
        J Offline
        John M Drescher
        wrote on last edited by
        #3

        If this is not a homework assignment you should try and use one of the built-in lists link CList or better yet list. Clist is a MFC template class and list is STL. See the MDSN help for documentation. There really is no good reason to have to create your own linked list when there are very good lists available. John

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups