Unmanaged memory – using malloc( )/free( )

The basic way to allocate memory for your computer program in C (and C++) is by using malloc(). malloc() designates a block of the computer system's memory for your program's use. Once your program is using a segment of memory, no other program can use or access that segment of memory. An attempt to access a segment of memory not allocated to your program will generate a "segmentation fault", and represents an illegal operation on most systems.

How to do it...

Let's look at an example code that allocates a pointer variable i, then assigns memory to it using malloc(). We allocate a single integer behind an int* pointer. After allocation, we store a value inside int, using the dereferencing operator *:

// ...

Get Unreal Engine 4 Scripting with C++ Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.