Take the 2-minute tour ×

I have a tree and I want to release the allocated memory, but I face a problem that a pointer may refers to a variable that isn't dynamically allocated,so how to know wether this pointer refers to dynamic a variable or not

share|improve this question
In general, you can't. Don't implement data structures like that. –  anon Mar 10 '10 at 11:51
Seriously? you created a tree of just pointers without any extra information on what they point to? Why couldn't you just create the tree where each node points to a structure containing extra information (including a pointer to whatever it is you're pointing to in the first place) that way you could add a flag to note "dynamica variable" and maybe the size of it as well? –  James Barrass Mar 10 '10 at 11:59
add comment

1 Answer

This is compiler-specific. You may compare given pointer with pointer to a local variable. Result interpretation depends on the way compiler implements heap and stack. Generally, for given compiler, stack pointer is always less (or greater) than heap pointer. In any case, THIS IS BAD DESIGN. This may not work if pointer belongs to another heap (for example, allocated in another Dll).

share|improve this answer
add comment

Your Answer


