Welcome to Software Development on Codidact!
Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.
Activity for Lundin
Type | On... | Excerpt | Status | Date |
---|---|---|---|---|
Comment | Post #293091 |
Btw `errno` error handling is another huge designer facepalm originating from that same smelly swamp, so it's not something to encourage or specify in when designing new functions. Someone asked "surely it isn't possible to make something even worse than Windows awful GetLastError" and then *nix/POSI... (more) |
— | 7 days ago |
Comment | Post #293091 |
@#53937 Yes they could define it but chose not to, and so the functions are broken. It's entirely a fault caused by the C standard and would have been easy to fix during C90 standardization, when they chose to pick up the function from the smelly swamp of sloppily designed, sloppily specified Unix cr... (more) |
— | 7 days ago |
Edit | Post #293091 | Initial revision | — | 7 days ago |
Answer | — |
A: Why is atoi dangerous and what should be used instead? The `atoi` family of functions should never be used for any purpose - they are broken by design. The reason why can be found in the C standard C23 7.24.1: > The functions `atof`, `atoi`, `atol`, and `atoll` are not required to affect the value of the integer expression `errno` on an error. If t... (more) |
— | 7 days ago |
Edit | Post #293090 | Initial revision | — | 7 days ago |
Question | — |
Why is atoi dangerous and what should be used instead? According to Which functions in the C standard library must always be avoided?, the `atoi` family of functions is dangerous and should never be used for any purpose. The rationale given in the answer is this: > These have no error handling but invoke undefined behavior whenever errors occur. Compl... (more) |
— | 7 days ago |
Edit | Post #293026 |
Post edited: |
— | 22 days ago |
Edit | Post #293026 | Initial revision | — | 22 days ago |
Answer | — |
A: typeof_unqual behaves differently in gcc and clang The C23 example as well as clang are correct. This is apparently a gcc bug in the latest 14.2 release, fixed in the "gcc (trunk)" unreleased version. The relevant part of the C23 standard here is 6.7.4.1 §10: > If the specification of an array type includes any type qualifiers, both the array a... (more) |
— | 22 days ago |
Edit | Post #293025 | Initial revision | — | 22 days ago |
Question | — |
typeof_unqual behaves differently in gcc and clang C23 6.7.3.6 contains this (informative) example demonstrating the use of `typeofunqual`: ```c const char const animals[] = { "aardvark", "bluejay", "catte", }; typeofunqual(animals) animals2array[3]; ``` And this is supposedly equivalent to `const char animals2array[3];` accordin... (more) |
— | 22 days ago |
Comment | Post #292978 |
@#64656 On the other hand, categories mainly make sense when there are different site policies for different kind of questions (like code review for example). I don't see how there is any differences in rules between lets say an OO design question and a code implementation question. For example neith... (more) |
— | 30 days ago |
Comment | Post #292978 |
@#64656 If there would be a lot of design, coding style or "big picture" questions it may might sense to create a separate category for them. But for now such questions seem to be a minority. Also, things like coding style are far less subjective than people think. Like if I would ask a question "wha... (more) |
— | about 1 month ago |
Edit | Post #292988 | Initial revision | — | about 1 month ago |
Answer | — |
A: How does the strict aliasing rule enable or prevent compiler optimizations? Pointer conversions and aliasing First of all, C historically allows all manner of crazy pointer conversions (whereas C++ is more restrictive) between compatible and non-compatible pointed-at types, as well as to/from the generic `void`. There are several reasons to allow that: type punning, gener... (more) |
— | about 1 month ago |
Comment | Post #292978 |
It is particularly important to discuss matters of program design and coding style with beginners, or otherwise they pick up bad habits from the start and carry those with them through their career. Therefore, giving everyone a forum to ask about such matters is important. (more) |
— | about 1 month ago |
Comment | Post #292978 |
This is a bit of a rant so I kept it out of the answer:
Some sites like SO have very strange misconceptions rooted into the site culture, such as the misconception that program design and coding style questions don't belong on a programming site. That's plain silly IMO - that's not how software en... (more) |
— | about 1 month ago |
Edit | Post #292978 | Initial revision | — | about 1 month ago |
Answer | — |
A: Comparing our site scope to Stack Overflow The main difference between Stack Exchange and Codidact is that SE loves to spawn off hundreds of sites with lots of overlapping scopes, whereas Codiact has the category system, which means that contents like for example AI and GenAI could exist on the same site. The general idea about software.c... (more) |
— | about 1 month ago |
Edit | Post #292976 | Initial revision | — | about 1 month ago |
Answer | — |
A: Understanding "logical OR" and "logical AND" in programming languages They aren't actually that different from natural language. But more verbose. Given some generic pseudo code looking like one of the C family languages: ``` if( !egg.boiled || !egg.peeled ) { donoteat(egg); } ``` Then this is to be translated as: "If the egg is not boiled or the egg is... (more) |
— | about 1 month ago |
Edit | Post #284849 |
Post edited: |
— | about 1 month ago |
Edit | Post #292934 | Question closed | — | about 1 month ago |
Comment | Post #292934 |
We'll need far more details than that. What libs or RAD tool were used, if any? How do you take keyboard input? If it's a raw Winapi program then please post relevant parts of the code by editing the question. (more) |
— | about 1 month ago |
Edit | Post #289414 |
Post edited: |
— | about 1 month ago |
Comment | Post #292695 |
@#53937 Once some behavior makes it into the Unix/POSIX libs, it tends to stay the same (for good or bad). But I would imagine that you would find oddities if you'd go on a tour and inspect all embedded systems compilers out there - most were designed for targets where heap allocation is senseless. S... (more) |
— | 2 months ago |
Edit | Post #292819 | Initial revision | — | 2 months ago |
Answer | — |
A: How do you implement polymorphism in C? First of all please note that polymorphism in C is clunky. It won't be pretty. We don't have `this` pointers, we don't have RAII, we don't have automatic constructors/destructors. Just accept that it the code won't look as pretty as in languages that have all of these features. Second, we really o... (more) |
— | 2 months ago |
Edit | Post #292818 | Initial revision | — | 2 months ago |
Question | — |
How do you implement polymorphism in C? The topic of how to implement polymorphism in C tends to pop up now and then. Many programmers are used to OO design from higher level languages and supposedly OO is a "language-agnostic" way of proper program design no matter language. So how to do it in C? A design pattern I've often seen is to ... (more) |
— | 2 months ago |
Comment | Post #292695 |
@#82304 System APIs have no benefits from providing a scenario where the program may potentially be running off into the woods. They are non-portable, whereas the C standard libs seek to be portable and therefore have vague wording and poorly-defined behavior - sometimes where it is justified, someti... (more) |
— | 2 months ago |
Comment | Post #292695 |
@#53937 The general approach for readability could be to add an abstraction layer on top of malloc to hide away all the ugly gunk. Or the other way around, strip away malloc and go straight for the system APIs which will be better defined than the C standard libs - at the expense of non-portable code... (more) |
— | 2 months ago |
Comment | Post #292695 |
...now we could of course always over-allocate malloc with one garbage byte. `malloc(n+1)` but the program only uses `n` bytes. That's 100% portable. (more) |
— | 2 months ago |
Comment | Post #292695 |
@#53937 As I said in the initial comment, it is probably naive to think `malloc(0)` results in less branches, because there will likely be a special case inside the malloc implementation instead, meaning that a rare branch gets hit. Also the implementation of heap allocation in itself is usually huge... (more) |
— | 2 months ago |
Comment | Post #292695 |
And well seriously, who thinks `while((*indirect) != entry) indirect = &(*indirect)->next;` is good-looking code? The first parenthesis is superfluous. `&(*indirect)->next;` is obfuscating posing and _here_ an extra parenthesis could perhaps have been motivated: `&((*indirect)->next)`. But the main p... (more) |
— | 2 months ago |
Comment | Post #292695 |
@#53937 As for Linus Torvalds he's probably as far from a C style authority as you can possible get. Anyone who is disagreeing ought to read the Linux kernel source. I was a fan of Linux until I read the that... afterwards I started to wonder if I need to keep a 10 meter distance to all Linux compute... (more) |
— | 2 months ago |
Comment | Post #292695 |
@#53937 Obviously you can write that code in any number of ways. `ptr = (size ? malloc(size) : NULL);` for example though I personally think that's less readable. Error handling always comes with overhead - it's a fact. The most elegant code out there is usually not very rugged. (more) |
— | 2 months ago |
Comment | Post #292748 |
@#53937 C is still flawed. There's the definition of a _null pointer constant_ in C, which is clear. And then there's an ambiguous part regarding the `NULL` macro saying "...NULL which expands to an implementation-defined null pointer constant;". I think the intention was to say it is implementation-... (more) |
— | 2 months ago |
Comment | Post #292695 |
@#53937 Sounds like premature optimization, `if(size>0) ptr=malloc(size); else ptr=NULL;` is hardly some massive overhead. You can still have your for loop after that and then pass the pointer to free when done. (more) |
— | 2 months ago |
Comment | Post #292695 |
@#53937 I'd say that _code bases_ relying on malloc(0) are dying platforms. The appropriate way to write programs is to initialize the pointer to NULL, only allocate when there is something to allocate, and upon deallocation pass the pointer along to free() no matter if malloc was ever called or not.... (more) |
— | 3 months ago |
Comment | Post #292695 |
Rather, nobody knows what exactly it is, because the changes to the standard were of such poor quality that it's all over the place... (more) |
— | 3 months ago |
Edit | Post #292695 | Initial revision | — | 3 months ago |
Answer | — |
A: Which platforms return a non-null pointer on malloc(0) It is trivial enough to test: ```c #include #include #include #define KNOWNGARBAGE ((int)0u) int main (void) { int ptr = KNOWNGARBAGE; ptr = malloc(0); int errnochanged = errno; if(ptr == NULL) puts("Returned NULL."); else if(ptr == KNOWNGARBAGE) puts("Didn'... (more) |
— | 3 months ago |
Edit | Post #291960 |
Post edited: |
— | 3 months ago |
Edit | Post #292618 |
Post edited: |
— | 3 months ago |
Edit | Post #292618 | Initial revision | — | 3 months ago |
Answer | — |
A: I created a tag, now it need edit. Streams is an universal concept not specific to any particular language. It originates from ancient Unix, most famously the stdout and stdin streams. And so they exist in pretty much any "C family" programming language and probably in a lot of other languages as well. Therefore `stream` is not a usef... (more) |
— | 3 months ago |
Comment | Post #292529 |
This isn't really in the domain of the compiler but something called static code analysis. Compilers may do that at some extent as a bonus, but it is not its job. As for clang specifically I believe it comes with a static analyzer built-in which you can invoke with `clang --analyze`. I haven't really... (more) |
— | 3 months ago |
Comment | Post #292371 |
`_X` is an identifier reserved for the library, so for whatever the reason, the compiler doesn't think you are compiling libraries but application code. I suppose this can happen if you mix different library implementations together. There are for example different flavours of Mingw64 CRT: the older ... (more) |
— | 4 months ago |
Edit | Post #292264 |
Post edited: |
— | 4 months ago |