C/C++ pointers for memory savings/acceleration

Asked 2 years ago, Updated 2 years ago, 60 views

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.

c++ c pointer

2022-09-29 22:30

4 Answers

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.


2022-09-29 22:30

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:

  • I cannot read the most important pass the value as an argument for the function
  • If
  • Keep it in Pointer refers to securing dynamic memory with new or malloc, it is neither memory-saving nor fast.Rather, memory consumption increases by the pointer variable, and the processing speed decreases.
  • It may not always be "memory-saving" or "fast."A relatively small and simple structure (such as 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:

  • Basically, declare it as a normal type Cobj; (not a pointer or reference).
  • Declare objects as smart pointer types std::unique_ptr<C>ptr; or std::shared_ptr<C>ptr; if you want to dynamically secure objects in the C++ language.
  • If you want to use another variable alias in the C++ language, declare it as a reference type C&ref=obj;. (I don't think there are many scenes to use.)
  • If you cannot avoid C language or C++, you must use the pointer type C*ptr;.

If 」declaration が means type declaration for function parameters:


2022-09-29 22:30

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.


2022-09-29 22:30

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.


2022-09-29 22:30

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.