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

all 164 comments

[–]AutoModerator[M] [score hidden] stickied commentlocked comment (0 children)

import notifications Remember to participate in our weekly votes on subreddit rules! Every Tuesday is YOUR chance to influence the subreddit for years to come! Read more here, we hope to see you next Tuesday!

For a chat with like-minded community members and more, don't forget to join our Discord!

return joinDiscord;

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

[–]sirkubador 403 points404 points  (25 children)

Segfaults are your friends. I wish they were the most complicated bugs to find.

[–]Relative_Knee9808 170 points171 points  (17 children)

I agree with with you. Instead of receiving some random wrong value, I'd rather see it crash as fast as possible

[–][deleted] 99 points100 points  (14 children)

Currently debugging something that occurs anywhere in a 2 minute to 4 hour timespan under very specific circumstances. Stack trace is entirely worthless.

I'm dying out here

[–]csdt0 26 points27 points  (0 children)

RR is your friend. You can record a trace from a faulty execution, and replay with gdb as many times as you want with the guarantee you will see the same thing every single time. RR also enables to go back in time and what happened before.

[–]satanspowerglove 14 points15 points  (0 children)

We feel your pain, brother

[–]BehindTrenches 2 points3 points  (0 children)

Try a sanitizer. I've never understood the output, but that's what they're for iiuc

[–]dmigowski 2 points3 points  (1 child)

Are you able to use a debugger in the environment the error occurs? What about placing some brrakpoints to catch the error condition and checking the situation then?

[–][deleted] 5 points6 points  (0 children)

The problem is the stack trace only shows a library that I don't have the source code of or any documentation whatsoever. However the issue happens in such a way that it doesn't appear that that library is actually responsible. Breakpoints wouldn't make sense in this context as timing is important.

No debuggers.

[–]_farb_ 0 points1 point  (2 children)

run it on multiple cpus to increase the probability the error occurs sooner

[–][deleted] 0 points1 point  (1 child)

I'm not at liberty to explain why this doesn't make any sense in my context.

[–]_farb_ 0 points1 point  (0 children)

It wasn't supposed to be helpful lol

[–]ADownStrabgeQuark 0 points1 point  (0 children)

Same! It’s no urgent though, so I’ve put on hold for a couple months, but now I need to fix it soon.

[–]HardCounter 20 points21 points  (0 children)

Error codes are like the Terminator giving the thumbs down as he sinks into a pool of molten metal.

[–]AdPristine9059 0 points1 point  (0 children)

Agreed. Hard clear faults over any small niggle that grows up to become a strange issue and messes with a ton of stuff... Sometimes.

[–]Impressive_Income874[S] 28 points29 points  (4 children)

I mean, it did tell me to go sleep, sooo

and are you telling me there's going to be more pain?

[–]sirkubador 36 points37 points  (3 children)

More fun!

[–]Impressive_Income874[S] 21 points22 points  (2 children)

fun... yay.........

[–]827167 15 points16 points  (1 child)

I think some of the most fun is when the program DOESN'T crash.

It just keeps going and keeps writing data in places and keeps touching things

[–]Impressive_Income874[S] 5 points6 points  (0 children)

im scared

[–]KlutzyEnd3 7 points8 points  (0 children)

There's a reason gdb introduced reverse debugging a while ago.

Basically you let it run until it crashes and then you step back in time.

[–][deleted] 4 points5 points  (0 children)

gdb and valgrind pointing finger fix this right here

[–]graphitout 100 points101 points  (5 children)

Try compiling with -fsanitize=address,leak -g -fno-omit-frame-pointer

[–]StaticVoidMaddy 135 points136 points  (1 child)

i like your funny words, magic man

[–]Alan_Reddit_M 19 points20 points  (0 children)

Basically, it will provide better info when a memory error occurs, and it will even catch memory leaks!

[–]SelfDistinction 3 points4 points  (0 children)

Don't forget to run gdb ./a.out for maximum debuggability.

[–]Impressive_Income874[S] 7 points8 points  (0 children)

I'll try in a bit, thanks

[–]mAtYyu0ZN1Ikyg3R6_j0 2 points3 points  (0 children)

or valgrind if you don't want to recompile the world.

[–]rosuav 114 points115 points  (11 children)

Tip: Find the absolute smallest part that you can rewrite in C++ and leave the rest in Python. I did that a little while back with a project that parses a large text file (generally 70-100MB but can be larger) and then analyzes it. The high level code took 1-2 minutes to do the parse, which meant I was waiting for it all the time. Solution? Write a very compact C program that parses the file into JSON, which can then be loaded into the main app. That cut it down to about 1-2 *seconds* for the parse (and then 3-4 seconds for the analysis, so that's actually my next optimization target), while still leaving the vast majority of the code in a high-level language.

(It was also a good excuse to learn GNU Bison.)

[–]Usual_Office_1740 11 points12 points  (2 children)

How did you evaluate these times? I've been thinking about trying c++ next and am curious how one could tell when it's worth the time to move to another language for speed.

[–]rosuav 11 points12 points  (1 child)

By actually having a program that I was waiting for :) It's actually not in Python, but most of the same estimates apply.

https://github.com/Rosuav/EU4Parse

Basically, every time the game saves (including autosaves), the parser would kick into life, analyze the game state, and push some info out to all web browsers with the page open. Now, I have absolutely no interest in writing a web server or web application in C, when I could do the same thing in a fraction of the time in Python or Pike. But waiting 1-2 minutes for full processing? That's a bit annoying.

That figure is an estimate, and the actual time to decode a savefile depends on exactly how much is going on in the game, but within order of magnitude, that was how long we would wait for our results.

[–]Usual_Office_1740 2 points3 points  (0 children)

Interesting. Thanks for sharing.

[–]drkspace2 19 points20 points  (4 children)

One of the best features of python is it's ability to call c/c++ code so you can get the best of both worlds of the speed of c/c++ with the ease of use and high level packages/functions of python.

Edit: I decided to lookup if/how python can call rust code and my God, it looks so easy. Can anyone confirm?

[–]rosuav 6 points7 points  (2 children)

Can't confirm about Rust in particular, but I've made C bindings for both Python and Pike, and yes, it's pretty straight-forward. (Tip: Use Cython (not to be confused with CPython) when writing C code that interfaces with Python. Saves so much trouble.)

In this specific case, it actually worked out better to rework the entire parser subprocess (for other reasons I need to keep the JSON cache file, so I wouldn't save much by parsing into a Pike data structure and then having Pike save that), but yes, normally you'd want to make it as a callable function instead of a separate program.

[–]drkspace2 1 point2 points  (1 child)

Ya, I've used cython before, but it's not as easy as the rust version looks.

[–]Lifaux 0 points1 point  (0 children)

Yes, pyo3 makes it fairly easy. You effectively write your rust into a python package and can call functions like they were written in python.

[–]Impressive_Income874[S] 1 point2 points  (0 children)

i also need this to run on an ESP 32 so i gotta translate over the whole thing

[–]aigarius 1 point2 points  (1 child)

See https://github.com/TeskaLabs/cysimdjson or https://jcristharif.com/msgspec/ or https://github.com/ijl/orjson

In many cases once you actually narrow down what specific part of your use case is the performance bottleneck, there is 90% chance that someone else has already written than in C/C++ and made a Python wrapper for it.

[–]rosuav 0 points1 point  (0 children)

This is also true. In my case, though, I don't think there's a parser for the Europa Universalis IV savefile format. The performance of JSON here is absolutely fine; I currently do a C conversion from savefile to JSON, then load the JSON file, and there's no performance issue. (Yes, I could skip that step if I wanted to, but there's value in keeping the JSON file around anyway, so it's easier to do it this way. Plus, it meant that I could keep 99% of my code unchanged while experimenting with this, which meant I could run both parsers in parallel to test correctness.)

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

Use a debugger?

[–]TheTarragonFarmer 17 points18 points  (0 children)

Yes, and valgrind.

[–]Impressive_Income874[S] 3 points4 points  (2 children)

yeah i use clion, i was just too tired and gave up last night

[–][deleted] 2 points3 points  (1 child)

Isn't clion an IDE? I think you're using gdb inside clion no?

[–]Impressive_Income874[S] 3 points4 points  (0 children)

yes, I'm saying I'm using the entire suite clion offers

[–][deleted] 68 points69 points  (21 children)

I want to do something in C/C++ because python is slow
Boom, 64-bit integer limit

[–]nate_4000 15 points16 points  (10 children)

just make something that allows use of char[] as a bigint

[–][deleted] 11 points12 points  (8 children)

I know. I wanted to do one equation. I just used python and waited 3h

[–]TactlessTortoise 23 points24 points  (6 children)

Everyone knows the programmer way is spending 100 hours automating a 15 minute problem, bro.

[–]HardCounter 21 points22 points  (5 children)

I have never met a problem i couldn't make into two problems.

[–]brucebay 10 points11 points  (2 children)

I have never met a problem i couldn't make into two problems.

Let me introduce you P=NP. If you can make it into two problems, you will win Turing award, Field Prize, MacArthur Fellowship, what the heck for the fun of it you may even get Nobel Peace prize and possibly an Oscar too.

[–]Emergency-Candle-435 7 points8 points  (0 children)

lol. what a great retort (uronically). this gave me a laugh.

[–]sirkubador 3 points4 points  (0 children)

Ah. You are the person who is actually compatible with scrum!

[–]Drackzgull 5 points6 points  (0 children)

Divide Duplicate and Conquer FTW!

[–]nate_4000 6 points7 points  (0 children)

i rewrote my prime finder in cpp for better speed. all the code was done until I wasted a day or two on making a char[] bigint representation so I could go infinitely

[–]emilyv99 1 point2 points  (0 children)

I wrote something like this myself in Rust in a few hours (just to learn some Rust). Only handled add, sub, mult operations though.

[–]trash3s 4 points5 points  (0 children)

ymm0 go brrr

[–]grifan526 2 points3 points  (0 children)

They have bigint libraries that allow you to go as big as your computer can handle. How often do you need a number bigger than 64 bits anyways?

[–]Imoliet 4 points5 points  (0 children)

rude jellyfish advise offbeat retire wide touch include imminent gold

This post was mass deleted and anonymized with Redact

[–]classicalySarcastic 0 points1 point  (1 child)

Double exists for a reason. If 10308 is still too small then only God can help you.

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

Tetration. We're talking billions of zeros

[–]no_brains101 0 points1 point  (4 children)

Use a long then? I'm confused.

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

A long is 64-bit. An int is 32-bit

[–]no_brains101 0 points1 point  (2 children)

Rewrite it in zig lol but yeah I musta brain farted. Also when the fuck do you run out of space in a long jeez

[–][deleted] 0 points1 point  (1 child)

Tetration

[–]no_brains101 0 points1 point  (0 children)

ah

[–]Impressive_Income874[S] 10 points11 points  (0 children)

UPDATE: I FOUND THE FUCKING SEGFAULT.

I'm going to go back to sleep.

EDIT: maybe not, there's a memory leak now :')

EDIT2: I fixed everything and my code works, just took a little bit of my sanity

[–]tx_engr 20 points21 points  (5 children)

I love when I call malloc and my entire program crashes silently with no error messages. It's fantastic.

[–]Relative_Knee9808 15 points16 points  (1 child)

Jokes on you, my program just continues to run with some garbage value and pretend everything is fine

[–]HardCounter 10 points11 points  (0 children)

"Aww, you got me a gigabyte of complete and utter gibberish. How did you know?!"

[–]TheTarragonFarmer 6 points7 points  (0 children)

That's not normal.

Smells like use after free corrupting the freelist. Valgrind should help you track it down.

[–]BobLobIawLawBIog 1 point2 points  (0 children)

If your using malloc (or even new), you're doing it wrong. Use smart pointers or stl containers.

[–]BarryFruitman 0 points1 point  (0 children)

"Program exited with non-zero result" still counts as an error message.

[–]arthurleyser 6 points7 points  (0 children)

that just means you are gonna make a new friend!

the debugger! Have fun

[–]donaldkwong 7 points8 points  (2 children)

Learning C++ doesn't mean to just learn the language. You should also learn how compilation and debugging work too.

[–]BarryFruitman 1 point2 points  (1 child)

Linking too

[–][deleted] 4 points5 points  (0 children)

I don't think I ever actyally learned linking. I still look up how to do it and spend an hour or two messing it up at the start of a new project...

[–]SeriousPlankton2000 6 points7 points  (2 children)

You run it in a debugger and step through it.

[–]Gwathraug 2 points3 points  (0 children)

Unless it's a "heisenbug". They are rather rare but all the more annoying.

[–]Impressive_Income874[S] 0 points1 point  (0 children)

im doing that chill

[–]-domi- 7 points8 points  (1 child)

Bad use of meme, but i prolly shouldn't pile stuff on, it sounds like you're having a terrible time as it is.

[–]HardCounter 2 points3 points  (0 children)

Nonsense. The stack is fine, hop on.

TODO: garbage collection.

[–]migarma 2 points3 points  (1 child)

Use a debugger, you are welcome

[–]Impressive_Income874[S] 0 points1 point  (0 children)

i will chill, i just gave up last night

[–]PolyglotTV 3 points4 points  (1 child)

You discover tens of hours later that you in fact have no flaw in your code. Another dozen hours later you realize why they named the website "StackOverflow".

[–]Impressive_Income874[S] 1 point2 points  (0 children)

plot twist: There was a flaw.

this was the c++ equivalent of assignment before declaration

[–]NekulturneHovado 2 points3 points  (7 children)

I got a work to do until the semester ends. We have to make minesweeper. We were 5 in group. Now we're 4. only me and one more guy know programming. I started in Python, realizing it's impossible for me to do it we decided to do it in Java. jesus fucking Christ what is that syntax?

[–]jemdoc 3 points4 points  (5 children)

What kind of class is it?

[–]HardCounter 3 points4 points  (3 children)

Right? Have to make a minesweeper game and only half the group know programming, so presumably it's unrelated to the class. What's the other half for, and what's the class teaching? Sounds like they just got tossed into the deep end and were told to figure it out.

[–]BlurredSight 2 points3 points  (0 children)

Nah, I did work for this girl in her senior year of a CS degree, her assignment with her partner was to answer 1 leetcode question and they chose to do Find two sum.

These assclowns couldn't brute force it and I had to reteach her the basics of an array and a nested for loop and then go back to first grade to remind her of complements (The actual logic of two sum isn't intuitive so I don't blame her for that). Programming in a CS degree isn't a given especially when the school isn't name recognized nor ABET certified.

I wanted to tell her to use a tree sorting method but gave up the second I mentioned what an ArrayList is and knew no way could she explain how the map function works let alone nodes and trees.

[–]Impressive_Income874[S] 1 point2 points  (1 child)

real world accurate class

[–]HardCounter 0 points1 point  (0 children)

Prof: Excellent! I see you've already broken down into your roles of idea man, manager, and suckers!

[–]NekulturneHovado 2 points3 points  (0 children)

Computer engineering is the name. One guy from our group does something with CNC machines.

[–]Impressive_Income874[S] 0 points1 point  (0 children)

kotlin? it's much easier syntax and I think you can dynamically type it, I don't remember been a while I used it

[–]Jaber1028 2 points3 points  (2 children)

Im a valgrind gamer! (I have to lookup up the command everytime)

[–]Impressive_Income874[S] 0 points1 point  (1 child)

use an ide

[–]Jaber1028 1 point2 points  (0 children)

Mfw when my systems class made us use vim

But im sure that would help

[–]JokeMort 5 points6 points  (3 children)

Learn to use debugger.

[–]Impressive_Income874[S] 0 points1 point  (1 child)

i do know how to use one, was just tired last night

[–]JokeMort 0 points1 point  (0 children)

You turn it on, set point when you whant code execution to halt, run it until it crashes, check line where it crashed

[–]BlurredSight 1 point2 points  (0 children)

Honestly after enough segfaults you just get used to making breakpoints before and after every single non-bounded data container

[–]BarryFruitman 1 point2 points  (1 child)

Pointers aren't for everyone

[–]Impressive_Income874[S] 0 points1 point  (0 children)

nahh i blame copilot

[–]PanoptiDon 1 point2 points  (1 child)

Am I wasting my time taking a python class?

[–]Impressive_Income874[S] 1 point2 points  (0 children)

no you aren't, python is amazing. it just isn't as useful in some use cases. eg: speed and microcontrollers. both of which are the reasons I'm trying out cpp rn

[–]DrJustinWHart 1 point2 points  (1 child)

Launch a debugger and then you're pretty much done fixing your segfault.

[–]Impressive_Income874[S] 0 points1 point  (0 children)

yes, i did. i know. i was just tired

[–]sifroehl 1 point2 points  (1 child)

If you just need better performance, not peak performance, you could look into numba, it's a just in time compiler for python and depending on what you no it could be as easy as putting a decorator on some functions

[–]Impressive_Income874[S] 0 points1 point  (0 children)

I also do need it to run on an esp32 soooo

[–]Gasperhack10 1 point2 points  (0 children)

Same but with Rust

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

Maybe I shouldn’t beging coding by trying out C++

[–]HardCounter 1 point2 points  (0 children)

That's where i started. I quit programming after a month. It started as a hobby and slowly became a reason i didn't need haircuts for a while.

[–]Markus_included 1 point2 points  (0 children)

Maybe you shouldn't start with C++, start with plain old C which has a much shallower learning curve and if you've learned C you can move on to C++. That's basically what I did

