Sign in to follow this  
BloodLust666

explicit array of pointers

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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].

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this