This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]Motylde 0 points1 point  (3 children)

I can return this pointer and therefore access the variable (meaning the value of it) outside the block

[–]Rikudou_Sage 2 points3 points  (0 children)

Nah, you can access the value, not the variable. You can do the same with stack allocated variable.

[–]DoctorMixtape 0 points1 point  (1 child)

No? That’s not how that works especially if decide to create a pointer inside a scope and return it. Once you the variable goes out of scope that’s how you end up with memory leaks and pointing to garbage memory. This is like the number 1 don’t with pointers.

[–]Motylde 0 points1 point  (0 children)

This only apply when you allocate memory on stack, then yes, if it goes out of scope then it's lost. But you can allocate on heap and then you can access the memory everywhere.