Skip to main content
GameDev.net gamedev.net
🔒 Locked

General pointer question

Started by MagTDK Jun 18, 2002 at 12:26 PM 7 replies 150+ views
Original Post
MagTDK
MagTDK
I have a single function that opens a file, then finds the length of it, then creates a pointer and allocates enough space before reading the file contents into it. The function then returns with the pointer holding the data to this file for further access outside of the function. The problem I have is when I''m done using this file in memory, were do I delete it so I don''t have a memory leak? Right now I''m deleting it outside the function when I''m done using it. Am I better off creating a pointer outside this function first and then sending the pointer to this function were the function will allocate enough space to hold the contents of this file? Either way I still have to delete it outside of the function. Also because I''m calling this function several times, I can''t have it as global. So what I''m asking is, what''s the best way to do this.
felonius
felonius
In general you should try to deallocate data in the same scope as you allocate it.

If you make a function that returns a new allocated piece of data then it becomes to easy for the caller to forget to deallocate it.

One nice solution it to encapsulate the allocation in an class:
(the following is pseudocode)


  
class File
{
FILE* f

File(string filename)
{
// open filename and set f

}

~File()
{
// close file and deallocate

}
};


If you now allocate an instance of Files on the stack it is automatically closed and cleaned up after use. Neat, isn''t it?
Jacob Marner, M.Sc.Console Programmer, Deadline Games
MagTDK
MagTDK
I like the class idea felonius, but if I were to do it using just a standard function, how does this look:


    
void CMyClass::Init (std::string file)
{

BYTE *pRaw = NULL;
loadfile (file, &pRaw);

// do what ever I have to do to file


delete [] pRaw; pRaw = NULL;

}

void CMyClass::loadfile (std::string file, BYTE **ppRaw)
{
using namespace std;

ifstream fin;
fin.open (file.c_str(), ios::in | ios::binary);

fin.seekg (0, ios::end);
length = fin.tellg();

*ppRaw = new BYTE[EndOfFile];

fin.read (reinterpret_cast<char *> (*ppRaw), length);
}



[edited by - MagTDK on June 18, 2002 3:06:35 PM]
felonius
felonius
If you do that then you should at the very least make loadfile private so unknowing users of the function don''t make memory leaks.

Even then, I don''t think that solution is very nice because two different parties allocate and deallocate. It it is too easy to forget and then introduce a memory leak.

An alternative you might be able to use is to pass a pointer to function to loadFile(). LoadFile could then load the file, call the provided function and then finally call delete on the data. In the implementation of the provided function you would do whatever you want with the file data. Do you follow?
Jacob Marner, M.Sc.Console Programmer, Deadline Games
MagTDK
MagTDK

Do you mean something like a wrapper function that only has access to the function allocating and deleting memory?
felonius
felonius
No, something like this:
(mind the syntax errors - its still pseudo)


  
typedef (*TMyFunc)(BYTE*);

loadfile(char* filename, TMyFunc* func)
{
// load file.

BYTE* b = new data;
func(b);
delete b;
}

dostuffwithfile(BYTE* b)
{
// do stuff

}

init()
{
loadfile("myfile", dostuffwithfile);
}


Now new and delete are in same function and you can do different things to the file depending on the function you pass to loadfile. Follow?
Jacob Marner, M.Sc.Console Programmer, Deadline Games
MagTDK
MagTDK

Never thought of doing it like that before. Looks alot messier then your class method, but I follow what your doing.

I think I''ll go with the class, it just looks more professional.

Thanks for your help felonius.
felonius
felonius
Sure thing. Have fun.
Jacob Marner, M.Sc.Console Programmer, Deadline Games
felonius
felonius
BTW, there is a third alternative. Instead of passing a pointer to a function you could simply use a virtual method for this. In loadfile you would call dostuff() directly, but distuff() would be a virtual function and derived classes could then override it depending on what you want to do with the file.
Jacob Marner, M.Sc.Console Programmer, Deadline Games

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.