• Advertisement
Sign in to follow this  

Double ** pointer parameter?

This topic is 3387 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi everyone, i'm having some misunderstanding in the concept of double pointers as a parameter, as what i'd tried to achieve is to pass in a ** pointer as a parameter and store the value in a recursive call until the value reaches to a limit. i'd been running into the debugging and during the recursive calls, everything stores as it seems, however when it is quitting the loop, my pointers all gone into corruption and memory has been released, i'm wondering how the pointers gone. As it seems to be vry much different from a single * pointer parameter. Could anyone explain the mechanism of the structure? here's my snippet of code : void funcA(int ** ppT = NULL, int * pS = NULL){ int ** ppR = new int * [*pS]; for(int i = 0; i != *pS; i++) ppR = ppT; if(ppT != 0) delete [] ppT; ppT = new int * [*pS + 1]; // copy new buffer for(i = 0; i != (* pS); i++) ppT = ppR; ppT[* pS] = pS; (*pS)++; // store value of pS into ppT and stop at 3 if(*pS != 3) funcA(ppT, pS); } int main(){ int num = 0; int ** ppNum = NULL; // call funcA funcA(ppNum, &num); cout << ** ppNum << endl; } Thanks in advance, Rgrds, Daniel.

Share this post


Link to post
Share on other sites
Advertisement
Dear god that's bad code. Have you considered giving your variables meaningful names? I'm sure it would make it a lot easier to follow your logic. Additionally, I'd suggest using pointers a heck of a lot less. For instance, there you seem to be using them to grow a dynamic array. What are you trying to accomplish that std::vector doesn't offer you?

Share this post


Link to post
Share on other sites
You made ppR point to ppT. Naturally ppT is an array of empty pointers. You then DESTROY the data that ppT points to. ppR turns into an array of invalid references. You then reallocate ppT to point to ppR which doesn't point to anything anymore. In the end neither point to anything.

Edited for clarity.

Share this post


Link to post
Share on other sites
Quote:
Original post by kittycat768
You made ppR point to ppT.


No, he made the elements of ppR equal to the elements of ppT.
Quote:
Naturally ppT is an array of empty pointers. You then DESTROY the data that ppT points to. ppR turns into an array of invalid references. You then reallocate ppT to point to ppR which doesn't point to anything anymore. In the end neither point to anything.


All of that is incorrect for similar reasons.

Share this post


Link to post
Share on other sites
Quote:
Original post by kittycat768
You made ppR point to ppT. Naturally ppT is an array of empty pointers. You then DESTROY the data that ppT points to. ppR turns into an array of invalid references. You then reallocate ppT to point to ppR which doesn't point to anything anymore. In the end neither point to anything.

Edited for clarity.


Thanks for replying ;D,
but No, i'm sure ppT destroy doesn't affect ppR and reallocating ppT to ppR points to something. It only seems that after quitting the recursive call only then the ppT is destroyed and things corrupted, where my problem occurs.

Share this post


Link to post
Share on other sites
Quote:
Original post by kittycat768
You made ppR point to ppT. Naturally ppT is an array of empty pointers. You then DESTROY the data that ppT points to. ppR turns into an array of invalid references. You then reallocate ppT to point to ppR which doesn't point to anything anymore. In the end neither point to anything.

Edited for clarity.
No he doesn't. He copies ppT into ppR, so when he destroys ppT, ppR is still valid.

I'd say the problem is because you are double dereferencing ppNum in main(). You never allocate memory for the 1-D array (the int* inner array), but you try to dereference it. If you could edit your post and put your code in [source][/source] tags, it would help a lot.

[edit]

ninja'd++;

Share this post


Link to post
Share on other sites
Quote:
Original post by Sneftel
Dear god that's bad code. Have you considered giving your variables meaningful names? I'm sure it would make it a lot easier to follow your logic. Additionally, I'd suggest using pointers a heck of a lot less. For instance, there you seem to be using them to grow a dynamic array. What are you trying to accomplish that std::vector doesn't offer you?


Thanks moderator,
I'm doing this for some purpose,
and this is just a simplified testing demo i'd made for debugging use,
so don mind bout the abbreviated form and meaningless names.

Share this post


Link to post
Share on other sites
Quote:
Original post by Sneftel
Dear god that's bad code.


This. You dereference several possibly NULL (hell, NULL by default) pointers without any sanity checks, leak the memory allocated for ppR every call, have magic numbers (Why is 3 the stopping point? What if *pS == 4 when the function is first called?), and have terribly hard to read variable names.

If I can follow your code, it looks like you are trying to add the numbers from *pS to 3 to the back of the array pointed at by ppT. There's no need to do this recursively, and no need to use all these pointers (even if you didn't want to use a std::vector, which would be the far more appropriate choice).

Share this post


Link to post
Share on other sites
Quote:
Original post by MikeTacular
Quote:
Original post by kittycat768
You made ppR point to ppT. Naturally ppT is an array of empty pointers. You then DESTROY the data that ppT points to. ppR turns into an array of invalid references. You then reallocate ppT to point to ppR which doesn't point to anything anymore. In the end neither point to anything.

Edited for clarity.
No he doesn't. He copies ppT into ppR, so when he destroys ppT, ppR is still valid.

I'd say the problem is because you are double dereferencing ppNum in main(). You never allocate memory for the 1-D array (the int* inner array), but you try to dereference it.

[edit]

ninja'd++;


yah mike, thanks it's what i'd come into mind as well,
but how'm i going to make the inner array exists after quitting the calls?
i'm guessing the inner one is declared as temporary and that's the reason why it ended up corrupting.

but my goal is trying to make this thing all happened within the call without out-of-scope manipulation of the size and declaration, question is, possible?

Share this post


Link to post
Share on other sites
Quote:
Original post by Driv3MeFar
Quote:
Original post by Sneftel
Dear god that's bad code.


This. You dereference several possibly NULL (hell, NULL by default) pointers without any sanity checks, leak the memory allocated for ppR every call, have magic numbers (Why is 3 the stopping point? What if *pS == 4 when the function is first called?), and have terribly hard to read variable names.

If I can follow your code, it looks like you are trying to add the numbers from *pS to 3 to the back of the array pointed at by ppT. There's no need to do this recursively, and no need to use all these pointers (even if you didn't want to use a std::vector, which would be the far more appropriate choice).


Thanks for advice,
as i had claimed,
i'm doing this for some purpose,
don't ask about why and don't concern about the magic number,
it corrupts when *pS == 4 when function is first called,
those are not of my concern,
as i had claimed,
i'm simplifying this all and just try to figure out what happens with the double ** pointer,
the magic number is not of my concern.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement