• entries
    383
  • comments
    1075
  • views
    352465

Confusion and C++

Sign in to follow this  
Trapper Zoid

58 views

Gah; all this C++ is making my head hurt. The more I read up about proper design techniques the more impossible it seems to actually do anything. Every time I think I have a solution to a problem there's some hidden gotcha that ruins everything.

My present concern is memory management. In my C styled programs I'm usually very strict about who has the power to create and delete memory in order to avoid leaks. However in a message passing system under C++ things get a little more complicated. I'd like to be able to pass pointers or references to data structures, such as strings or maybe even arrays, as arguments in my messages. But given the message could in theory last longer than the object that creates it, and it be passed to many different other objects, and I don't particularly want to have to make copies of everything if I don't need to, then deriving a memory leak proof system gets really complicated. Especially if I'm rusty on design and my C++ isn't that good.

Then there's the problem of how I can make a Message class general enough to take pointers to pretty much anything. I suppose I could derive a bazillion different message flavours from a base class, but then it gets trickier to use a memory pool of dead messages in order to save on memory reallocation. I'm sure there's a decent way I can have a general Message class that could take nearly anything (possibly using templates? I'm pretty naive on the proper uses of those), but I'm fighting against my C instincts of damning type safety to hell and bringing out the old "pointer to void" stand-by.

So far I've been reading up tips in Effective C++ and searching the internet on info on smart pointers, templates, garbage collection and various other techniques, but there's only so much I can take in before my brain has it's own memory leak and it all swims together. I'm now fighting the thought that maybe C++ isn't the best language for this kind of thing - maybe something with in-built garbage collection or possibly even functional would be better [smile]. Of course, improving my C++ is all part of the goals of this project.

Guess the therapeutic journal entry is now over; I've got more advanced C++ to learn.
Sign in to follow this  


4 Comments


Recommended Comments

I use the void* technique for passing my pointers in a message structure. Yes it's ugly but so is having a million different message types. I have one message class that is used in different ways for different messages and having void* is how I acheive that. Compile time type checking is overrated. Especially if you know you write perfect code :).

Share this comment


Link to comment
After having a break from all the C++ books I'm starting to lean that way myself. I got slightly brainwashed from all the "how to write perfect C++ code" techniques. Type safety should be easy to enforce if I'm the only one using my code [grin]

Share this comment


Link to comment
Yeah, I find myself doing this at times. It's a dirty way of getting things done, but like mentioned if no one is going to be toying with your code you shouldn't have very many problems.

Share this comment


Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now