CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 15 of 24

Threaded View

  1. #19
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,675

    Re: Call Array Member in function Help

    Quote Originally Posted by M2W View Post
    There is also a pseudo-random number generator function (a lot easier to work through since that's my background).
    Now these always look like kind of (black?) magic to me. I always was happy when I had ready-made ones at hand, and particularly appreciate the PRNG functionality offered by TR1.

    Oh, and my question about accessing them goes more like this: How can I access, say Node 3 directly and change its data member? You know, like if you had an array, you would just say Array[3].data = 10 or something like that.
    Well, the lack of random access capabilities is one of the key properties of a linked list. (This downside is compensated by certain advatages however.) If you only need direct access to a relatively small number of certain elements (and that preferably frequently), it might pay to put aside some separate direct pointers to these nodes. If you need direct access to a lot of nodes and/or not always the same ones...

    BTW, Where would I stick the delete to free the memory? Right before my function_two ends or somewhere in main? I'm thinking the former.
    Definitely main() would be the right place to do that, in particular right after leaving the while loop. And that's for two reasons: 1) As main() is also responsible for allocation of the nodes (indirectly by calling insert_node()) it is the natural choice. 2) If you actually wanted to do that in function_two(), it would be unnecessairily complicated to determine when to do that, as function_two() gets called (and returns) over and over, either from main() or recursively. And of course it wouldn't be a single delete, it would be one corresponding to each call to new, i.e. node.
    Last edited by Eri523; September 27th, 2010 at 09:22 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured