IT/Software career thread: Invert binary trees for dollars.

ShakyJake

<Donor>
7,624
19,249
But whatever was popped is still a thing in memory. Why would you not be able to reference it?
A copy of the element is created locally within the pop() method. The pointer to the element will be deleted. You cannot return a reference to a locally scoped variable.
 

Asshat wormie

2023 Asshat Award Winner
<Gold Donor>
16,820
30,963
A copy of the element is created locally within the pop() method. The pointer to the element will be deleted. You cannot return a reference to a locally scoped variable.
But why copy in the first place?
 

Deathwing

<Bronze Donator>
16,384
7,385
The standard library does call the element's desctructor when popping but that doesn't mean your implementation has to.
 

Noodleface

A Mod Real Quick
37,961
14,508
A stack is just a pseudo container in this case. In a real system the item would be popped and stored elsewhere by hand. For instance, because I used to write assembly, we used the stack to hold register values since we only had A and B to hold current data in. If you lost track of it, it would be "gone".

Without seeing his code I assume that the pop method is deleting the item from the stack and returning the reference to where it's located so you can manipulate/use it as needed.

Think of a Pringles can. If you pop the top chip off the stack it's just sitting there. It didn't get deleted or.have it's memory allocated over or zeroed out. Returning the pointer to it just says I've popped this item and removed it from the stack and here it is, do what you want if anything to it.

Another reason for returning a pointer would be in the case where you aren't returning a simple data type like an int or book; perhaps what's on your stack is a bunch of messages whose class contains 100 public/private variables.

Perhaps what is confusing you is how it's stored in memory. The stack is referencing objects in memory inherently. Meaning the first item is from address 0x250C, the next is 0x35FF, etc. They aren't sitting contiguously in memory inside of the stack. The stack is a pseudo container that is just referencing memory location in the system. It's just a convenient way for you, the programmer, to keep it all together.
 

alavaz

Trakanon Raider
2,001
713
You're deleting a pointer from your stack, but not the data in memory. So you can still return a pointer to that data.

It's kind of like tearing out an index page of a book. You no longer have the page that tells you where a specific chapter is, but you still have the chapter in the book and if you wanted to, you could set that index page aside as well somewhere outside of the book.
 

Deathwing

<Bronze Donator>
16,384
7,385
I would be careful about using the word "deleting" in this context as it can be very confusing. Note that standard pop implementations don't return anything and do remove the element from memory via the delete keyword or a destructor.
 

alavaz

Trakanon Raider
2,001
713
I used the word delete because he did.

I also re-read and see where the confusion is. It's able to return the reference value because of the const keyword which is a "feature" of c++.

GotW #88: A Candidate For the “Most Important const”

As to why you would do this, I think that there can be value in having a reference to the last item popped off of the stack, though I suppose the easier way is just to copy top() and then pop(). I've seen much more esoteric shit in college though. Some professors love going down the (void**)->&x**** hole just because they can.
 
Last edited:

ShakyJake

<Donor>
7,624
19,249
So let's back up a step.

From a simple main() method say you do this:

Code:
int* myArr = new int[10];
int myInt = 100;
    
myArr[0] = myInt;

Is there now a reference to 'myInt' at myArr[0]?
 

alavaz

Trakanon Raider
2,001
713
I don't think you can just assign a value to a pointer like that. You would have to do:

Code:
myArr[0] = &myInt

edit: I also don't think initializing a pointer array with new int[10] would work either. Usually you would just say int* myArr[10] and then you could add pointers to other variables by assigning them with the & i.e. myArr[0] = &myInt.

In C you can get into some crazy pointer shit because you have the actual value. I.e. int i = 10, the pointer (or address in memory to that value) i.e. int *p = &i and then from there you can point to the value of a pointer or even the address in memory to a pointer (and so on and so on).
 
Last edited:

Deathwing

<Bronze Donator>
16,384
7,385
I think you can, just not a great idea. The OS will likely get mad at you for trying to reference memory location 100.
 

Asshat wormie

2023 Asshat Award Winner
<Gold Donor>
16,820
30,963
That's just going to store 100 into first position in the array. myArr[0] is a pointer to the first position of the memory block represented by myArr.

Jake what you want to think about is this

IMG_20181013_120100.jpg
 

alavaz

Trakanon Raider
2,001
713
I think you can, just not a great idea. The OS will likely get mad at you for trying to reference memory location 100.

True, the delineation between can and should are huge in C. It also varies by compiler.
 
  • 1Like
Reactions: 1 user

Asshat wormie

2023 Asshat Award Winner
<Gold Donor>
16,820
30,963
It's a data structures class. Using vector defeats the whole purpose of learning data structures. Also it's probably C with classes type of lessons which is epic gay.
 

Asshat wormie

2023 Asshat Award Winner
<Gold Donor>
16,820
30,963
Probably because they are taught old trash by Professors who learned to program on fucking punch cards. If you ain't jamming 14/17, you ain't jamming.
 
  • 1Like
Reactions: 1 user

Kharzette

Watcher of Overs
4,921
3,568
I usually write mine as C with a cpp extension, but I do like const. C has const though right? Can't remember.

The second place I worked was really into doing stuff like const someStruct * const. You could pass it a pointer to someStruct in memory and know that the function wasn't going to mess with the pointer itself or the contents. I think. It's been awhile.

C# doesn't have const other than lame const variables which are just sort of like #defines in C.
 

Asshat wormie

2023 Asshat Award Winner
<Gold Donor>
16,820
30,963
It's a crime that raw pointers are still used. It's an even bigger crime that raw pointers are being taught.
 
  • 1Solidarity
Reactions: 1 user

Kharzette

Watcher of Overs
4,921
3,568
I always found them easier than reference counting stuff. I still screw up occasionally in C# with passing references around.

The early "smart" pointers people wrote in C++ were horrid. Reference counting for allocations but with odd quirks that lead to leaks.