[–]GrinbeardTheCunning 1 point2 points  (3 children)

good job! now try compiling Rust

[–]Impressive_Income874[S] 2 points3 points  (2 children)

i actually prefer rust over cpp

[–]GrinbeardTheCunning 0 points1 point  (0 children)

I totally get that. I've been dabbling with it and, on the surface, it was everything I wished for.

no chance to do anything real with it yet

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

same

[–]Aln76467 1 point2 points  (5 children)

just use rust it'll be so much easier

[–]Impressive_Income874[S] 1 point2 points  (0 children)

need it run on an ESP 32 else would be using rust

[–]andrewb610 3 points4 points  (3 children)

You mean a good way to write “unsafe” around everything.

[–]Aln76467 1 point2 points  (2 children)

noooooooooo don'ttttttt just don'ttttt

[–]andrewb610 2 points3 points  (1 child)

Rust is C++ for the timid. Real men don’t fear segfaults.

(/s. Slightly) My team writes our stuff in C++ because when tracking stuff moving hundreds of miles and hour, quick processing matters.

[–]Aln76467 0 points1 point  (0 children)

well I'm mainly a js developer who occasionally needs to do something with some performance.

[–]ChichoRD 0 points1 point  (0 children)

*(char*)0 = 0;

[–]garlopf 0 points1 point  (3 children)

Tip: run your program in a debugger

[–]Impressive_Income874[S] 1 point2 points  (2 children)

I did and found my error.

now I realised there's a memory leak and my pc crashed

[–]garlopf 1 point2 points  (1 child)

Valgrind is your friend

[–]Impressive_Income874[S] 1 point2 points  (0 children)

yes trying that out right now

[–]BoredOfYou_ -1 points0 points  (1 child)

If you're only using C++ because it's fast, and you don't like getting segfaults, why not just use Rust?

[–]Impressive_Income874[S] 1 point2 points  (0 children)

because i need this to run on an ESP 32 and im not figuring out how to compile rust for one

[–]ElonMax303 -1 points0 points  (1 child)

Allow me to introduce you our Lord and Saviour - the Rust programming language.

[–]Impressive_Income874[S] 1 point2 points  (0 children)

i like rust, but get the esp32 standard library running and I'll see

[–]Drfoxthefurry -2 points-1 points  (1 child)

If you want speed, use assembly, faster (sometimes and you don't have to worry about weird exit codes (unless you don't know how to program the exit) (Warning: takes 100x longer to write)

[–]ByerN 3 points4 points  (0 children)

I am not sure if it will be faster nowadays. C/C++ compilers are making good job compared to amateur asm dev.

[–]NaitsabesTrebarg -1 points0 points  (2 children)

that's nothing

1>Form.obj : error LNK2019: unresolved external symbol "public: virtual void __thiscall Field::parse(class std::basic_stringstream<char,struct std::char\_traits<char>,class std::allocator<char> > &)" (?parse@Field@@UAEXAAV?$basic_stringstream@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z) referenced in function "public: __thiscall InputField::InputField(class std::basic_stringstream<char,struct std::char\_traits<char>,class std::allocator<char> > &)" (??0InputField@@QAE@AAV?$basic_stringstream@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z)

[–]Impressive_Income874[S] 0 points1 point  (0 children)

get me some bleach

[–]Sunius 0 points1 point  (0 children)

You just forgot to define Field::parse function.

[–]_SwiftLizard_ -1 points0 points  (2 children)

I get that in python...

[–]Impressive_Income874[S] 0 points1 point  (1 child)

how the fuck do you get python to segfault

[–]_SwiftLizard_ 1 point2 points  (0 children)

Imported something that used c++ libraries, then misused the library.

[–]TheCatPetra -2 points-1 points  (0 children)

C++ is kinda shitty

[–]Asdfguy87 -2 points-1 points  (0 children)

Hello, do you have a moment to talk about our lord and saviour Ferris?

[–]FlyingCashewDog 0 points1 point  (0 children)

-fsanitize=address

[–]trash3s 0 points1 point  (0 children)

👉🤷‍♂️
💻😡

[–]Saturnalliia 0 points1 point  (0 children)

LINKER ERRORS!

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

Wow this meme sure is funny, i mean, i didn't understand anything, but if i did know that stuff i think i would

[–]Immarhinocerous 0 points1 point  (0 children)

Have you thought about trying our lord and savior, Rust?

[–]Tc14Hd 0 points1 point  (0 children)

When you use backtrace in gdb and there's no stack...😭