This is a rudimentary question.
I learned that C/C++ saves memory and speeds by securing and delivering non-simple models (such as int and double) with a pointer.
Should all self-made classes and structures be declared with a pointer?I look forward to hearing from you.
In C/C++, I learned that it is faster to save memory by securing and delivering non-simple types (such as int and double) with a pointer.
Where did you get the information? Exactly case by case.
Should all self-made classes and structures be declared with a pointer (smart pointer)?
Of course, there is no generalization on a case-by-case basis.If I have to say anything, you should only select a pointer when necessary.For example, you should not use a pointer when you do not want to share values between the caller and the caller.In addition, C++ languages offer references to replace pointers, which are often more appropriate.
In C/C++, I learned that it is faster to save memory by securing and delivering non-simple types (such as int and double) with a pointer.
This sentence is not recognized correctly for the following reasons:
new
or malloc
, it is neither memory-saving nor fast.Rather, memory consumption increases by the pointer variable, and the processing speed decreases.std::pair<int,int>
or std::complex<float>
) may also lose its pointer-through benefits.If you want to leave the original text behind and modify it, would it be like this:
In most cases functions, passing other types as pointers or references would save memory and speed when passing values as arguments for functions
is not a simple type (such as int or double)Should all self-made classes and structures be declared with a pointer (smart pointer)?
It is unclear what the "declaration" in this question refers to, but the short answer is "no."
If "declaration" means variable declaration of self-class or structural type C
:
Cobj;
(not a pointer or reference).std::unique_ptr<C>ptr;
or std::shared_ptr<C>ptr;
if you want to dynamically secure objects in the C++ language.C&ref=obj;
. (I don't think there are many scenes to use.)C*ptr;
.If 」declaration が means type declaration for function parameters:
C&
or pointer type C*
.If you can take a null pointer, you will choose a pointer type.std:unique_ptr<C>
, std::shared_ptr<C>
, std::weak_ptr<C>
) in type declarations for function parameters should be avoided unless you are familiar with them."GotW#91 Solution: Smart Pointer Parameters" or " F.7: Forginal us, Tumment; by Herb Sutterthere than smart pointers "
Secure with Pointer
I don't know what you mean, but
int*n = new int(2);
Or something like that? Dynamic acquisition itself is expensive, so we shouldn't use it unnecessarily (automatic variable area for what)
If you want to keep it dynamic, you should use a smart pointer (e.g., std::unique_ptr
) or container class (e.g., std::vector
) for C++, so the pointer does not appear
Hand it over with a pointer.
In C++, you should use references for most situations (there is a guarantee that it will not be null), so there is still no pointer.
Also, C/C++ commonality is that the pointer reference is also slightly costly, so it is a balance between the copy cost and the reference cost.
But remember, compiler optimization is the use of smart pointers, which prevents compiler optimization.
For example, if there is a program that frequently communicates over and over in one second, securing/releasing the buffer each time it communicates will result in a lot of memory fragmentation and performance degradation.
If you use it frequently like the above, I think it is one way to use the buffer that you have reserved in advance through static declarations.
On the other hand, if there is a screen class that is rarely used, there is no need to keep securing memory for that screen.
When designing a program, engineers must optimize it to match system resources.
I don't think you need to be so nervous except when you strictly manage your memory with RTOS.
While it is faster not to dynamically reserve memory when performance is given full priority, large apps are still case-by-case because if they do not release memory appropriately, resources will run out and they will be stuck.
In this area, the only way to do this is to discuss the size of the program and how often resources are used.
© 2024 OneMinuteCode. All rights reserved.