Help in using smart (double) pointers
Howdy I'm very new to Boost libraries, and this is my first try in using it, so pardon my naive questions. Now for the question: I'm trying to port a Java module to C++, as in a J2ME game to Windows Mobile. I've re-written all the classes that the game code depends on the J2ME packages using Windows Mobile's native methods. Now for the last difference between the two: memory. The game code has lots of new and obviously no deletes, since Java has automatic garbage collection. So for the C++ equivalent , I thought of going for smart pointers, so that I needn't bother about memory leaks and to call delete for all news. I've no problem in using smart pointers for normal dumb pointers. While it is the double pointers that I'm confused of. Say if I have: int **palette = new int* [5]; palette[0] = new int[10]; palette[1] = new int[7]; ... ... what is the boost equivalent that I can use, so that when all references to palette's are released, all memory allocated in the name of palette should be freed i.e. both the single (palette[0], etc.) and double pointer (**palette) should be freed. If yes, can you please show me how? Thanks! Sundaram
Hey Shmelev,
Amazing! That's exactly what I want! Stupid me, I didn't think of this at all. Instead I was just trying shared_array of shared_ptrs:
shared_array< shared_ptr<int>* > palette (new shared_ptr<int>* [5]);
It never occurred to me to use shared_array inside too :) Also, I learned the usage of reset in smart
pointers. Now it works like a charm. Once again, thanks a lot. Solves a lot of headache, phew!
Regards
Sundaram
________________________________
From: Roman Shmelev
int **palette = new int* [5]; palette[0] = new int[10]; palette[1] = new int[7];
Something like:
shared_array
participants (2)
-
Roman Shmelev
-
Sundaram