I have a question about the c++ pointer!

Asked 2 years ago, Updated 2 years ago, 143 views

Hello.

I got curious while implementing the stack with a linked list.

I implemented to release the memory of the node that has data that will be pop when pop operation is performed.

Does the pointer of the element just below the top have a NULL value because the memory of the next node has been released?

Or does the memory have been released, but do you keep the address value that you were pointing at?

If you still have the address value, I'd like to know how to get the pointer to point to NULL, other than direct substitution!

Thank you! :)

c++ pointer null stack

2022-09-22 18:20

1 Answers

Does the pointer of the element just below the top have a NULL value because the memory of the next node has been released? Or does the memory have been released, but do you keep the address value that you were pointing at?

First, the pointer is a variable. It has a value like other variables used in code, and it just means that the value is a location on memory. This means that the value of the pointer does not change when memory is drained. Unless you change it yourself, you'll keep that value.

If you still have the address value, I'd like to know how to get the pointer to point to NULL, other than direct substitution!

The pointer was previously called a variable, but you have to substitute it directly when you change the value of the variable. Then what kind of method do you want?

Or you can write top=nullptr in the c++ style.


2022-09-22 18:20

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.