Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When to use C++ pointers in game development

Tags:

c++

pointers

I've looked at a bunch of articles, and most tell the same story: don't use pointers unless you have to. Coming from a C#/Java background where memory is all managed, I have absolutely no idea when it's appropriate to use a pointer, except for these situations:

  • dynamic memory (like variable-size arrays)
  • polymorphism

When else would I use pointers, especially in the context of gamedev?

like image 296
jmegaffin Avatar asked Dec 09 '22 02:12

jmegaffin


1 Answers

"Don't use pointers, they're slow" doesn't make sense (at least not in C++).

It's exactly like saying, "Don't use variables, they're slow".

  • Did you mean, "Don't use dynamic memory allocation"?
    If so: I don't think you should worry about it right now. Write the code first, then optimize later.

  • Or did you mean to say, "Don't use raw pointers (i.e. of type foo*)", which would require new and delete?
    If so, that is good advice: You generally want smart pointers instead, like shared_ptr or unique_ptr, to manage objects, instead of dealing with raw pointers. You shouldn't need to use new and delete in most C++ code, even though that might seem natural.
    But they're still pointers under the hood!

  • Or did you mean something else?

Addendum

Thanks to the @bames53 below for pointing this out:

If passing a copy is an option (i.e. when you're passing small amounts of data, not a huge structure or an array that could be larger than a few registers, e.g. on the order of ~16 bytes), do not use pointers (or references) to pass data; pass by copy instead. It allows the compiler to optimize better that way.

like image 195
user541686 Avatar answered Dec 14 '22 22:12

user541686