Thread: best way to uniquely tag a node

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    23

    best way to uniquely tag a node

    hi just a quik query,

    i am trying to implement unique id's for list nodes, that are safe, and can be locked,

    in php or javascript i find this easy, and can do this in ways not possible in c language, for obvious reasons...
    anyhow, i thought of assigning an integer type id to each node and incrementing that,
    but i am a bit short on ideas of how to make this safe,

    i thought of assigning my initial node's id with a value of one, and then on each new node created store the previous nodes id in a temp var, increment that and assign this to the next node, but i am worried that this might fall apart when deleting and and sorting nodes,

    so then i thought perhaps i could have a predefined variable that gets incremented with each new node, and the respective node's id has this value, but this too seems like it might have potential problems, especially if every node suddenly took on the same value,....

    well as i said i am a bit short on ideas,
    if anybody know of a relatively standard safe way to implement this, advice would be awesome, no source code is needed, just a heads up

  2. #2
    * noops's Avatar
    Join Date
    Jun 2008
    Posts
    108
    so then i thought perhaps i could have a predefined variable that gets incremented with each new node, and the respective node's id has this value, but this too seems like it might have potential problems, especially if every node suddenly took on the same value,....
    I don't see why this wouldn't work...

  3. #3
    Registered User
    Join Date
    Jan 2008
    Posts
    23

    yes thanks

    ja thanks,

    i came here a few minutes ago, read your post, told myself i was being lame and went put together something, i am glad to say that i will be able to do this:>

  4. #4
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Why not just use the address of the node? Guaranteed to be unique. No thinking either.
    Mainframe assembler programmer by trade. C coder when I can.

  5. #5
    Registered User
    Join Date
    Jul 2008
    Posts
    71
    Quote Originally Posted by Dino View Post
    Why not just use the address of the node? Guaranteed to be unique. No thinking either.
    Huh?I'm a C++ beginner....I remember asking a C++ guy in message board about ways to implement hashcode using object address to identify object uniqueness.He told me this is of little use since unlike Java,object address are visible and prone to error since we can mess this up using pointers.
    But now you brought this up....
    Just asking whether you can elaborate further what you said?
    thanks.
    Last edited by kypronite; 08-25-2008 at 07:45 PM. Reason: changed the word 'C++ expert' to 'C++ guy' :p

  6. #6
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Every node (item, thing, structure, whatever) in storage is going to be in memory at some particular address. If you have 5 items, each will have its own address in storage, like this:

    Item1 is at address 40568
    Item2 is at address 41770
    Item3 is at address 6000C
    Item4 is at address 1245644
    Item5 is at address 0BC

    No two items will ever occupy the same location in memory, therefore, they will all have unique addresses.

    It is a very common practice to associate the address of something as its unique identifier. If you are familiar with HANDLES, like in Windows programming, that's the same thing. Sometimes this unique address is also called a TOKEN.

    Visibility of where you keep your things in storage is a completely different aspect that the OP did not mention as a concern.

    C and C++ are built upon the concept of addresses, addressing, address arithmetic and addressability. No big deal. It sounds like the C++ guy was more of a Java fan, (and Java is a great language too), but driving your car is inherently life threatening too, and we do it every day.
    Mainframe assembler programmer by trade. C coder when I can.

  7. #7
    Registered User
    Join Date
    Jul 2008
    Posts
    71
    Quote Originally Posted by Dino View Post
    Every node (item, thing, structure, whatever) in storage is going to be in memory at some particular address. If you have 5 items, each will have its own address in storage, like this:

    Item1 is at address 40568
    Item2 is at address 41770
    Item3 is at address 6000C
    Item4 is at address 1245644
    Item5 is at address 0BC

    No two items will ever occupy the same location in memory, therefore, they will all have unique addresses.

    It is a very common practice to associate the address of something as its unique identifier. If you are familiar with HANDLES, like in Windows programming, that's the same thing. Sometimes this unique address is also called a TOKEN.

    Visibility of where you keep your things in storage is a completely different aspect that the OP did not mention as a concern.

    C and C++ are built upon the concept of addresses, addressing, address arithmetic and addressability. No big deal. It sounds like the C++ guy was more of a Java fan, (and Java is a great language too), but driving your car is inherently life threatening too, and we do it every day.
    thanks for your answer.But how about using static int i and then i++ for each object created.Therefore it is much safer?
    I hope I don't sound nit-picking or arguing with you.I'm more like wondering why should I travel using route A instead of route B.
    thanks.

  8. #8
    Registered User
    Join Date
    Jan 2008
    Posts
    23
    hi,

    came check other replies for interest,
    as this last post is suggesting, this is exactly how i went about implement a uniqe id,
    using the address is not something that crossed my mind, but will check it out anyhow, thanks.

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Using the address is an excellent way - and it has the advantage of not having to store any extra information [if you use "i++", then you would have to store that number in each node - the node's address is already stored in some way to store the node itself].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  10. #10
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Quote Originally Posted by kypronite View Post
    thanks for your answer.But how about using static int i and then i++ for each object created.Therefore it is much safer?
    I hope I don't sound nit-picking or arguing with you.I'm more like wondering why should I travel using route A instead of route B.
    thanks.
    Either route will get you there.

    One could argue that the "static int i" / "i++" method is less safe because you are managing the value yourself, and you could screw up.

    And I really don't think the term "safe" is really the best adjective for either method. Using the address of an item would probably be better described as "automatic" (or a "no brainer"), whereas the i++ method could be called "manual".

    Here's how I look at it. The less code I have to write is less code I have to support, less code that is apt to break or be susceptible to breaking, and the faster it can be written.

    Todd
    Mainframe assembler programmer by trade. C coder when I can.

  11. #11
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I'm with Todd here. And if "we can mess up with pointers" is an argument, then you should probably be considering that if you "mess up with pointers", there is MUCH more problems than the fact that you've messed up your unique identifier of the object - the object in itself is also lost or messed up in that case, so the whole system is broken if you mess up with your pointers - and it's usually very obvious.

    An integer ID on the other hand is decidedly riskier, as there is no guarantee that you can identify the correct value of your ID [you can check that your node has the correct ID by comparing the address with your stored tag].

    There is however one large drawback with using the address for unique ID - if you store the list to a file and reload it, the pointers will almost certainly not be the same as they where last time - particularly if you load and save things in different order [that is, you insert something in the middle of the list before saving, and the list is saved in the order elements are stored].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  12. #12
    Registered User
    Join Date
    Jan 2008
    Posts
    23
    thanks man, its not so much that i mess up with pointers, but was having a bit of a paranoid moment i guess, and actually felt kinda silly when i realized how silly i was sounding, i am not quite sure yet which way i will use, would've have stuck to the static int, but using the address could save some, if you were to saving to file you could just regenerate the id's when reloading the list,

    but you can tick this one as solved...

  13. #13
    * noops's Avatar
    Join Date
    Jun 2008
    Posts
    108
    There is no reason why managing the primary keys yourself would be less safe. You should be handling all operations on your list using functions anyways. And thus your 'manual' becomes 'automatic'.

    I would prefer to explicitly define how primary keys are generated and handled. I am thinking beyond a list; more like a database.

    It's going to depend on what you are developing and for what purpose.

  14. #14
    Registered User
    Join Date
    Jul 2008
    Posts
    71
    thanks all the poster above for your replies.It seem that I always thought of "cons" but not "pros".

    Now I have a better judgement.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help Debugging my AVL tree program.
    By Nextstopearth in forum C Programming
    Replies: 2
    Last Post: 04-04-2009, 01:48 AM
  2. Unknown memory leak with linked lists...
    By RaDeuX in forum C Programming
    Replies: 6
    Last Post: 12-07-2008, 04:09 AM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM