- Unreal Engine 4.x Scripting with C++ Cookbook
- John P. Doran William Sherif Stephen Whittle
- 150字
- 2025-02-28 12:28:30
How to do it...
Let's look at some 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 *:
// CREATING AND ALLOCATING MEMORY FOR AN INT VARIABLE i
// Declare a pointer variable i
int * i;
// Allocates system memory
i = ( int* )malloc( sizeof( int ) );
// Assign the value 0 into variable i
*i = 0;
// Use the variable i, ensuring to
// use dereferencing operator * during use
printf( "i contains %d", *i );
// RELEASING MEMORY OCCUPIED BY i TO THE SYSTEM
// When we're done using i, we free the memory
// allocated for it back to the system.
free( i );
// Set the pointer's reference to address 0
i = 0;