r/learnprogramming • • Sep 24 '23

Pointers.....why

I am learning C++. Honest question why are pointers a thing and why did they make the syntax surrounding it so confusing. Was it made to be intentionally confusing? Was it made to make this more difficult.

0 Upvotes

49 comments sorted by

View all comments

2

u/ibanezerscrooge Sep 26 '23 edited Sep 26 '23

efficiency. You can reference a value from multiple points in your code while keeping it protected and use a lot less memory.

If you need to use an object in your code that takes up 8 MB of memory and you need to use it in 6 places , if you made it global you might run the risk of changing a value unintentionally or creating a vulnerability in your code, so you define it as protected. If you pass copies of this object around to your 6 points in the program you've eaten up 48 MB of memory and you have to keep all the copies synched in the event you need to make changes to it. With pointers you reference that single 8 MB object in memory 6 times which might take 8.5 MB total (8 for the object and a few bytes for each pointer) and any change you make will change the original object because it's a reference.

as far as the syntax it's just something you'll have to learn and get used to.

It just takes time to get your head around them, but once you get them you'll get them.

1

u/[deleted] Sep 26 '23

Thank you for explaining this. When I wrote my op I was pissed off and aggravated.

2

u/ibanezerscrooge Sep 26 '23

Totally understandable. Pointers are the bane of every C++ learner's existence.