A Quick Look at Memory

I am working on completing the [Unreal Engine C++ Developer: Learn C++ and Make Video Games] Udemy course. One of the lessons goes over Memory allocation. Now, I am no expert in logical processes and memory allocation, so this section got me really confused.

My question is, how important is this knowledge. Is knowing how the computer allocates memory to store values really that important? Or can I go about my business without giving it much thought?

If it is important, why is that? What should I focus on?

Adding ScreenShot for reference

It’s not needed to know if you’re coding at a higher level of abstraction. Though I would say it certainly useful knowing.
What confused you specifically?

1 Like

What confused me the most was the endianness reference from the lecture. I was not at all familiar with it and granted, I did not do any research on it before posting this question.

After reading the Wikipedia reference from the Resources part of the lecture and moving on to further lectures, I feel like I got a better grasp of memory allocation.

What I have been struggling with a lot has been the “Pointers” or reference variables and such. I completely understand why they are useful/necessary, but the concept of them and how to use them gives me a headache from time to time.

All in all, I feel like I am in a much better place of understanding and knowledge now than I was before.

Thank you for answering my question.

If you expand on this I might be able to help.

Its honestly a matter of syntax and experience. For the reference variables, I may forget to add the & identifier and end up getting errors on the VS Code. For the pointers I may forget the * or the newly introduced ** in Building Escape.

Im currently going through the Escape Building project and in the first stages of that project we go over overloading a pointer - not sure if that is the correct use of those words, but here is the example

FString Log = TEXT("Hello World!");
FString* PrtLog = &Log;
UE_LOG(LogTemp, Warning, TEXT("%s"), **PrtLog);

I had a hard time at first understanding what was actually going on and how to manipulate variables/values in order to achieve the desired result/output.

Im certain that with practice and hours of debugging under my belt, this will all make more sense.

That’s not something different. It’s just a dereference and then dereference the returned value i.e.

**PrtLog

In that code is the same as

FString Text1 = *PrtLog;
const TCHAR* Text2 = *Text1;

But without the use of extra variables.

  • The first line creates an FString called Log
  • The next line creates a pointer to the variable declared on the line above.
    Sidenote: * as part of a declaration is pointer and & is a reference. As part of an expression * means dereference and & means address of
  • And then the ** is as explained above, the first dereferences the pointer to get the FString and then the next * uses FString's overloaded * operator which gets a pointer to the first element of the character array (these are referred to as “C-style strings”).

Honestly, I don’t think this is very practical example of their usage. So lets take a look at function parameters.

void LogMessage(FString Message);
//...
FString Message = TEXT("Some message");
LogMessage(Message);

Here LogMessage's parameter is taken by value this means that what LogMessage gets is a copy of Message; and assuming the definition of LogMessage lives up to the name, it doesn’t need a copy of it, it’s just going to write it out to wherever and that’s it i.e. it only wants to read its contents.

What happens behind the scenes when you make a copy of an FString is it’s going to allocate memory large enough to store that string and then copy its contents over, then at the end of the scope (end of the log function in this example) free it from memory. That’s a lot of unnecessary work when all it wanted to do was read its value.

So instead we should pass it by reference

void LogMessage(const FString& Message);

Now Message refers to the same object that the caller calls it with and not a copy of it. However this function shouldn’t need to modify Message so it should be passed by const reference.

1 Like

You didnt have to take the time to explain it to me, but you did, and for that, I applaud you.
I did not expect this level of attention when I signed up for this course. :clap:
Thank you for the detailed explanation and run through. It makes SO MUCH more sense after that explanation.

And this part I completely understand. It makes complete sense to pass references when possible instead of copying the entire functions/ variables and consume unnecessary processing power/memory. Thank you again for the explanation!

For small types (all primitive types like int and char), they should be passed by value as they are small enough to fit on registers and thus require no allocation.

https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#f16-for-in-parameters-pass-cheaply-copied-types-by-value-and-others-by-reference-to-const

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.