Pointers Flashcards
(18 cards)
In memory, what is “The Stack”?
a section of memory where the current code is sitting on
what are the 3 ways to pass data into functions?
Pass by:
Value (no symbols)
Reference (& symbol)
Pointer (* symbol)
what happens when you call a function?
your program places 8 bytes (the function address) onto The Stack.
If the function has no params, then only those 8 bytes will be pushed onto The Stack and will free once the program is done. If there are params, then they are also pushed onto The Stack.
what happens when you pass in those 3 ways?
Pass By Value = the object is COPIED into The Stack, it’s not the original object
Pass By Reference/Pointer = 8 Bytes are placed, no matter the size of the object. You are passing in a memory address to that object.
Anything above 8 Bytes, best off using a Reference/Pointer
why pass in pointers and refs instead of value?
saves time and memory
anything larger than an FVector passed by const reference
what does the const modifier do?
it basically means, “this cannot be modified”
modifying a reference to something will modify the original data itself, sometimes we dont want this happen
Function(const int& variable) = “this variable cannot be modified, its constant”
why use references over pointers?
pointers can null (non existing) and a reference cannot be null (something has to exist to be referred too)
use a pointer for objects where being null is a potential state in the world.
always check to see if that object is null if (object != nullptr) { do stuff }
how do you pass in a UObject?
pass in by Pointer (99%) of the time, 1% by Reference if for good eason
what is some useful pointer knowledge?
can be null
have a memory address
can be changed to point at something else other than the previous pointed object
you cant point to a reference
some useful Reference knowledge?
can reference a pointer of the object in memory itself
has no address, thus cant assign a pointer to it
once you declare it, you can’t change it to reference something else
what do pointers give you?
speed
power
control
a living connection
how do you get the memory address for a pointer?
you use the & (address of) symbol
FVector = X = FVector (1, 1, 1);
FVector* Xptr = nullptr;
Xptr = &X;
Xptr now points to memory address of X.
what must you remember about pointers?
you must always check to see if that thing exists first - check for nullptrs
return out of the function for a “silent” fail case
how do you do access the data in a pointer?
you must dereference to access the data
Deference with * or ->
(using previous example…)
FVector NewVector = FVector::ZeroVector;
if (X)
NewVector = *X
or
if(X)
const float XValue = X->Value;
reason 1 for using a pointer?
gives you access to memory locations that miles away - you dont need to use 100x Get() sequences to grab something
e.g. GetGalaxy()->GetSolarSystem()->GetPlanet()->GetMainCharacter()->GetCurrentArmor().Durability; GetGalaxy()->GetSolarSystem()->GetPlanet()->GetMainCharacter()->GetCurrentArmor().Color; GetGalaxy()->GetSolarSystem()->GetPlanet()->GetMainCharacter()->GetCurrentArmor().Size;
INSTEAD
you can just use this once:
FArmorStruct* TheArmor = & GetGalaxy()->GetSolarSystem()->GetPlanet()->GetMainCharacter()->Armor
reason 2 for using a pointer?
you can access huge quantities of data at any time.
the pointer points the location in memory, that actual amount of memory involved could be massive
reason 3 for using a pointer?
it stays current and gives you a living connection to that object.
you dont need to duplicate any data, you have a direct connection to it.
whats the safe approach to passing in data?
pass by reference where possible, instead of pointers
pass UObjects by pointers 99& of the time