When to use C ++ pointers in game development

I looked at a bunch of articles, and most tell the same story: don't use pointers unless you need to. Coming from a C # / Java background where everything is memory driven, I absolutely don't know when to use a pointer, except in the following situations:

  • dynamic memory (e.g. variable-sized arrays)
  • polymorphism

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

+4
source share
4 answers

"Do not use pointers, they are slow" does not make sense (at least not in C ++).

This is exactly the same as saying: "Do not use variables, they are 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 it later.

  • Or would you like to say: "Do not use raw pointers (i.e. type foo* )", which will require new and delete ?
    If so, this is good advice . Typically, you need smart pointers, such as shared_ptr or unique_ptr , to manipulate objects, not to process the original pointers. You do not need to use new and delete in most C ++ codes, although this may seem natural.
    But they are still pointers under the hood!

  • Or did you mean something else?

Adding

Thanks @ bames53 below for pointing this out:

If transferring a copy is an option (i.e. when you transfer a small amount of data, rather than a huge structure or array that can be larger than several registers, for example, of the order of ~ 16 bytes), do not use pointers (or references) to transfer data; copy a copy instead. This allows the compiler to better optimize this path.

+6
source

One main use of pointers (although references are generally better when possible) is the ability to pass an object without copying. If an object contains a lot of data, copying can be quite expensive and unnecessary. From a game development point of view, you can store pointers in a container like std::vector so you can manipulate objects in the game. For example, you might have a vector containing pointers to all the enemies in the game so that you can perform some “bulk procedure” on them. It might be worth taking a look at smart pointers as they can make your life a lot easier (they help a lot in preventing memory leaks).

+3
source

The idea is that you don’t want to have memory leaks and do not slow down the program while extracting memory, so if you pre-allocate the structures you need, you can use pointers to pass structures around, as this will be faster than copying structures .

I think you are just confused about why pointers can be considered bad, since there are many places to use them, you just need to think about why you are doing this.

+2
source

You do not need pointers for variable-sized arrays; you can (and usually should) use std::vector . You also don't need source pointers for polymorphism; virtual functions work with smart pointers such as unique_ptr .

In modern C ++, you rarely need pointers. Often they will be hidden inside resource ownership classes, such as vectors. To transfer objects without copying them, you can use links. Most of the other uses of pointers are covered by smart pointers. Very rarely, you will need a non-owned pointer, in which case the original pointers are fine.

You will learn how to use pointers correctly if you learn about what to use instead of pointers. So find out about RAII, links and smart_pointers.

+1
source

Source: https://habr.com/ru/post/1399512/


All Articles