Thread: Problem keeping track of instances

  1. #1
    Registered User
    Join Date
    Oct 2006
    Location
    UK/Norway
    Posts
    485

    Problem keeping track of instances

    Hallo,

    I am having an array of structures that I want to be able to abstract behind a helper class. I want to be able to create a way of referencing the instance of the structure inside of the helper class in a way that the reference will always be valid as long as that instance is alive.

    What im looking for is a helper class with an interface for Adding and Removing and a way of changing the data of an instance in a way that I can make sure that the index or what ever I use to find the instance, always is the same instance.

    Simply using an index to reference the instances does not work as it will break as an instance of the structure is removed.
    I can not make pointers, as there is no way of returning a pointer to a structure in c# as far as I know?
    Hash tables are out of the question as well as I need a pure array in order to pass the data to the gpu.

    Im not sure if I explained myself very well here, but hopefully someone will understand what I am looking for.
    Any ideas?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    So, this is about C#?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Oct 2006
    Location
    UK/Norway
    Posts
    485
    Yes, the program is written in C#
    Edit:
    Im so sorry, I though I was in the c# part of the forum when I posted this but that must have been in a different tab. Could you please move it?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Moved to the C# Programming forum.

    Quote Originally Posted by h3ro
    What im looking for is a helper class with an interface for Adding and Removing and a way of changing the data of an instance in a way that I can make sure that the index or what ever I use to find the instance, always is the same instance.
    I do not program in C#, but it seems to me that you just need to return a reference to the instance, which should mean just not returning a copy of the instance.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Yeah, something like a reference counter super(man)class would be useful.
    Devoted my life to programming...

  6. #6
    Registered User
    Join Date
    Oct 2006
    Location
    UK/Norway
    Posts
    485
    Laserlight: C# does not pass structures as references and I cant use unsafe code.
    Sipher: Im not sure if I understand, could you give a quick example or elaborate a bit further?
    Last edited by h3ro; 12-20-2010 at 09:07 AM.

  7. #7
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    You could use a base class, superclass, from with to derive all the others.
    That base class should include a reference counter variable as well as methods such as grab() and drop(). Each time you call grab, a pointer of that instance will be returned and the counter will be incremented. Each time you call drop the counter will be decremented. If the counter's value falls below one, the instance is destroyed.

    Edit: And it's Sipher...

    EditMore: I just noticed that my post may be irrelevant!...
    Last edited by GReaper; 12-20-2010 at 08:51 AM.
    Devoted my life to programming...

  8. #8
    Registered User
    Join Date
    Oct 2006
    Location
    UK/Norway
    Posts
    485
    That would mean that my structure would have to be a class and have more data in it, or am I missing something?
    The problem here is that I cant make the structure a class and I cant add any more data members to it, as the whole array is copied to the gpu.

  9. #9
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    OK...
    Devoted my life to programming...

  10. #10
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    I don't fully understand what you're asking. Let me suggest a solution, and you tell me why it wouldn't work.

    Assuming your structure was named Struct, I'd use a SortedDictionary<Struct, [anything]> to store your instances. I'd use SortedDictionary instead of Dictionary because: 1) it's easier to write a comparer than it is to write a good override of GetHashCode, and 2) you can adjust the comparer to sort items into the order that you need. To get the array you need out of it, you copy SortedDictionary.Keys to an array.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  11. #11
    Registered User
    Join Date
    Oct 2006
    Location
    UK/Norway
    Posts
    485
    That would work, but it means that I would have to copy the array very frequently, and the array could potentially be huge (100k instances) so its not an optimal solution. Thanks for the suggestion, unless there is any better alternatives I think I will go with it, as for now its probably worth swapping the loss in performance for ease of programming.

  12. #12
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    Yeah, you should probably ignore my first post. I'm still a bit groggy.

    What does it mean to "remove" an instance from your array? Do you have to shorten the array, copying all the instances into a new array? Similarly, do you have to grow your array to "add" an instance to it? If you're using an array, you probably shouldn't be adding or removing instances from it. That's a List<T>.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  13. #13
    Registered User
    Join Date
    Oct 2006
    Location
    UK/Norway
    Posts
    485
    The reason im using arrays is because thats the data structure the gpu allows me to copy into it. A linked list would be perfect for my needs, but there is no way of copy it to the gpu as far as I know. Removing means removing an element from the array, making the size one smaller by making a new array that is one smaller. I can sort of buffer the elements to void that. I tell the graphics card where to start the copy and where to end the copy.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 06:24 PM
  2. Replies: 5
    Last Post: 11-07-2005, 11:34 PM
  3. Keeping track of values without an array
    By m712 in forum C++ Programming
    Replies: 5
    Last Post: 11-07-2002, 10:49 AM
  4. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 08:37 AM
  5. Can't spot the problem...am i even on the right track?
    By iluvmyafboys in forum C++ Programming
    Replies: 8
    Last Post: 02-05-2002, 09:26 PM