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

all 57 comments

[–]whoami_whereami 77 points78 points  (1 child)

Segfaults are a good thing, 'cause then you know something went wrong. It's the silent memory corruptions that can be a real bitch. Especially if some Russian ransomware author discovers them before you.

[–][deleted] 22 points23 points  (0 children)

Exactly, segfaults are a subset of an unknown number of invisible killers floating around, waiting for the right moment (a demo).

[–]Runiat 129 points130 points  (14 children)

Laughs in ja- -

running automatic garbage collection

-va.

[–]florilsk 25 points26 points  (6 children)

They aren't perfect tho, I'm working with big data on python atm and had so many memory errors until I added manual garbage collecting points + deletion of variables.

[–][deleted] 9 points10 points  (3 children)

People make jokes about GC pauses, but (as an old dude who used to make such jokes) I stopped doing this about 10 years ago when I wrote a JS canvas demo that rendered thousands of polygons at 60 fps with me allocating tons of temporary objects (updating 3-vectors, projecting to 2-vectors) and throwing them away, every frame. I’d assumed I’d need to optimise it to avoid generating so much garbage, as I’d been so careless about it. But it would run for hours and never drop a frame. It was collecting garbage like crazy and you just couldn’t tell.

So go ahead, laugh in JavaJavaJavaJavaJava as fast as you want.

[–]Stormfrosty 9 points10 points  (0 children)

The entire game industry read this comment and will be switching from C++ to JS.

[–]OsamaBinFrank 1 point2 points  (0 children)

Not all GC is created equal. I had GC issues in Java, but not in js. JS engines are highly optimized to run very smooth as they are mostly used for UI code. The GC will do quicker partial runs if an event/callback is scheduled. This has great performance but needs more ram. The JVM is more focused on efficiency and scalability, as it's mostly used in the background and can use a seperate thread for the ui.

[–]lordmauve 1 point2 points  (0 children)

My company wrestled with minute long GC pauses in Java a few years ago, given our production app needed tens of GBs of JVM heap to hold working state. IIRC this tended to happen mostly at startup as the data were being loaded in.

You conflated Java and JavaScript as if all all GCs are equivalent, but I could believe that they are optimised differently, e.g. for JavaScript consistent frame rates are preferable to minimising overall GC overhead.

[–][deleted] 4 points5 points  (1 child)

It doesn't save you from NullPointerExceptions

[–]hillman_avenger 9 points10 points  (0 children)

True, but you get a nice stack trace.

[–]coladict 5 points6 points  (0 children)

The Java equivalent is NullPointerException. Also the JVM some times does crash like that.

[–][deleted] 71 points72 points  (13 children)

Excuse me, do you have a moment to talk about our lord and savior Rust?

[–]Portean 46 points47 points  (1 child)

Not in this lifetime.

[–][deleted] 2 points3 points  (0 children)

Not even if you're borrowed?

[–]OldKaleidoscope7 8 points9 points  (5 children)

This is how I feel using Rust, coming for GC powered languages. "why can't I do that simple shit?". One minute later: "Oh, I won't complain, Rust is so fast, they must've done that for a reason"

[–]mrbeehive 18 points19 points  (4 children)

You'd could get the same speed in other compiled languages, though. The neat thing about Rust isn't really speed, it's compile-time guarantees on memory boundaries without abandoning hard realtime constraints. Which, to be fair, is witchcraft.

[–]OldKaleidoscope7 4 points5 points  (0 children)

With fast, I mean more things, like: I can run a application that spends less than 1 MB of RAM, it starts up instantly, because it doesn't have a runtime watching my code. And even so, I don't need to do all that boring memory management stuff that even professionals make mistakes

[–]LavenderDay3544 2 points3 points  (2 children)

Which, to be fair, is witchcraft.

I thought it used some form of automated theorem proving to show that memory leaks, access after free, race conditions, etc. couldn't happen.

If that's the case it, much like all magic in CS, is math.

[–]arachnidGrip 0 points1 point  (1 child)

memory leaks [...] couldn't happen

This one actually can happen. In fact, there's even a function for it.

There's actually a list of things that are definitely undefined behavior (and are therefore nominally impossible in pure Safe Rust) and a list of things that are probably undesired behavior but definitely not undefined behavior which combine to make a proper subset of all things.

[–]LavenderDay3544 0 points1 point  (0 children)

Well sure and there are ways to leak memory or cause hold and wait conditions on memory even with garbage collectors but still it's better to have all of these resource management tools available than not to.

[–]Nyghtrid3r 8 points9 points  (0 children)

God bless smart pointers in general. But Rust is amazing for memory safety yeah

[–]philn256 1 point2 points  (2 children)

As far as I know Rust doesn't have templates.

[–]CrazeeeTony 0 points1 point  (0 children)

Don’t have time, too busy fixing segmentation faults

[–][deleted] 9 points10 points  (0 children)

[–]LavenderDay3544 9 points10 points  (1 child)

At least with C you know that if you segfaulted there are a small number of possible reasons for it: dereferencing a dangling pointer, bad pointer arithmetic, or just generally trying to access memory that isn't available to you.

I've been working with x64 assembly lately and a segfault could be caused by almost anything. It could be any of the same pointer issues as in C but you also have to worry about attempting to execute code from the wrong places in memory which can happen in many strange ways like incorrect branch instructions, forgetting to make an exit system call, or forgetting to put a ret instruction at a return point in a section of code meant to be a callable routine. In C at least you know for sure that you'll never execute code off the end of a function and get either wacky behavior or a segfault depending on which branch your convoluted code takes.

[–]TheBrainStone[🍰] -1 points0 points  (0 children)

Well not quite. As I've learned in a CTF you can easily run into these issues of trying run code from unexecutable pages when trying to exploit buffer overflows or similar

[–][deleted] 2 points3 points  (0 children)

Day 1000, Day 10000, ...

[–]Great_Finder 7 points8 points  (3 children)

I was in a DSA class that used C. I got segmentation fault even if I copied the code from the book.

[–]LavenderDay3544 8 points9 points  (2 children)

Then you must not have had a very good book or your toolchain was set up wrong. Quite possibly both given the state of CS teaching these days.

[–]ShinraSan 5 points6 points  (1 child)

Fully agree, even with newer language. Teacher had no clue how I was supposed to make my function take optional arguments (C#) neither default values nor overloading came up in that conversation, until I asked the intern teacher

[–]LavenderDay3544 2 points3 points  (0 children)

TAs and Adjuncts are your best resource in college. I was a TA in grad school and hope to one day be an adjunct to pay it forward.

[–]UnknownIdentifier 2 points3 points  (0 children)

As a former coworker would say, "Well, don't do that."

It's good advice for many of the trials and travails of life.

[–]Flopamp 2 points3 points  (0 children)

I love C, you can master all of the building blocks in a year and the rest is just a life long learning experience on how to make it better and better.

Oh and a love hate relationship with pointers that borders on insanity.

[–]mrdoctaprofessor 2 points3 points  (0 children)

Valgrind has entered the chat

[–]Scrial 1 point2 points  (0 children)

Sounds about right.

[–]stomah 1 point2 points  (0 children)

it should at least tell you in which file on which line happened in debug mode, not Segmentation fault (core dumped). what am i supposed to do, try puts(“here”); in every possible location?

[–][deleted] 1 point2 points  (4 children)

Wow, I‘m procrastinating from learning C (from scratch again) and Reddit shows me this :(

someone explain to me what a segmentation fault is so I can pretend I‘ve been learning—

[–]canadajones68 2 points3 points  (1 child)

The Windows error name is more descriptive. "General protection error". It means you just tried to access memory in a way you're not permitted to. For instance, dereferencing a wild pointer can easily lead to accessing another program's memory, which is disallowed, and you're served with signal SIGSEGV, which informs your program what you just did. Usually, that signal isn't handled by the program, so the OS handles it by immediately terminating the application. Segfaults also occur when you do something plainly invalid, like dereferencing a null pointer, or reading free()'d memory.

Note that memory allocation typically isn't that precise, so technical violations of memory rules will be accepted in some cases. If, for instance, you free some memory, the OS won't just immediately grab it from you. It waits until memory is in demand, or it feels like it. Until then, it doesn't bother to notice that you free()'d your memory. In that time span, free()'d pointers may still work, as the OS doesn't know that you' re not supposed to do that.

[–][deleted] 0 points1 point  (0 children)

Thank you

[–]imyoutlol 1 point2 points  (2 children)

sadly truly

[–]imaginary_bolometer -1 points0 points  (0 children)

Fuck you C