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

Deathwing

<Bronze Donator>
16,382
7,384
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.
Without manual memory management, do you really need const?
 

Kharzette

Watcher of Overs
4,919
3,565
It would be handy for methods for sure. Just to be able to glance at it and know it doesn't modify the object is handy.
 

Deathwing

<Bronze Donator>
16,382
7,384
Hmmm, guess I've been spending too my time in Python. I barely miss private and public too.
 
  • 1Like
Reactions: 1 user

Asshat wormie

2023 Asshat Award Winner
<Gold Donor>
16,820
30,963
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.
Smart pointers are in the standard library now. Using anything else is silly.
 

Kharzette

Watcher of Overs
4,919
3,565
My last foray did not go well. Stack corruption down in the ugly smelly bowels of unreal. There's some great stuff in Unreal but some of it is just ugh.
 

ShakyJake

<Donor>
7,624
19,249
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

View attachment 179027
Sorry for the delayed response, was away yesterday and didn't feel like typing a response on a 8" Amazon tablet.

So here's the deal: this Stack implementation is templated. So you don't know what what type it's being initialized as. In my example the user of the Stack could be instantiating it like so:

Code:
Stack<int> myStack = Stack<int>();
myStack.push(100);
myStack.push(5);

and then later...

Code:
int myInt = myStack.pop();
So back to my original question: how could the Stack return a reference to a pop'd element if it's just a value type of 'int' (or double, or char, etc.)? I don't think it can. It would make sense if the Stack was storing objects which themselves would have a reference, but for value types this doesn't make sense, right? And, again, since this Stack is templated you don't know what type it's storing.
 
Last edited:

Asshat wormie

2023 Asshat Award Winner
<Gold Donor>
16,820
30,963
Sorry for the delayed response, was away yesterday and didn't feel like typing a response on a 8" Amazon tablet.

So here's the deal: this Stack implementation is templated. So you don't know what what type it's being initialized as. In my example the user of the Stack could be instantiating it like so:

Code:
Stack<int> myStack = Stack<int>();
myStack.push(100);
myStack.push(5);

and then later...

Code:
int myInt = myStack.pop();
So back to my original question: how could the Stack return a reference to a pop'd element if it's just a value type of 'int' (or double, or char, etc.)? I don't think it can. It would make sense if the Stack was storing objects which themselves would have a reference, but for value types this doesn't make sense, right? And, again, since this Stack is templated you don't know what type it's storing.
The stack is just an abstract concept. What you have in reality is a bunch of pointers to memory locations. Return the pointer, not what is stored at locations.
 

ShakyJake

<Donor>
7,624
19,249
I think your question can be answered by answering the following:

What is the type of a pointer and does it differ based on what the pointer points to?
I don't understand the question.

The stack, internally, is using an array. If I return array[0] with pop() then shift the elements in the array to account for index 0's content removal (i.e. index 1's value is moved to 0), what am I returning a reference to?
 

Asshat wormie

2023 Asshat Award Winner
<Gold Donor>
16,820
30,963
I don't understand the question.

The stack, internally, is using an array. If I return array[0] with pop() then shift the elements in the array to account for index 0's content removal, what am I returning a reference to?
Yeah it was badly written, I removed it.

Array[0] is syntactic sugar for a pointer p + 0 (Array[1] would be p + 1 and so on). It points to some memory location. When you pop your element 0, that memory location still has your data. You return the pointer to that location.
 

ShakyJake

<Donor>
7,624
19,249
Yeah it was badly written, I removed it.

Array[0] is syntactic sugar for a pointer p + 0 (Array[1] would be p + 1 and so on). It points to some memory location. When you pop your element 0, that memory location still had your data. You return the pointer to that location.

But it's not because I am shifting the elements in the array upon pop(). The value of p + 1 is being moved to p + 0 (and so on depending upon number of elements in the array). If I returned a reference to p + 0 then the user of the Stack would actually being getting p + 1's data.
 

Asshat wormie

2023 Asshat Award Winner
<Gold Donor>
16,820
30,963
But it's not because I am shifting the elements in the array upon pop(). The value of p + 1 is being moved to p + 0 (and so on depending upon number of elements in the array). If I returned a reference to p + 0 then the user of the Stack would actually being getting p + 1's data.
Have you considered not shifting the elements but changing what is considered the top instead?
 

ShakyJake

<Donor>
7,624
19,249
Have you considered not shifting the elements but changing what is considered the top instead?
Sure, I could do that but that would mean that the Stack would forever store anything pushed into it. That's crazy and I seriously doubt real-world stacks work this way.
 

Asshat wormie

2023 Asshat Award Winner
<Gold Donor>
16,820
30,963
Sure, I could do that but that would mean that the Stack would forever store anything pushed into it. That's crazy and I seriously doubt real-world stacks work this way.
You can always empty that location later. Or you can do a circular stack. Or probably a million other things. The point is that your professors implementation returns a pointer to what is being popped and not the value of what is being popped.
 

ShakyJake

<Donor>
7,624
19,249
You can always empty that location later.

You can't do that because you don't know when the user will access what was popped. It may not be immediately.

My point is that the professor is wrong and from discussing it with him I could tell he himself doesn't have a firm grasp of what's going on.

Anyway, thanks for your input. Appreciate it.
 

Asshat wormie

2023 Asshat Award Winner
<Gold Donor>
16,820
30,963
And this is why using c/c++ is retarded for data structures class. One spends more time wrestling with code instead of learning actual data structure concepts.

Python truly is king.
 
  • 1Solidarity
Reactions: 1 user

Asshat wormie

2023 Asshat Award Winner
<Gold Donor>
16,820
30,963
Yeah me too. I remember one project was to solve any maze; the solution was implementing a dfs using a deque I think. Anyway, I remember it taking me a week to work out the c++ kinks when the actual idea of the solution took like an hour.