Click here to Skip to main content
15,889,335 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I've learned the pointers in C++ but I have a question. If I have this code:

int * a = new int;

then program create new integer, which takes 4 bytes, but pointer takes 8 bytes. Also I can use delete:

delete *a;

and I will clean the memory cell, but pointer but the pointer will reman. Then what kind of memory savings are you talking about when using pointers?

What I have tried:

Test in VS2017. And I can't relise how does it work.
Posted
Updated 3-Jan-21 4:29am

Pointers have their usages. One of these, is passing relatively large objects to functions.
Consider, for instance
C++
struct Point
{
  double x,y,z;
};

double distance_byval(Point pa, Point pb);
double distance_byref(Point * ppa, Point * ppb);


At every call, 2*sizeof(Point) (namely 48) bytes are copied form the caller code to the distance_byval function.

On the other hand just the two pointers (that is 16 bytes on a 64-bit machine) are copied from the caller to the distance_byref function.

Note a somehow nicer formulation of the distance_byref would be
C++
double distance_byref(Point & pa, Point & pb);
Anyway that wouldn't change my point (pardon the pun).

[update]
Fixed a bug thanks to KasternK.
[/update]
 
Share this answer
 
v2
Comments
Patrice T 3-Jan-21 10:21am    
+5 too
KarstenK 3-Jan-21 13:25pm    
Shouldnt Point ppb also be a pointer?
CPallini 3-Jan-21 14:02pm    
Yes, of course. Thank you very much for pointing it out.
The positive effect of memory saving with pointers comes into place when you have big chunks of data like arrays or objects which are handled in different functions and so every call would leed to a "new copy". This can also achieved with the reference syntax in C.

For further insights read the Pointers Usage in C++: Beginners to Advanced.

My personal experience is that "clean code" is the best code, because avoiding errors and maintainance are most important. So I use pointers only for big data problems because memory and speed are very rarely the issue, but getting the code shipped. ;-)
 
Share this answer
 
Comments
temchik_ggg 3-Jan-21 5:31am    
So using pointers for point on int, str etc... Is a more useless than really necessary? And using pointers for dynimic arrrays is more useful? Anyway thx for answer.
Stefan_Lang 3-Jan-21 11:40am    
You use pointers either for large objects as described above, or in cases where you want to allow another function to modify a local variable, even if it's only a char or short value.

As for dynamic arrays, you cannot define one without a pointer, because without it you cannot access it's contents!
CPallini 3-Jan-21 14:05pm    
My 5.
"reference syntax in C".
I suppose you meant: "reference syntax in C++".
In addition to what was said about avoiding errors when using C++, take a look at:
Addresssanitizer[^]
Another option would be to use a more memory safe language like Go (Golang), see: statically-typed-compiled-memory-safe-programming-languages[^]
 
Share this answer
 
v2
Quote:
Pointers in C++. What kind of memory savings are you talking about when using pointers?

Memory saving is just not the purpose of pointers.
Pointers allow many things like handling of arrays, dynamic array allocation at runtime, passing arrays as parameter to a routine ... , all things that would be nearly impossible without pointers.
C++ Pointers[^]
Pointers - C++ Tutorials[^]
C++ Pointers - Tutorialspoint[^]
Pointers in C/C++ with Examples - GeeksforGeeks[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900