Why would you use pointers for Low Memory Allocation? [closed] - c++

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.

Related

I wanted to use Smart Pointers, but not sure where to use and when to use [closed]

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

C++: Is it alright to keep creating new variables? [closed]

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 4 years ago.
Improve this question
I am new to C++ and I have a general question. In order to solve any question in the exercises of the book I am learning from, while I am able to successfully solve the questions, I usually end up creating a lot of new variables within functions in addition to the ones that I have already initialised. For some reason, this worries me because I feel that I am writing inefficient code that might hog resources if I follow this practice for more complex programmes. Am I wrong in thinking this way? Are there any best practices regarding initialising and declaring new variables?
EDIT: I forgot to add, before resolving any question, I tend to convert the solution into plain English and then attempt to draw the program structure.
Normally compilers do liveness analysis of variables during the compilation of your code. Variables are considered live only starting from their assignment till their last use - optimizing compilers are capable of reducing the amount of local storage on the stack that is required by sequentially used variables (sometimes they even can eliminate their use entirely or keep them in registers only for a short period of time).

What is the correct way to minimize data held on the stack? [closed]

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 4 years ago.
Improve this question
Preempt: (if anyone can link helpful articles explaining the stack and heap at a deep level, up to registers, would be much appreciated)
I am new to C++: I am trying to really grasp how memory management works. At this point, I understand any declaration of the type ObjClass obj; to have automatic duration within the scope it's declared in. Yet, ObjClass* obj = new ObjClass(); stores the obj pointer on stack, but assigns it an address of memory on the heap. What I'm wondering about is, in more complex programs, what design pattern is used to prevent stack overflow? I could see the storage on stack quickly exceeding 1mb. Is this achieved by making multiple smaller functions which run, use stack, and then automatically deallocate?
Related Q: As for global variables, I know they are held in "static" storage, yet am unsure how that works in the context of stack/heap. How is their memory allocated, and is there a small limit like the stack? Is heap close to the size of system RAM minus the OS-reserved memory?

What is the correct way of initializing a smart pointer member variable? [closed]

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 7 years ago.
Improve this question
I am currently learning up on smart pointers, and actually used them in my code as well. However, I wanted to reread the documentation on smart pointers and came across this line in MSDN (https://msdn.microsoft.com/de-de/library/hh279669.aspx):
// When initialization must be separate from declaration, e.g. class members,
// initialize with nullptr to make your programming intent explicit.
shared_ptr<Song> sp5(nullptr);
//Equivalent to: shared_ptr<Song> sp5;
right now I am using this in my header file:
shared_ptr<Song> sp5 = NULL;
I tried it a few time now but I couldn't really get the MSDN example to work without a bunch of errors popping up, but when trying to find out the problem there is not really much in terms of resource which even tell me to initialize smart pointers like this.
Is it really necessary to solve it like MSDN suggested?
No, it's not necessary. There's no "correct" way of initialising a smart pointer.
As the comment itself indicates, you may safely omit the initialiser.
For a initializing I would just write.
shared_ptr<Song> sp5;
and then later assign a new pointer or use
sp5.reset(new Song());
Inizializing it with
shared_ptr<Song> sp5(nullptr);
works to show that you want the sharded_ptr to be null in the beginning.
But i personally would suggest to you, that when you deal with null shared_ptrs to check if they are null before using them.
shared_ptr operator bool checks if the pointer is not null.
http://www.cplusplus.com/reference/memory/shared_ptr/operator%20bool/

API Memory: Heap or Stack? (C++) [closed]

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 9 years ago.
Improve this question
Context: I'm making a simple library. In it, I'm returning one of the classes I've created, which I could, a) declare on the stack and expect the user to copy to the heap if it needs to be on the heap, or, b) declare on the heap and expect the user to delete the object when they're done with it.
Which of these methods is a best practice, and if there isn't a clear winner, which should be used where? I'm thinking I'll create a .dll (.so, etc), but would the answer change if I were creating a different type of binary? What would happen if a user copied the headers and sources into their project and built the entire library each time they build their project. What would change?
Side note: I suppose a third option would be to allow a parameter to signify whether or not an object returned by a function is on the stack. This seems really convoluted, though, and I've never seen a library/API do that. A fourth option would be to allow a pointer to an already-existing object, which is then filled with new data. This way allows for the user to specify which sort of object they'd like, but it, again, seems rather convoluted.
Always prefer automatic memory management. Only use dynamic memory management if you need dynamic lifetime. If your function's purpose is to return a T, there is no need for dynamic lifetime here.
If you do need dynamic lifetime, then do not ever deal with raw pointers to objects that you need to delete- always use smart pointers.
Also,
expect the user to copy to the heap if it needs to be on the heap
You mean move to the heap, right?