Readit News logoReadit News
GorgeRonde commented on So you think you know C? (2016)   wordsandbuttons.online/so... · Posted by u/goranmoomin
dahart · 6 years ago
I was trying to help by explaining it, instead of saying go read K&R, but I don’t get the feeling you really heard or understood me. There is no other story. There is no yes and no. There is only no. You cannot manage to do it. It does not work to return local memory from a function, ever, period. Once you return, it is 100% unsafe to try to use the memory from your previous stack. There is absolute zero comfort in recursive calls.

You are mistaking some luck in having it not crash once for thinking that it’s okay in some situations. It’s not okay under any circumstances. That’s what makes this even more dangerous. Your program could crash at any time. It might run a thousand times and then suddenly start crashing. It might always run for you, and then crash on other people. But just because it runs once without crashing doesn’t mean it’s working.

A signal is not the only way your function’s stack can get stomped on the very next instruction after you return. Other processes and other threads can do it, the memory system can relocate your program or another one into your previous memory space. Recursive calls are guaranteed to stomp on previous stack frames when your recursion depth decreases and then increases, the previous stack will be overwritten.

Returning a pointer to a local stack frame is always incorrect. It’s not risky, it’s wrong.

BTW: you have the ability to see comments below the downvote limit, go to your profile settings and turn on showdead.

I didn’t downvote you, if that’s why you were trying to explain voting behavior to me, but you will find on HN that downvotes and upvotes both happen for a wide variety of reasons, and are not limited to either whether people agree, nor whether the comments are polite. Downvotes are often cast for comments that break site guidelines, for example just failing to assume good faith can get you downvoted. So can making blanket generalizations about a group of people, like the above “I guess C programmers do not know the difference...”. See the comments section here: https://news.ycombinator.com/newsguidelines.html

I sometimes upvote what appear to be unfairly downvoted comments to me. I usually upvote people who read and respond to me, regardless of whether I agree with them.

GorgeRonde · 6 years ago

    - Java: no
    - Ruby: no
    - PHP:  no
    - C:    yes and no

GorgeRonde commented on So you think you know C? (2016)   wordsandbuttons.online/so... · Posted by u/goranmoomin
dahart · 6 years ago
> it is possible (it’s not really stable but works reliably in recursive calls IIRC). [...] I do not like this attitude

You might want to listen. You’re getting the K&R comment and the downvotes because this does not work, ever. It’s a really, really bad idea. In recursive calls, it might not crash right away, but you will have bad data, the memory at the pointer address will have been overwritten by the next stack frame that’s placed there.

Don’t ever return pointers to local memory because the memory is “gone” and unsafe to use the moment your function returns. Even if you try it and think it works, it can and probably will crash or run incorrectly in any other scenario - different person, different computer, different compiler, different day...

Your comments about getting a warning and ‘However if you wrap the local’s address... it “works”’ should be clues. The warning is the compiler telling you not to do it. The workaround doesn’t work, it only compiles. By using aliasing, you’re only tricking the compiler into not warning you, but the warning is there for a reason.

GorgeRonde · 6 years ago
Listening to what ? To the dude that tells me that's not possible and proceeds to dump a big pile of authority on top of my head or to my own experiment that tells me another story ?

I would have preferred to be told:

- yes and no. You'll get warnings if you try to return a pointer to a local, however, doing this and that, you can manage to do it.

- but once you have achieved that, the result will be dependent on the way the stack is handled (not really in your control). You'll feel some comfort doing this in recursive calls, however beware of signal.h.

But this isn't the answer I received. I guess C programmers do not know the difference between what you can do (however risky) and what you shouldn't do. Also when someone asks such "weird" questions, do not assume he's a beginner with no notion of what constructs he can handle safely, maybe he's someone trying to find the limits of C – and once these limits are identified it can be a good conversation starter about C's internal and the way various compilers differ.

Edit: also downvotes on HN are not like downvotes on Reddit: there's actually a limit (-2 ?). Below this the comment disappears. Conclusion: only downvote when the comment engages in antisocial behavior (not respecting the rules or common human decency, etc ...), not when you disagree with it. I always upvote an unfairly downvoted comment for these reasons.

GorgeRonde commented on So you think you know C? (2016)   wordsandbuttons.online/so... · Posted by u/goranmoomin
todd8 · 6 years ago
Yes.

