|
Java code:
|
# ? Mar 9, 2013 17:39 |
|
|
# ? May 25, 2024 15:06 |
|
What's wrong with it, other than having it be a bounded size? EDIT: whoops, didn't read pop() enough carefully. It seems this is a C guy who was forced into Java.
|
# ? Mar 9, 2013 17:57 |
|
My first thought: "What's wrong with an array implementation? It's even in the CLR book." My second thought: "Oh."
|
# ? Mar 9, 2013 17:58 |
|
It seems like if you push one thing and then try to pop it you'll get an exception? Edit: Also, returning 1. I'm guessing that was originally error handling but now it's just pointless
|
# ? Mar 9, 2013 17:58 |
|
Let me guess, they also think Java is pass-by-reference for objects.
|
# ? Mar 9, 2013 18:02 |
|
If you instantiate, push, pop, you get an exception because he starts at -1 instead of 0. You must push a sacrificial element to this implementation. I would recommend pushing it's own reference. E;f,b...
|
# ? Mar 9, 2013 18:11 |
|
HappyHippo posted:It seems like if you push one thing and then try to pop it you'll get an exception? Volmarias posted:If you instantiate, push, pop, you get an exception because he starts at -1 instead of 0. Java code:
Java code:
|
# ? Mar 9, 2013 18:37 |
|
Also, memory leak: references to popped elements stay around until that spot is re-used.
|
# ? Mar 9, 2013 19:11 |
|
Xerophyte posted:Note: consider not trying to learn Agda. I've almost managed to write a modulus function on nats that passes the termination checker.
|
# ? Mar 9, 2013 19:24 |
|
Freakus posted:Also, memory leak: references to popped elements stay around until that spot is re-used. Aha, good catch. Obviously not a leak in C but he doesn't know Java at all.
|
# ? Mar 9, 2013 21:38 |
|
Also nobody has commented on the fact that, why the hell do the push and pop functions return an int? And why do they always return the value 1? vvv I knew that, it's a rhetorical question to bring attention to the fact that this, too, is a horror vvv PrBacterio fucked around with this message at 04:35 on Mar 10, 2013 |
# ? Mar 10, 2013 04:24 |
|
Because it originally looked like this:C++ code:
|
# ? Mar 10, 2013 04:32 |
|
Spatial posted:
Arrays
|
# ? Mar 10, 2013 05:55 |
|
It's Java, that's how they do work. Yay type erasure. Though JDK code casts elements, not arrays, from what I remember.
|
# ? Mar 10, 2013 06:25 |
|
Aleksei Vasiliev posted:It's Java, that's how they do work. Yay type erasure. Though JDK code casts elements, not arrays, from what I remember.
|
# ? Mar 10, 2013 06:29 |
|
ninjeff posted:Arrays Every semester when I teach my data structures course, I spend a good 10 minutes ranting about Java's horrible design decisions when it comes to generics, especially about how there's no legitimate reason to NOT allow new E[...] since E is always going to be an object type so the compiler should just allocate an Object array and implicitly do the cast for me. And then the cast isn't even enough, I have to @SuppressWarnings to completely shut it up.
|
# ? Mar 10, 2013 17:35 |
|
PrBacterio posted:vvv I knew that, it's a rhetorical question to bring attention to the fact that this, too, is a horror vvv I guess I don't understand what's wrong with it in C.
|
# ? Mar 10, 2013 17:39 |
|
Suspicious Dish posted:I guess I don't understand what's wrong with it in C.
|
# ? Mar 10, 2013 18:04 |
|
Suspicious Dish posted:Because it originally looked like this:
|
# ? Mar 10, 2013 18:19 |
|
Why?
|
# ? Mar 10, 2013 18:25 |
|
There's no useful way to get the element out of "pop" (or "stack_pop") since the assignment is local to that method only. It actually makes the data structure completely useless since it's "input only". In other words: carry on then posted:Let me guess, they also think Java is pass-by-reference for objects.
|
# ? Mar 10, 2013 18:31 |
|
Shugojin posted:The entirety of Numerical Recipes in C and how they didn't completely port it from Numerical Recipes in FORTRAN so every single loving bit of array math starts at 1 instead of 0 so you need to constantly shift so that it accesses what you want FAKE EDIT: Also, I'm pretty sure ptr - 1 isn't even valid Standard C, since it can have undefined behavior.
|
# ? Mar 10, 2013 18:32 |
|
ExcessBLarg! posted:There's no useful way to get the element out of "pop" (or "stack_pop") since the assignment is local to that method only. It actually makes the data structure completely useless since it's "input only". Yeah, but how is the C one just as useless?
|
# ? Mar 10, 2013 18:38 |
|
Zemyla posted:FAKE EDIT: Also, I'm pretty sure ptr - 1 isn't even valid Standard C, since it can have undefined behavior.
|
# ? Mar 10, 2013 18:40 |
|
carry on then posted:Yeah, but how is the C one just as useless? code:
|
# ? Mar 10, 2013 18:42 |
|
ExcessBLarg! posted:There's no useful way to get the element out of "pop" (or "stack_pop") since the assignment is local to that method only. It actually makes the data structure completely useless since it's "input only". Not in C. Perhaps I should have put void **elem_out as the signature instead, but it's the same.
|
# ? Mar 10, 2013 18:43 |
|
ExcessBLarg! posted:Although not obligated, presumably the implementation of the function is: That wouldn't make any sense. The implementation would be *elem_out = elem; which would assign to the out pointer.
|
# ? Mar 10, 2013 18:43 |
|
Suspicious Dish posted:Not in C. Perhaps I should have put void **elem_out as the signature instead, but it's the same. Java object references aren't like C++ references. Actually they're more like implicit C/C++ pointers. All Java objects are dynamically allocated from the heap (but automatically freed by the garbage collector). The Java "." operator is equivalent to the C(/C++) "->" operator for struct(/class) types. However, there's no "*" or "&" operators, no double-pointers, no pass-by-reference, etc. There's only
Edit: Suspicious Dish posted:That wouldn't make any sense. ExcessBLarg! fucked around with this message at 18:58 on Mar 10, 2013 |
# ? Mar 10, 2013 18:50 |
|
ninjeff posted:It's array covariance, not type erasure - C# and VB have the same problem despite having real generics Huh? C# code:
|
# ? Mar 10, 2013 18:51 |
|
Yeah, I'm assuming that he took away things like * which caused compile errors in Java. So it was probably something likecode:
code:
|
# ? Mar 10, 2013 18:52 |
|
ExcessBLarg! posted:That's exactly the point though. To make "stack_pop" useful in C, you have to pass a double-pointer and assign to the deferenced pointer. Java doesn't have "double-pointers" so there's no way to assign a method reference as they're passed by value. Yes, we know. What we're saying is that this guy used to teach C or C++, and then got stuffed into a Java course, and naively fiddled around enough to make his lesson plans compile, but not actually work.
|
# ? Mar 10, 2013 19:01 |
|
Suspicious Dish posted:Yes, we know. What we're saying is that this guy used to teach C or C++, and then got stuffed into a Java course, and naively fiddled around enough to make his lesson plans compile, but not actually work. If nothing else, this is the only explanation for using a RuntimeException to indicate an empty stack. "Hmm, I don't know what this 'throws' clause is, I'll just find a way to not have to use it."
|
# ? Mar 10, 2013 19:27 |
|
Not to mention the half implemented "using ints to indicated success or failure" idiom.
|
# ? Mar 10, 2013 19:36 |
|
Zemyla posted:Oh god yes. I used it as a pedagogical text in high school, and then when I got to college I found out about the GNU Scientific Library. Seriously, screw 1-based arrays in C. I was working on something that relied upon an unholy, uncommented union of NR's Levernberg-Marquardt fitting method and GSL libraries last week and bitching about NR in here was one of my rewards for success. As a first encounter with GSL it was... Suboptimal. All the GSL calls and such were fine but all the logic for calling it relied on NR's absolute horseshit.
|
# ? Mar 10, 2013 20:14 |
|
HappyHippo posted:Huh? The problem C# has with array covariance is this: C# code:
|
# ? Mar 10, 2013 20:21 |
|
Qwertycoatl posted:The problem C# has with array covariance is this: Right. But does that affect this case (with the stack class the prof is trying to make)? C# code:
|
# ? Mar 10, 2013 21:44 |
|
HappyHippo posted:Right. But does that affect this case (with the stack class the prof is trying to make)? No, that's fine. C# isn't as crazy as Java, it's just a bit crazy.
|
# ? Mar 10, 2013 22:50 |
|
Aleksei Vasiliev posted:It's Java, that's how they do work. Yay type erasure. Though JDK code casts elements, not arrays, from what I remember. Java code:
|
# ? Mar 11, 2013 06:38 |
|
Don't forget the out of bounds indexing when you pop() an empty Stack, or the out of bounds indexing when you try to push size+1 elements in, and both cases the class cheerfully makes the attempt. Man, a data structures lecturer who completely bombs array index math 101, that's a new one.
|
# ? Mar 12, 2013 01:15 |
|
|
# ? May 25, 2024 15:06 |
|
A co-worker stomped over a change I made to a source controlled file today. The horror is that we're both ALM consultants and this guy's spent hundreds if not thousands of hours teaching people how to use source control.
|
# ? Mar 12, 2013 01:51 |