Pointers are the coolest thing yet in c++

  • (2 Pages)
  • +
  • 1
  • 2

26 Replies - 2953 Views - Last Post: 29 August 2017 - 01:28 AM

#1 jeffindenver  Icon User is offline

  • D.I.C Head

Reputation: 22
  • View blog
  • Posts: 114
  • Joined: 07-August 15

Pointers are the coolest thing yet in c++

Posted 24 October 2016 - 01:15 PM

The coolest thing I've seen in C++ so far is pointers. I love that you can use them to pass functions.

But they've also made me scream inside, "Why!!?" They make code harder to read, harder to reason about, and are mostly unnecessary (keep in mind I'm just writing tiny, insignificant assignments at the moment).

So what's the story with these? Are they primarily used in writing compilers and system software? What do you think about pointers?

Is This A Good Question/Topic? 0
  • +

Replies To: Pointers are the coolest thing yet in c++

#2 jimblumberg  Icon User is offline

  • member icon

Reputation: 5296
  • View blog
  • Posts: 16,505
  • Joined: 25-December 09

Re: Pointers are the coolest thing yet in c++

Posted 24 October 2016 - 02:20 PM

Quote

What do you think about pointers?

I think that pointers are dangerous but necessary evils that should be avoided whenever possible.

For a few of the reasons I feel this way, re-read your post.

Jim
Was This Post Helpful? 4
  • +
  • -

#3 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon


Reputation: 6966
  • View blog
  • Posts: 14,572
  • Joined: 16-October 07

Re: Pointers are the coolest thing yet in c++

Posted 24 October 2016 - 04:53 PM

C++ isn't quite a standalone language; it stands on the shoulders of C. While you can, and should, write most C++ code without worrying about this ancient legacy, it still lurks below, waiting to be confronted at inopportune moments.

One unavoidable C++ pointer usage is in polymorphism. Another is all those cool ADTs that the Standard Template Library allows you to generally avoid. However, as a student, you're usually expected to write your own.

Basically, you're stuck with them. Use only when necessary.
Was This Post Helpful? 2
  • +
  • -

#4 jeffindenver  Icon User is offline

  • D.I.C Head

Reputation: 22
  • View blog
  • Posts: 114
  • Joined: 07-August 15

Re: Pointers are the coolest thing yet in c++

Posted 24 October 2016 - 05:41 PM

My instructor told the class that we'll love using them. But I'm going to reserve judgment until I at least finish my first pointers assignment.

Took me roughly twenty minutes to figure out WTF was going on in 200 lines of head-scratching, pointer heavy code the instructor provided. Figuring it out gave me a high :bananaman: , if you know what I mean.
Was This Post Helpful? 0
  • +
  • -

#5 dandymcgee  Icon User is offline

  • New D.I.C Head

Reputation: 6
  • View blog
  • Posts: 12
  • Joined: 12-October 16

Re: Pointers are the coolest thing yet in c++

Posted 25 October 2016 - 10:55 AM

Pointers are extremely useful and completely necessary. They are *not* something you should "try to avoid as much as possible". They should be used according to the following logic:

Let's assume you have a data structure that stores vertex data for a 3D model. The model has 1,000,000 vertices. If each vertex is 4 floats, that's 4 floats * 4 bytes * 1,000,000 vertices = 16,000,000 bytes, or 16 MB.

Now let's assume you have function which needs this data to render the model to the screen. Somehow, you need to pass the data to that function. Without pointers (or some variation, e.g. handles or references), you have to copy 16 MEGABYTES of data in RAM before the function can do its work. Now you're using 32 MB of memory! If the render function calls any other utility functions that also need the vertices, you'll be copying the data yet again! Wow, what a waste!

The more logical thing to do is to store the data at a known location in RAM, and share it around. This location is called a "memory address", or often just "address". An address, in a 32-bit addressable memory scheme, is a 4-byte integer. Now, when I call a function that needs to have access to the vertex data, I can simply provide the address for that data, or 4 bytes. I've just saved myself the time and space required to copy 16 MB and now only have to copy 4 bytes! That's great!

Unfortunately, this isn't always the right approach. What if the function needs to *modify* the vertex data? Well, if you simply pass the address, it's going to modifying the same data that *every* other function uses! Sometimes that's desired, sometimes it's not. When you want to modify the data *and* keep the original, you have to fall back on making a copy of the entire data (or at least the parts the function needs to do its work). For this reason, the language can't assume that you 1) always want to copy the data or 2) never want to the copy the data. Instead, you need to tell it by 1) using a pass-by-value type or 2) using a pass-by-reference type *or* a pointer (pointers are passed by value, but you're passing *just* the value of the pointer, 4 bytes, rather than the value that is being pointed to).

For those who prefer a metaphor, imagine you want to share a YouTube video with a friend. You can either 1) download the video and bring it to your friend's house or 2) send him the URL. URLs or "uniform resource locators" are essentially pointers. They point to the IP address of a server which holds a bigger set of data; in this case, the video.
Was This Post Helpful? 1
  • +
  • -

#6 jimblumberg  Icon User is offline

  • member icon

Reputation: 5296
  • View blog
  • Posts: 16,505
  • Joined: 25-December 09

Re: Pointers are the coolest thing yet in c++

Posted 25 October 2016 - 11:05 AM

Quote

They are *not* something you should "try to avoid as much as possible". They should be used according to the following logic:

I'll have to disagree with you and IMO your own logic supports my view. Avoid pointers whenever possible, prefer references and const references. References are much less dangerous and much easier to understand than pointers, IMO.

Jim
Was This Post Helpful? 3
  • +
  • -

#7 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon


Reputation: 6966
  • View blog
  • Posts: 14,572
  • Joined: 16-October 07

Re: Pointers are the coolest thing yet in c++

Posted 25 October 2016 - 11:11 AM

View Postdandymcgee, on 25 October 2016 - 12:55 PM, said:

Without pointers (or some variation, e.g. handles or references), you have to copy 16 MEGABYTES of data in RAM before the function can do its work.


No. Without pointers you use std::vector or some other type safe container to pass data around. And, honestly, if your design relies on you passing a raw 16MB structure around, you're doing something profoundly wrong. That 16MB should be wrapped in a class and that function should be a member of that class.

However, you do offer a good example why avoiding pointers when possible is good advice. Believing pointers can be preferable leads to fundamentally bad C++ design choices.
Was This Post Helpful? 4
  • +
  • -

#8 dandymcgee  Icon User is offline

  • New D.I.C Head

Reputation: 6
  • View blog
  • Posts: 12
  • Joined: 12-October 16

Re: Pointers are the coolest thing yet in c++

Posted 25 October 2016 - 03:31 PM

View Postbaavgai, on 25 October 2016 - 11:11 AM, said:

No. Without pointers you use std::vector or some other type safe container to pass data around. And, honestly, if your design relies on you passing a raw 16MB structure around, you're doing something profoundly wrong. That 16MB should be wrapped in a class and that function should be a member of that class.

And how do you suppose std::vector works internally? It dynamically allocates a fixed-size memory pool, which expands unpredictably as you add more items to the collection. The std::vector instance itself is just a complex pointer wrapper.

You can read more about std::vector auto-growth here:
http://stackoverflow...-vectors-growth

I wouldn't recommend relying on the standard library's data structures or algorithms without first understanding how they're implemented, what they're good at, and what they're not. If we're going to blindly use these sorts of abstractions, we would be much better off using a safer language such as Java or C#.

If you don't believe what I say (and you shouldn't, I'm a random guy on an internet forum), listen instead to Chandler Carruth, an widely respected expert on this topic:
https://www.youtube....h?v=fHNmRkzxHWs
Was This Post Helpful? 0
  • +
  • -

#9 dandymcgee  Icon User is offline

  • New D.I.C Head

Reputation: 6
  • View blog
  • Posts: 12
  • Joined: 12-October 16

Re: Pointers are the coolest thing yet in c++

Posted 25 October 2016 - 03:39 PM

View Postjimblumberg, on 25 October 2016 - 11:05 AM, said:

I'll have to disagree with you and IMO your own logic supports my view. Avoid pointers whenever possible, prefer references and const references. References are much less dangerous and much easier to understand than pointers, IMO.

Jim

I must admit, I approached my answer from C perspective more so than a C++ perspective, where references simply do not exist. The title explicitly states C++, so I will apologize for not clarifying that sooner.

The short answer is when you need performance requirements that OOP C++ doesn't meet. There is a measurable amount of overhead involved in the conveniences C++ offers, which sometimes need to be circumvented for performance reasons. You can find loads of information about these sorts of cases on StackOverflow and around the web, written by people far more intelligent than me.

A few good starting places:
http://stackoverflow...hy-use-pointers

http://stackoverflow...e-object-itself
Was This Post Helpful? 0
  • +
  • -

#10 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 5822
  • View blog
  • Posts: 19,818
  • Joined: 05-May 12

Re: Pointers are the coolest thing yet in c++

Posted 25 October 2016 - 05:09 PM

You don't need to use function pointers in C++. People transitioning from C to C++ use them a lot. I was one of them until I learned about functors. After that point, I leaned towards using them over function pointers.
Was This Post Helpful? 1
  • +
  • -

#11 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 5822
  • View blog
  • Posts: 19,818
  • Joined: 05-May 12

Re: Pointers are the coolest thing yet in c++

Posted 25 October 2016 - 05:29 PM

As mentioned above, I came from a C background before going into C++. I was making that transition before the advent of STL. At the time, I was a big proponent of using pointers... even for places where references would have made more sense: "out" parameters of functions. To me, I wanted to make my APIs obvious that something will change if you pass it in. But that was also when IDEs were dumb.

Now, I advocate avoiding pointers. If you must use a pointer, use the smart pointer classes. The reason for the change of heart is because developer time is more expensive than CPU time. If I can make my code easier to understand and reason with, I am successful. I had personally done many hours of debugging issues which had a root cause of a bad pointer freeing, lack of freeing, or a bad cast. I had watched my colleagues do the same. It didn't matter if it was legacy code out brand new code. Later, when I was working with a team where the rule was "if you must use pointers, use smart pointers", the class of bugs we were spending time on changed. Of course, dandymcgee hit the nail on the head because the class of bugs we were running into were performance issues where parameters either not marked as const references, or that the move semantics in current versions of C++ was not available to us.
Was This Post Helpful? 0
  • +
  • -

#12 jon.kiparsky  Icon User is offline

  • Screw Trump (before he screws you)
  • member icon


Reputation: 10622
  • View blog
  • Posts: 18,174
  • Joined: 19-March 11

Re: Pointers are the coolest thing yet in c++

Posted 25 October 2016 - 07:22 PM

View Postdandymcgee, on 25 October 2016 - 05:31 PM, said:

And how do you suppose std::vector works internally?


Properly speaking, you shouldn't care.
Was This Post Helpful? 2
  • +
  • -

#13 jimblumberg  Icon User is offline

  • member icon

Reputation: 5296
  • View blog
  • Posts: 16,505
  • Joined: 25-December 09

Re: Pointers are the coolest thing yet in c++

Posted 26 October 2016 - 02:12 AM

Do you realize that both of the links you provided in post #9 are reinforcing the notion of avoiding pointer whenever necessary?

Quote

The short answer is when you need performance requirements that OOP C++ doesn't meet.

This is way too broad of an excuse for blindly using pointers. Until you positively determine where exactly the program is being bogged down by profiling, then analyzing the code to determine why the code is actually slow just jumping into C with classes type of programming is pre-optimization.

Yes some C++ concepts can be slow, especially when used incorrectly. For example passing objects by value instead of passing by reference/const reference can be a large source of problems. For example you should rarely pass a std::string by value since the copy inherent in passing by value can be significant. The same with std::vector, avoid passing by value, pass by reference or const reference. When dealing with vectors, if you know the size of the vector construct the vector either by specifying the size at construction or by reserving or sizing the vector as soon as you know the size. If you know the size of the container pre-allocating the memory will be much much faster. By the way a std::vector doesn't "expand unpredictably". How the vector "expands" is very predictable, but it can be slightly different from compiler to compiler. But each compiler is predictable because they need to meet complexity requirements provided in the standard.

Also you should prefer smart pointers if you are going to use pointers. Let the compiler help you manage the memory whenever possible.

Quote

You can find loads of information about these sorts of cases on StackOverflow and around the web, written by people far more intelligent than me.

Just because there are topics on a forum, even StackOverflow, doesn't mean you should blindly follow that advice, the people there are not necessarily more intelligent than the people here, you included. Also you should, IMO, be insuring that the data you're reading is actually still relevant. The C++ language is evolving, relying on information that is several years old may or may not be reliable.

Jim
Was This Post Helpful? 0
  • +
  • -

#14 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon


Reputation: 6966
  • View blog
  • Posts: 14,572
  • Joined: 16-October 07

Re: Pointers are the coolest thing yet in c++

Posted 26 October 2016 - 05:22 AM

View Postdandymcgee, on 25 October 2016 - 05:31 PM, said:

And how do you suppose std::vector works internally? It dynamically allocates a fixed-size memory pool, which expands unpredictably as you add more items to the collection. The std::vector instance itself is just a complex pointer wrapper.


Granted, std::vector almost certainly uses pointers. However, that's an implementation detail and not something I have to worry about. It might use some other wrappers that use pointers. Also, "expands unpredictably" is making assertions about implementation, which are not necessarily true.

Also, irrelevant. The point is avoiding pointers. By using a nice, well tested, wrapper class, you are doing just that.

View Postdandymcgee, on 25 October 2016 - 05:31 PM, said:

I wouldn't recommend relying on the standard library's data structures or algorithms without first understanding how they're implemented, what they're good at, and what they're not.


Fixed that for you. Rather than making assumptions, perhaps you want to review some fundamental OOP concepts? https://en.wikipedia...er_programming)
Was This Post Helpful? 0
  • +
  • -

#15 dandymcgee  Icon User is offline

  • New D.I.C Head

Reputation: 6
  • View blog
  • Posts: 12
  • Joined: 12-October 16

Re: Pointers are the coolest thing yet in c++

Posted 26 October 2016 - 05:44 AM

Thank you all for clarifying, you make good points. I think the OP (and readers) now have at least enough information to make the decision for themselves.

View Postjimblumberg, on 26 October 2016 - 02:12 AM, said:

Until you positively determine where exactly the program is being bogged down by profiling, then analyzing the code to determine why the code is actually slow [...]

I could never possibly disagree with this, and if the reader only takes one thing from this whole thread I hope it's: profile, profile, profile.

** Note: Below this line is opinion stated for the record. I make no proclamation of "bestness". **

View Postjimblumberg, on 26 October 2016 - 02:12 AM, said:

just jumping into C with classes type of programming is pre-optimization.

With the above said, I disagree with this. It's 100% a matter of opinion so I'm not telling you your way is wrong. I just prefer mine. I find it much easier to read, test, and maintain functional C++ code than OOP C++ code. I also have a fantastically easier time deducing exactly how the program will behave before running it. This saves me, personally, a ton of time and frustration that using a bunch of classes and containers without any consideration of necessity from the get-go would not. I almost certainly wouldn't recommend some of my habits to a new programmer, nor would I expect a team to blindly conform to them in a work environment, but it's what's work best for me so far. If you ask me again in 5-10 years, I may tell you something else.. who knows. Such is the nature of experience.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2