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 Question

Linked List Question

Scheduled Pinned Locked Moved C / C++ / MFC
questiondata-structuresregexperformancetutorial
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.
  • A Offline
    A Offline
    Abin
    wrote on last edited by
    #1

    I'm thinking of how a linked list verifies whether a given node pointer points to a valid node in the list. For example, suppose we have a linked list which chains up 10000 NODE, the user calls one member functions which takes a NODE* as parameter: InsertBefore(const NODE* pBefore, LPVOID lpData), now how does the linked list verify if pBefore points to a valid NODE which is contained in the list? I think basically there would be 2 possibilities: Solution 1, Use ::IsValidReadPtr and ::IsValidWritePtr to check if the memory is available to current process and the consecutive memory block space are large enough to be read/written against a NODE object. Solution 2, Travel through the whole list and exam pBefore with address of each NODE, stops whenever a match is found. Personally I think solution 2 is definitely a no go because that would make the linked list ultimately slow if it contains a larget amount of nodes. Solution 1 is faster but not safe, because that way we cannot really make sure if pBefore really points to a NODE or not, it could be any object whose size is same or greater than a NODE and if we use some iterating method like while (pNode->pNext != NULL) {...;} we'll get our system hang... So I really want to know how a linked list verify a NODE*, is there any better solution, thanks.

    N D 2 Replies Last reply
    0
    • A Abin

      I'm thinking of how a linked list verifies whether a given node pointer points to a valid node in the list. For example, suppose we have a linked list which chains up 10000 NODE, the user calls one member functions which takes a NODE* as parameter: InsertBefore(const NODE* pBefore, LPVOID lpData), now how does the linked list verify if pBefore points to a valid NODE which is contained in the list? I think basically there would be 2 possibilities: Solution 1, Use ::IsValidReadPtr and ::IsValidWritePtr to check if the memory is available to current process and the consecutive memory block space are large enough to be read/written against a NODE object. Solution 2, Travel through the whole list and exam pBefore with address of each NODE, stops whenever a match is found. Personally I think solution 2 is definitely a no go because that would make the linked list ultimately slow if it contains a larget amount of nodes. Solution 1 is faster but not safe, because that way we cannot really make sure if pBefore really points to a NODE or not, it could be any object whose size is same or greater than a NODE and if we use some iterating method like while (pNode->pNext != NULL) {...;} we'll get our system hang... So I really want to know how a linked list verify a NODE*, is there any better solution, thanks.

      N Offline
      N Offline
      Navin
      wrote on last edited by
      #2

      AFAIK, many linked lists use the "honor system"... that is, when you insert a node, it takes your word for it that it's a valid node. :) The STL linked list uses iterators for this purpose, which makes it less likely to err. You can still mess up if you pass in a list an iterator from a different list, though. If your nose runs and your feet smell, then you're built upside down.

      1 Reply Last reply
      0
      • A Abin

        I'm thinking of how a linked list verifies whether a given node pointer points to a valid node in the list. For example, suppose we have a linked list which chains up 10000 NODE, the user calls one member functions which takes a NODE* as parameter: InsertBefore(const NODE* pBefore, LPVOID lpData), now how does the linked list verify if pBefore points to a valid NODE which is contained in the list? I think basically there would be 2 possibilities: Solution 1, Use ::IsValidReadPtr and ::IsValidWritePtr to check if the memory is available to current process and the consecutive memory block space are large enough to be read/written against a NODE object. Solution 2, Travel through the whole list and exam pBefore with address of each NODE, stops whenever a match is found. Personally I think solution 2 is definitely a no go because that would make the linked list ultimately slow if it contains a larget amount of nodes. Solution 1 is faster but not safe, because that way we cannot really make sure if pBefore really points to a NODE or not, it could be any object whose size is same or greater than a NODE and if we use some iterating method like while (pNode->pNext != NULL) {...;} we'll get our system hang... So I really want to know how a linked list verify a NODE*, is there any better solution, thanks.

        D Offline
        D Offline
        Dave Bryant
        wrote on last edited by
        #3

        I don't think there is any way you could 100% guarantee that a node passed to one of the member functions was valid. However, you could add an extra member to the NODE that was a pointer back to the linked list. This would reduce the chances of a node being passed in from another list, but even that is still not a guarantee that it is valid. Dave http://www.cloudsofheaven.org

        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