It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I have a strategy pattern, and would like to run it in a main loop in a game for example. The problem is there will be a memory leak If I'm not deleting the instance, and I also would like to use that instance somewhere else. How can I deal with memory allocation/deallocation in a strategy pattern.
CompressionContext *ctx = new CompressionContext();
//we could assume context is already set by preferences
ctx->setCompressionStrategy(new ZipCompressionStrategy());
//get a list of files
ctx->createArchive(fileList);
Use an std::shared_ptr<CompressionContextBase> instead of a CompressionContextBase* (i.e. a raw pointer).
Edit: This is just a suggestion, based on the information you provided, there may be other smart pointer implementations with different semantics, such as e.g. unique_ptr, which might be more suited. As #akappa suggests, you may want to read up on the topic more, to make a better decision -- again, based on the information in the question, you probably want a shared_ptr but there might be additional considerations you omitted.
Related
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
I have huge problems with making this code work properly.
http://pastebin.com/Mi6gj188
There's an output from example program on the bottom. It simply crashes and doesn't deliver proper results too. It seems that none of the overloaded operators work as it should
You didn't write a copy constructor, or use RAII. As a result, every time your vector object is copied (and it is, a lot, because you make no use of references!!) your internal data pointer is copied, sharing it amongst multiple objects (each of which will attempt to delete it on destruction) causing a horrible bug.
Your book tells you about the rule of three, which you should now go ahead and work on following.
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I can't quite seem to figure out why this doesn't work. I tried to make a queue of pointer types and it failed. I have a class Room, and I want to make a queue of pointers to Room.. so I did:
queue<*Room> bfsRooms;
this gave me the error:
`*' cannot appear in a constant-expression
Does this mean it is impossible to make a STL queue of pointers?
No it should be fine, you should really link the exact code and exact error so we can help you better.
That said a queue of pointers would look something like this:
Room r;
std::queue<Room*> rooms;
rooms.push(&r);
EDIT: it is worth noting that if these pointer own their objects they point to you should really encapsulate the pointer in a smart pointer, something like:
std::queue<std::unique_ptr<Room>> rooms;
This abide by the RAII principle and will automatically clean up resources.
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 11 years ago.
Similar to this question, what are the pro/cons to using const local variables like in this answer?
Personally, I like to use const for any declared object (I'm not sure the word "variable" applies) unless I'm actually going to modify it. It documents to the reader that the value of the object is always going to be whatever it was initialized to, which can make the code easier to follow and analyze.
(It can also help the compiler in some cases, but most compilers, when invoked in optimizing mode, are smart enough to notice that the object is never modified. And if you invoke the compiler in non-optimizing mode, you're telling it that you don't care much about performance.)
In fact, if I were going to design my own language, all declared objects would be const (read-only) by default unless you explicitly mark them as modifiable.
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 11 years ago.
Here's what I am trying to accomplish.
I want program1 to create a shared memory segment where I store various arrays.
Then, I want program2 to read in the arrays and modify them.
This sounds pretty simple, but for some reason, I cannot find a single example online that shows how this is done. Every example I have found uses a single program (e.g the initialize, read and write are both done by program1).
If somebody can provide an example here, I'm sure this would be hugely beneficial for pretty much everybody that wants to use IPC in C++.
Boost.Interprocess has a guide for the impatient.
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 12 years ago.
I'm trying to pick out my next hackery project. It'll likely be one of the following:
A sparse radix trie Implementation with extremely fast set operations
A really good soft heap implementation
A bloomier filter implementation
A collection of small financial algorithms, such as deriving total returns given a set of dividends and minimal information about them.
But I can't choose. So I thought I'd put my fate in the hands of my peers. Which of those four would you find most useful? Most interesting to work on? Which do you think is the most needed?
I didn't know what a bloomier (maybe Bloom?) filter is until reading your question. Sounds cool and useful.