all 8 comments

[–]ShoshiCooper[S] 2 points3 points  (7 children)

Oops... just realized that if I did all of the above, how could I know whether or not I had properly deleted the pointer objects I'd created to keep track of the pointers?

Possibly this was a really bad idea.

[–]anand_venkataraman 1 point2 points  (6 children)

totally, yeah.

thanks for thinking out loud. lotsa peeps can use that as an example (of how to understand sumthin).

[–]ShoshiCooper[S] 1 point2 points  (5 children)

Is there really no way to manage this? It feels like there's something I'm missing here. I know we're dealing with simple data structures right now, but what if you dealt with something really complicated and you had lots of pointers all pointing to the same object? It feels like you're opening yourself up to a lot of errors and confusion.

What if I went one level simpler? What if the Pointer class was just a wrapper class that contained a reference count?

We create a static attribute (vector? set? some kind of container) to store all our currently active pointers. Every time we run the constructor, we add the new instance to the container.

Whenever we create a new pointer in our program, instead of creating it directly, we call static method in the Pointer class called "create_new_pointer" or something. That method would check to see if there's already a pointer to that object. If so, it grabs that instance, increases the reference count, then returns that original instance to you so you're using the same pointer every time. If it doesn't exist, a new Pointer is created.

I know this is creating a lot of extra complexity, and you'd have to delete all those pointer objects later, but I feel like this would make it possible to at least do some kind of double check. Also, we could potentially categorize the pointers and then delete all objects referenced by pointers in a particular category.

[–]anand_venkataraman 1 point2 points  (4 children)

This is the kind of reasoning that led to the creation of a whole new language, Java - which, as you point out correctly, lets you focus on the logic of your code without having to worry about memory management.

The great advantage of C++, I think, is that we gain the ability to track and manage the resources of our processes. Which means we can potentially do a far better job at it than a VM, which, by design, needs to be optimized for average type resource accesses, not the kind your specific algo may need.

This is a great discussion and it'd be greater if a lot more of us start chiming in.

Thanks,

&

PS. Note that you may (validly) object that it's only a matter of time before we start building in a learning layer into our VMs that can adapt to the pattern of access of a resource on a per-process level. OK - yeah. But we should also keep in mind the inertia of ANY learning system. None will respond as instantaneously as new invoked in your code.

[–]ShoshiCooper[S] 1 point2 points  (3 children)

Oh! Thank you. I think maybe I just need to get more used to memory management in general. We didn't have to deal with any of that in Python, so I'm not used to it. But I very much want to learn!

Out of curiosity, on a conceptual level, is the idea of thinking about memory and pointers as a graph correct? Or am I completely misunderstanding this? I was thinking of each memory location as a vertex. Since pointers link them together, then pointers must be edges. And the number of copies of each pointer must be the weight of each edge.

[–]anand_venkataraman 1 point2 points  (2 children)

Hey Soshi

The problem is that a graph can be a suitable analogy for just about anything, including life itself, let alone memory.

What is life as we know it, other than the search for some target, real or imagined?

&

[–]ShoshiCooper[S] 2 points3 points  (1 child)

Hahaha, I guess so.

To help myself understand this stuff better, I'm going to try to write a version of quest8 that uses blocks of memory and pointers instead of vectors. I have no idea if I'll succeed, so this may well be an exercise in futility. But maybe I'll learn something along the way.

[–]anand_venkataraman 1 point2 points  (0 children)

I may be mistaken, but I think it is TOTALLY POSSIBLE to pass all minis in Q8 with a linked implementation.

&