Reforming my Ego

posted in noaktree leaves
Published February 05, 2005
Advertisement
I spent the time I had today reading Bjarne's website (thanks coder!). It definitely deserves a reread.

It has come to my attention that my hand writing has always been a mix of print and incursive - a sort of hybrid. This habit has also become a part of my C/C++ development style for some time. After reading what Bjarne has to say, and also receiving certain tips, I have decided that I should begin moving to a more standard C++ development style. So I guess this is the end of my love affair with C, it's been wonderful baby but hey we'll always have bit shifting.

I'll start reworking my core engine classes tomorrow. So I guess the material editor will have to go on hold for a couple of days. Seems like a cycle for me lately: Work on engine - Work on editor - Realize I can do something better so return to step 0. I feel this is a good process that will ultimately lead to something far more stable and satisfying.

Edit: I don't think I'm going to give up my C FILE* operations unless someone has a good reason why I should. Anyone?
Previous Entry Buggin Out!
Next Entry Changes
0 likes 5 comments

Comments

evolutional
Lose C FILE and get yourself PhysicsFS. It has similar syntax but can read to/from archives seamlessly and allows you to build a VFS with ease.
February 06, 2005 03:36 AM
Rob Loach
Sometimes the only thing you can do to get out of that endless loop is set the end goals, finish them one at a time and never turn back.
February 06, 2005 10:03 AM
noaktree
Quote:Lose C FILE and get yourself PhysicsFS.
Thanks evolutional. Your comments are always very helpful. However I plan to build my own archiving virtual file system at a later time. I was just wondering if C FILE has any drawbacks compared to C++ streams?
Quote:Sometimes the only thing you can do to get out of that endless loop is set the end goals, finish them one at a time and never turn back.
Thanks Rob. I agree. I am very good at completing projects. I just feel that this project is something special and for the first time I'm not afraid to go back and fix my mistakes. Even if it means getting stuck in an apparent infinite loop. [smile]
February 06, 2005 04:36 PM
Muhammad Haggag
Quote:I was just wondering if C FILE has any drawbacks compared to C++ streams?

There's 2 facets to this: Text and binary IO. For text IO, C++ streams beat the std C library IO hands-down. For binary, it depends.

The biggest thing to me is RAII - with C++, your files close themselves when out of scope. When an exception is thrown (I do use exceptions), they're automatically destroyed. Not true for C's FILEs.

Also, you always work with FILE pointers. I generally don't use pointers unless I have to, because the probability of messing something up gets higher here. Basically, I rarely use a naked pointer in C++. It's always an auto_ptr (single-ownership, copyable smart pointer), boost::scoped_ptr (single-ownership non-copyable), boost::shared_ptr (mult-ownership, copyable, reference counted), ...etc. Dereferencing a NULL smart pointer usually results in a fired assertion or a thrown exception, so you never get access violations anymore, and you track things easier.

Plus, I don't like fread and fwrite. They assume you're writing N elements, each of size S, and make each of these a parameter (under the hood they just multiply them to get the total size). That's really subjective, though.

In the end, you can really make FILE pointers safe by making them a smart pointer. Off the top of my head (meaning: Won't work, and will crash your system into oblivion):

class file_t
{
private:
    FILE* m_file;

public:
    // Default constructor, parameterized ones and whatnot
    ~file_t()
    {
        if(m_file)
        {
            fclose(m_file);
            m_file = 0;
        }
    }

     .... // operations

     // conversion operator
     operator FILE*()
     {
          assert(m_file);
          return m_file;
     }
};
February 06, 2005 05:11 PM
noaktree
Thanks, that's the reason I was looking for. I have been working with pointers for so long I seem to have lost my fear of them. Things do change however when my code starts getting very large and all of a sudden I have a memory leak.

I think I'll just try to make the FILE pointer safe for now.
February 06, 2005 09:13 PM
You must log in to join the conversation.
Don't have a GameDev.net account? Sign up!
Profile
Author
Advertisement
Advertisement