Signal handlers allow C programs to respond to events outside of the normal control flow (see signal.h, etc.). This means that once a function, say fnc1, has returned, the memory on the stack that was used by fnc1 can end up being reused at any point in time. A signal, perhaps generated completely asynchronous to the program itself by a different process, causes a stack frame to be allocated (possibly on top of fnc1’s old stack frame) for use by the corresponding signal handler. This could happen at any time, even before fnc1’s caller gets a chance to use the pointer returned by fnc1.

GorgeRonde · 6 years ago
Thanks, that was interesting.
GorgeRonde commented on So you think you know C? (2016)   wordsandbuttons.online/so... · Posted by u/goranmoomin
GorgeRonde · 6 years ago
Went to an IRC chat room when I was learning C in school. Asked if you could return a pointer to something that lives on the stack. Was talked down by an all-knowing dude telling me to go read K&R again. Proceeded to write a code sample [1] that showed it is possible (it's not really stable but works reliably in recursive calls IIRC).

I do not like this attitude (then again it was just one random dude).

[1] https://www.onlinegdb.com/HyO5VXRxS

GorgeRonde commented on Fighting Complexity in Software Development   github.com/atsapura/CardM... · Posted by u/atsapura
GorgeRonde · 6 years ago
Fighting stupidity, laziness and a misplaced love for typography in software developers
GorgeRonde commented on Burnout and the Brain (2016)   psychologicalscience.org/... · Posted by u/akeck
ben_w · 6 years ago
Good for you. I’ve tried working twice as much once for ten days, and I am never doing that again. Not even for a one-off with one hundred thousand dollars/Euros/whatever after-tax overtime pay.
GorgeRonde · 6 years ago
So are you fine with the idea that I get twice as much money and decision power than you, all things beyond worked hours being equal ?
GorgeRonde commented on Burnout and the Brain (2016)   psychologicalscience.org/... · Posted by u/akeck
newshorts · 6 years ago
I’m surprised at the number of comments saying they don’t work 60/80+ hours a week.

I thought this was just the industry norm now and was going to factor it into my salary when I start looking for a new job

Honestly, isn’t it safe To assume the company is underreporting the workload and instead calculate Salary at an average of 60 hours/week?

GorgeRonde · 6 years ago
Officially I work 35h/w. A little less than 40k€. Senior level. Very efficient. I think my actual hourly pay is under the minimum wage. No overtime pay of course.
GorgeRonde commented on Burnout and the Brain (2016)   psychologicalscience.org/... · Posted by u/akeck
stareatgoats · 6 years ago
> At its core, burnout emerges when the demands of a job outstrip a person’s ability to cope with the stress.

Having witnessed multiple burnouts first-hand I'd say this description is only half-way helpful. In my lay-man experience a burnout occurs when the aforementioned precedes several days without sleep. Lack of sleep over a prolonged stressful period must be avoided, especially from ages 30-40, otherwise damage will highly likely occur, similar to blown fuses in the brain, which takes many years to recover from (if ever, completely).

Many people that reach this stage of stress unfortunately acquire a tunnel vision that convinces them they are invincible and can do anything (except take care of themselves and their loved ones). It is one of the scourges of modern life, and a tragedy that people are not properly prepared for this in school, especially in industries that are susceptible to it, like ours.

GorgeRonde · 6 years ago
I have no problem working twice as much as normal. What I want is to be paid twice as much (at least).
GorgeRonde commented on A general-purpose probabilistic programming system with programmable inference   github.com/probcomp/Gen... · Posted by u/espeed
ioquatix · 6 years ago
The email operator.
GorgeRonde · 6 years ago
In Romance languages the at-sign is called an arobase/arroba.
GorgeRonde commented on A general-purpose probabilistic programming system with programmable inference   github.com/probcomp/Gen... · Posted by u/espeed
bayareanative · 6 years ago
RPN FTW. Good ol' LISP. :)

In Haskell, you can turn any function into an infix operator with backquotes. As a qualitative, nonfunctional stylistic, ergonomic opinion, I find Haskell much more flexible and beautiful than the ugliness of most brace-, paren- and prolixity-heavy languages. It can present too many features for pragmatic use, orthogonal to something in another domain like Rust or Go for effective software production where features are intentionally constrained or disallowed.

GorgeRonde · 6 years ago
We have macros and LISP is a building material, not a language.

One line of code is just enough to get something that works (defmacro infix [a op b] `(~op ~a ~b))

There are libraries for that too. Clojure: https://github.com/rm-hull/infix

u/GorgeRonde

KarmaCake day279November 4, 2013View Original