Live data from Hacker News

No way to parse integers in C (2022)

blog.habets.se

71–80 of 117 posts

Re: No way to parse integers in C (2022)

#71

Earlier quoted context omitted.

Crash and report an error.

You report an error and exit cleanly with a proper operating system error code. Crashing is a quick hack, acceptable for throwaway projects but not in software used long-term.

Crashing (in the sense of "give up and exit with an error") on invalid inputs is valid (and often the best thing) in many cases.

Fix your inputs.

Re: No way to parse integers in C (2022)

#72

Earlier quoted context omitted.

You report an error and exit cleanly with a proper operating system error code. Crashing is a quick hack, acceptable for throwaway projects but not in software used long-term.

Crashing (in the sense of "give up and exit with an error") on invalid inputs is valid (and often the best thing) in many cases. Fix your inputs.

I think you're using "crash" to mean "exit early". I am using "crash" in the sense of "this program did something causing the OS to terminate it externally". I suppose that's a real point of difficulty in communication across different programming languages.

We agree that the program should exit early. I think we agree it should do it cleanly and intentionally. I'm adding the constraint that "crash" doesn't necessarily mean "cleanly and intentionally", especially when talking about a C program.

Re: No way to parse integers in C (2022)

#73

Earlier quoted context omitted.

You first ask if you really need to.

Unless you're exposing it to the internet, ever, in the entire future history of the program. Then you kind of have to, in one form or another.

You have to, but you probably shouldn’t do it by trying to add the inputs. That opens a door for DDOS attacks.

Returning an error on inputs that are too long (for some definition of it) is the way to go.

Re: No way to parse integers in C (2022)

#74
post #16

Earlier quoted context omitted.

The thing I find irritating is all the folks who say C is broken because it’s not a write once run anywhere language like JavaScript or python. Part of the deal has always been that the programmer needs to understand the target platform and the target compiler’s behavior.

Write once run anywhere? But C already is a "write once run anywhere" language! Though, you usually have to recompile first :) The criticisms related to UB are not about understanding the target platform and the target compiler's behavior. Undefined Behavior is not the same thing as Implementation-defined Behavior, and lots of folks (including me) would be satisfied with reclassifying chunks of UB as the latter. The…

> crash in a C program that turned out to be due to the compiler removing a null check.

The what now? Though not lately, I did program in C for 15 years and never seen something like this. I did see some compiler bugs on obscure platforms (SINIX, IRIX, HPUX on Itanium64, etc.) with proprietary compilers, this kind of thing would make really get me shouting.

Were you able to determine why the compiler did this? Is it a bug in the compiler?

Re: No way to parse integers in C (2022)

#75
post #59
post #56

Earlier quoted context omitted.

No. What gives that idea? The language doesn't even fix the data size of its primary numerical type. No way anyone thought that was portable.

Is this sarcasm? I thought C didn't fix the size of int because they were trying to make C programs "portable" between architectures with different natural word sizes. It was a mistake, but I remember that as being the stated reason. I'm happy to be corrected if I'm misremembering my history though.

I suppose one could say that they didn't fix the data size so the language would be portable. But I can't see how the intent was that programs would be portable, if you define portable to mean 1:1 functioning across differing platforms.

Re: No way to parse integers in C (2022)

#76
post #63
post #27

Earlier quoted context omitted.

My memory growing up is that making your own C library was basically an inevitable rite of passage for any aspiring programmer.

Yeah, it's a shame we never got something like boost for C. Every company I ever worked for had its own common C library solving these problems.

I worked at a shop where we used Boost in a C++ code base that the only use of C++ was the harness to use Boost. After that, it was all C, object-styled C, as that code base started before C++ compilers were not a template overlay on C.

Re: No way to parse integers in C (2022)

#77
post #63

Earlier quoted context omitted.

Yeah, it's a shame we never got something like boost for C. Every company I ever worked for had its own common C library solving these problems.

It's a shame we never got a package manager for C (or C++). EDIT: perhaps I should have been clearer; by not having one early on, we now have multiple competing package managers, with no clear winner. Responses prove that point.

Never used this? https://vcpkg.io/en/

Re: No way to parse integers in C (2022)

#78
post #16

Earlier quoted context omitted.

The thing I find irritating is all the folks who say C is broken because it’s not a write once run anywhere language like JavaScript or python. Part of the deal has always been that the programmer needs to understand the target platform and the target compiler’s behavior.

The point of this post, though, is even something as simple as "give me this string as an integer" doesn't have an answer that doesn't come with "are you OK with this best effort parse under these edge cases? Oh and we use this number as error, so you can't parse that". Like… edge cases? It's parsing a number ! We're not talking about I/O on hard vs soft intr NFS mounts, here. There's a right answer. strlen(), on val…

Somewhat true, but C is pretty close to translating directly to machine code, even if most compilers now do so many complex things the assembly can be pretty far off. My point being is that if you have a type int in your program, it's specifically tied to the byte size of an integer on the target platform. While it can be 8, 16, 32, 64 bits, it's defined based on what the target platform supports efficiently.

So, when you say, "it's purely the language", I have to disagree. The language means different things on different platforms but it's still defined exactly on the target platform. And it's efficient on that platform.

Nowadays, we prefer correct vs. efficient, which I do agree with, of course. But, I also understand why C is like it is. It is possible to claim it's a problem of the language but I would argue that it is not. C gives us barebones and working with it we have to know this. If that's not needed then sure, other languages will be easier to work with.

Re: No way to parse integers in C (2022)

#79
post #63

Earlier quoted context omitted.

Yeah, it's a shame we never got something like boost for C. Every company I ever worked for had its own common C library solving these problems.

It's a shame we never got a package manager for C (or C++). EDIT: perhaps I should have been clearer; by not having one early on, we now have multiple competing package managers, with no clear winner. Responses prove that point.

Although vcpkg is probably the most popular, I’m a fan of https://conan.io/center

Re: No way to parse integers in C (2022)

#80
post #59
post #56

Earlier quoted context omitted.

No. What gives that idea? The language doesn't even fix the data size of its primary numerical type. No way anyone thought that was portable.

Is this sarcasm? I thought C didn't fix the size of int because they were trying to make C programs "portable" between architectures with different natural word sizes. It was a mistake, but I remember that as being the stated reason. I'm happy to be corrected if I'm misremembering my history though.

Why would it be a mistake? It's efficient for the target platform.

The same code can be compiled for different platforms, yes, but the assembly and machine code will vary significantly, so it could behave differently. Porting to a new platform was usually a very complex process, but the code produced was efficient. Nobody seems to care about this nowadays, though, it seems.

Post reply on HN