Browse  Forum  Clubs  Chat  News 

Game Developers Club -> Tutorials -> [Basic] Using the Memory leak class from the memory leak tut

Tutorials Forum: [Basic] Using the Memory leak class from the memory leak tut

GhostManZero: [Basic] Using the Memory leak class from the memory leak tut - Jul 13th 2006, 9:05PM Link | Report
This is just a small example for how to use the memory leak protection system on my other tutorial, "[Advanced] Basic and helpfull memory leak protection on C".
I'll also be writing some source and commenting:

Example 1:

#include <stdlib.h>
#include <stdio.h>
#include "MemryContainer.h" //Include our class (You will have to write this file from the code on the other tutorial)
struct Vector3D //A sample structure for a 3D vector, basically 3 floats for each 3D vector component (X,Y and Z)
{
float x,y,z;
};

MemoryContainer<Vector3D> Vec3s; //We create a memorycontainer for the Vector3D's
int main(int argc, char **argv)
{
Vec3s.Allocate(20); //Allocate 20 Vector3D's
Vector3D *Vec3Ptr = Vec3s.GetPointer(); // [OPTIONAL] You can also just do the same without creating a variable, i'll show how in my next example.
for(int ElementID=0; ElementID < 20; ElementID ) //For each Element in the array do
{
//Give random numbers for each element
Vec3Ptr[ElementID].x = (float)(rand());
Vec3Ptr[ElementID].y = (float)(rand());
Vec3Ptr[ElementID].z = (float)(rand());
printf("Element %i's data is: (%f,%f,%f)\n",Vec3Ptr[ElementID].x,Vec3Ptr[ElementID].y,Vec3Ptr[ElementID].z); //Print the data on each element
};
//After this we can just stop the program, everything will be freed automatically, but for safe keeping we set the pointer to NULL now.
Vec3Ptr = NULL;
return 0;
};

Example 2:


#include <stdlib.h>
#include <stdio.h>
#include "MemryContainer.h" //Include our class (You will have to write this file from the code on the other tutorial)
struct Vector3D //A sample structure for a 3D vector, basically 3 floats for each 3D vector component (X,Y and Z)
{
float x,y,z;
};

MemoryContainer<Vector3D> Vec3s; //We create a memorycontainer for the Vector3D's
int main(int argc, char **argv)
{
Vec3s.Allocate(20); //Allocate 20 Vector3D's
//The next step might be a bit more confusing, but it works just fine as in the other example.
//By calling Vec3s.GetPointer()[<index>], since GetPointer() return a pointer and dynamic pointers can be arrays, its more than
//normal that you can use that pointer without storing it in a variable and calling its data as usually, so this is just a
//different yet a bit more confusing way
for(int ElementID=0; ElementID < 20; ElementID ) //For each Element in the array do
{
//Give random numbers for each element
Vec3s.GetPointer()[ElementID].x = (float)(rand());
Vec3s.GetPointer()[ElementID].y = (float)(rand());
Vec3s.GetPointer()[ElementID].z = (float)(rand());
printf("Element %i's data is: (%f,%f,%f)\n",Vec3s.GetPointer()[ElementID].x,Vec3s.GetPointer()[ElementID].y,Vec3s.GetPointer()[ElementID].z); //Print the data on each element
};
//After this we can just stop the program
return 0;
};
Page: