explicit array of pointers

Started by
6 comments, last by snk_kid 18 years, 11 months ago
How would i make a new array of pointers? such as: m_pointerarray = new long*[8]; //<- probably wrong
-------------------------Unless specified otherwise, my questions pertain:Windows Platform (with the mindset to keep things multi-platform as possible)C++Visual Studio 2008OpenGL with SFML
Advertisement
Quote:Original post by EvilKnuckles666
such as:
m_pointerarray = new long*[8]; //<- probably wrong


Appears to be fine, just that you left the type of m_pointerarray so we can't say if that is wrong or wright, in any case if you need a fully dynamic array of pointers then:

long** foo = new long*[8];//....delete[] foo;or:#include <vector>std::vector<long*> v(8);



Although i can't see where that would be useful, better off with a vector of longs instead, then again i don't know exactly what your trying to do.
ok, say i'm using the long** lNum[8];
how would i delete them?

delete [] lNum; ?
-------------------------Unless specified otherwise, my questions pertain:Windows Platform (with the mindset to keep things multi-platform as possible)C++Visual Studio 2008OpenGL with SFML
Quote:Original post by EvilKnuckles666
ok, say i'm using the long** lNum[8];
how would i delete them?

delete [] lNum; ?


If you meant:

long** lNum[8];


That makes a statically allocated array, you don't manually delete those [smile], they are automatically destroyed & deallocated at the end of a scope in which they where declared in.

If you meant a dynamic array then yes, personally i suggest using std::vector it takes care of memory management plus a whole bunch of other things like automatically growing, or reserving a chunk of uninitialized memory, provide custom allocator types etc and it is as efficent as a C-style dynamic array.

Take note that calling delete[] or when std::vector is destroyed by scope they don't free the memory for individual pointers you have to do that you self or store smart pointers instead [smile].
i'm using a pointer to a pointer for a specific reason. how would i delete my array of pointers? i want that memory back after i allocated it
-------------------------Unless specified otherwise, my questions pertain:Windows Platform (with the mindset to keep things multi-platform as possible)C++Visual Studio 2008OpenGL with SFML
Quote:Original post by EvilKnuckles666
i'm using a pointer to a pointer for a specific reason. how would i delete my array of pointers?


If i understood you correctly i already said you had it wright in my last post [smile] unless you meant:


int main() {   long** foo  = new long*[2];	foo[0] = new long(10l);	foo[1] = new long(30l);	delete foo[0];	delete foo[1];   delete[] foo;}


i know its silly but you get the idea.
now with that, do i have to delete the "foo[0]" and "foo[1]" b4 i delete the whole thing? or it doesn't matter
-------------------------Unless specified otherwise, my questions pertain:Windows Platform (with the mindset to keep things multi-platform as possible)C++Visual Studio 2008OpenGL with SFML
Quote:Original post by EvilKnuckles666
now with that, do i have to delete the "foo[0]" and "foo[1]" b4 i delete the whole thing? or it doesn't matter


Yes it matters because if you deleted the array of pointers before, elements 0 & 1 technically no longer exist, those individual addresses are gone, either foo is null or refers to some random location in memory in both cases you've lost those individual addresses so you can not delete them thus cause memory leaks.

This topic is closed to new replies.

Advertisement