Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I have a very large vector as follows.
vector<vector<int>> a(100000, vector<int>(100000);
// do some work using a here
......
After the work is done, I use
vector<vector<int>>().swap(a);
in order to free the memory used by "a" immediately. I add
sleep(100)
after the swap and use "top" command to see whether memory usage is reduced immediately. But I observe that nothing has changed. What happens here?
To immediately clear vector out from the memory you should do :
v.clear();
v.shrink_to_fit();
It should work you might have error elsewhere in the code or you don't judge correctly whether the memory is freed.
You could make the deallocation more general - so it'll work on anything without performing any unnecessary operations in the swap. Swap consists of three moves. You can achieve the same result with a move and default reinitialization.
{auto x=std::move(a);a={};}
x steals a's memory and gets destroyed after leaving the scope. The a={}; isn't be necessary in most cases but according to standart after moving from object is allowed to be in a rather weird state.
Related
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 2 months ago.
Improve this question
I am trying to code a relatively simple program to understand in which scenarios it would be more efficient and useful to use the heap.
I first read that you better store large objects on the heap.
So I created a std::vector on the heap and filled it with an insane amount of bytes, something like 18Gb. At some point, it threw a std::bad_alloc exception, then my OS (Linux mint) killed the process after the swap was full. But the result was the same with the stack, so I don't understand how it's better on the heap.
Maybe I lack creativity, but I cannot think of a design where I would absolutely need to use the heap, I can always pass a reference of my object on the stack and achieve the same memory usage efficiency. For every program I wrote, it was always more efficient to use the stack speed wise and was the same for memory usage.
So, in what scenario is it useful to use the heap regarding memory usage efficiency or speed?
You use the heap whenever you need more flexibility than the stack provides. If you need to allocate memory to return something from a function, then it can't be on the stack, because it would be freed when the function returned. And it can't be global, because you might call the same function more than once.
In C++, you might not always realize that you are using the heap, because classes like std::vector and std::string use the heap internally. You don't always have to write new yourself to use the heap.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 11 months ago.
Improve this question
The stack class will maintain a dynamically allocated internal buffer of type unsigned long* which will be created in the constructor and is used to store the elements. This internal buffer must be wrapped in a smart pointer. The buffer is always increased or decreased in blocks and not with each element that comes in or goes out. The increment/decrement block size is passed as a parameter in the constructor.
If you want to learn how to use smart pointers then forget this code for a moment. Start a new small program that just makes sure you understand how they work.
Define a test class with just a string and an int in it. Make them public
Add a constructor so you can see when its created.
Add a Destructor so you can see when its deleted.
Create an instance using std::make_unique
Set things in the instance, write a function that accepts the unique_ptr as an argument and cout the fields in the test object
repeat with std::shared_ptr
Make sure you understand what is going on, dont just try to blindly nail stuff into your stack implementation. If you get stuck post a new question just on this.
The code above has so many other errors in it you will not get a clean set of answers to your specific 'smart pointers' question. Plus you wont be able to make it work reliably
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
So I was looking at the source code of unreal engine and I came across this:
class USphereComponent* CollectionSphere;
There are using pointer for something that is going to be initialized and spawned once when the game begins and some objects will be overlapping it,I thought pointers are mainly used for more mid-high memory allocation purposes
So why are they using it now?
Keep in mind Im not really good at pointers so Im just trying learn and gather some information about it.
Whether to use pointers or not has almost nothing to do with "low" or "mid-high memory allocation purposes".
You use a pointer when you need a pointer, and often that's when the pointee (the thing being pointed to) will live for longer than an "automatic" object would in any of the program's existing, conveniently-accessible scopes.
Basically, *CollectionSphere will be a global (or something similar), but we can't construct it right away. The author has decided that they need fine control over the lifetime of the object. So, they have created a pointer to point to that object when the time comes.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I would like to know if Weak Pointers can be used against/instead of Mutex/Critical Sections for synchronization issues.
If the only synchronisation issue you've got is ensuring the object's still around when you'd like to access it, then getting a shared_ptr from a weak_ptr will ensure its lifetime is extended. It's only sufficient for very narrow scenarios though, for example - if a shared_ptr is stored in a container somewhere, and may be erased at any time but is otherwise unused, while your code with the weak_ptr may want to actually access or modify the data content of the object.
If you also need to protect against concurrent accesses during updates to the data in the object, then you need more than that shared_ptr....
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
I need a temporary string to append and modify pre-existing strings so that i can use with DrawText. That temporary string needs to change inside a function so I have 2 options:
-Use std::string::clear()
-Initialize another temporary string.
I can use and understand both methods but I'm wondering, which one is better?
Edit: To the function in question, having low running-time is essential
Whichever one more clearly reflects the intent of the code is better. The one you would use if you didn't stop to think which was "better" is better.
If (and only if) profiling reveals a performance problem in your function then you might save tiny amounts of time by reusing an existing string.
The memory of the existing string is already allocated. No new memory allocation needs to be made unless the string exceeds the size of the memory allocation.
On the other hand, if you create and destroy a lot of strings, the allocation time can start to add up.
I have some code where std::string allocation and copying dominates the profile. To fix it sometime in the future, we're going to have to implement string pooling, custom allocators and use string_ref instead of string.
So yes, it can be a problem. But measure to find out before trying to fix it.