What do YOU use pointers for?

Started by
29 comments, last by Kaezin 20 years, 5 months ago
I''m not exactly sure as to when I should use pointers, so I''ll get straight to the point: I have a class for handling my map so it will probably be holding a lot of variables. Would there be any point to using a global pointer to my general map class or should I just make it the old fashioned way? In other words, should I do
CMap *g_Map; 
or
CMap g_Map; 
Thanks in advance.
Half the people you know are below average.Trogdor the Burninator
Advertisement
Dynamic Memory & Linked Lists
The sentence below is true.The sentence above is false.And by the way, this sentence only exists when you are reading it.
I use pointers for pretty much anything.
I use pointers to get a handle on memory

PROGAMERS









I don't have to do nothing but live and die, everything else i choose to do- I don't know, but my dad says it alot
I use pointers to get a handle on memory

the second one
I use pointers to get a handle on memory

the second one

PROGAMERS









I don't have to do nothing but live and die, everything else i choose to do- I don't know, but my dad says it alot
When it comes to global objects, you should use a pointer to the object if you want to control exactly when that object is created.

If the object is statically allocated (as in, not a pointer), then you can''t control whether that thing is created first, or last, or in the middle. If the object is dynamically allocated (as in, using a pointer), then you obviously control when it gets created because you have to create it yourself.
If C_Map is large, you''ll want to have it on the heap, it would blow the (32k ?) stack.

Wizza Wuzza?
with vc6 the stack is 1MB, with mingw it''s 2MB

My Site
CMap g_Map would be fine if either...

g_Map is outside the scope of a function.

or g_Map is made static.

example

// map.hclass CMap { ...... };extern CMap g_Map;


// map.cppCMap g_Map;



or

// map.hclass CMap { ...... };extern CMap* GetMap();


// map.cppCMap* GetMap(){    static CMap g_Map;    return &g_Map;}


in both case, g_Map will be on the heap, not on the stack.

However, as so rightly said above, it is better to have it as a pointer, to control the time it gets created. Basically, the global variables are created at program startup, but in no specific order, which could cause problems.

If you use the example2, the static variable will be allocated at startup (I think), but constructed when you first call GetMap(). So you ''kind'' of are able to control the time g_Map gets created.

But to be on the safe side, have something like

// map.hclass CMap { ...... };extern CMap* InitialiseMap();extern CMap* GetMap();extern CMap* ShutdownMap();


// map.cppCMap* g_pMap = NULL;CMap* InitialiseMap(){    Assert(!g_pMap, "Map not shutdown.");    g_pMap = new CMap();    return g_pMap;}extern CMap* GetMap(){    Assert(g_pMap, "Map not initialised.");    return g_pMap;}extern CMap* ShutdownMap(){   Assert(g_pMap, "Map not initialised.");   delete g_pMap;   g_pMap = NULL;}


and you initialise.shutdown all your instances at your program init in one felt swoop, and in order.

even better, if using C++, have instanciation classes

class CMap{public:    static void  CreateInstance();    static CMap* GetInstance();    static void  DestroyInstance();    Vector GetVertex(int i, int j) { ... }    .....    .....    .....private:     CMap();    ~CMap();    static CMap* g_pMap;};

Everything is better with Metal.

This topic is closed to new replies.

Advertisement