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 |
---|---|---|---|---|
Edit | Post #293256 |
Post edited: |
— | 9 days ago |
Comment | Post #293247 |
@#53937 It is always a pointer. Check out [Do pointers support "array style indexing"?](https://stackoverflow.com/q/55747822/584518) (more) |
— | 9 days ago |
Edit | Post #293256 | Initial revision | — | 9 days ago |
Answer | — |
A: 2D-array pointer as a struct member Given that you don't need the flexibility provided by a `char` but rather need efficiency, plus a fairly large amount of items, it does sound like you need a true 2D array allocated on the heap. The char member isn't ideal either for performance reasons, since it will point at data allocated outs... (more) |
— | 9 days ago |
Comment | Post #293246 |
@#85470 The `char*` member isn't ideal either for performance reasons, since it will point at data allocated outside the struct array. Ideally the struct array will be allocated on the heap too, along with the pointed-at data. But if you want this to be truly flexible and realloc:able, you'll probabl... (more) |
— | 9 days ago |
Comment | Post #293246 |
In case the data is strings, an array of pointers `char**` is often the most convenient since it is flexible and memory efficient, but at the cost of execution speed. If the data is something else, then the flexibility of using a `char**` is probably not worth it. (more) |
— | 10 days ago |
Comment | Post #292988 |
@#53937 One can probably write a big paper on all the problems with the rules. How to treat type qualifiers, aggregate types, how dynamic allocation works in C at all etc etc. This isn't a new problem, it's been there since C89. As for WG14 they should keep themselves well-busy writing technical corr... (more) |
— | 10 days ago |
Edit | Post #292988 |
Post edited: |
— | 11 days ago |
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) |
— | about 1 month 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) |
— | about 1 month ago |
Edit | Post #293091 | Initial revision | — | about 1 month 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) |
— | about 1 month ago |
Edit | Post #293090 | Initial revision | — | about 1 month 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) |
— | about 1 month ago |
Edit | Post #293026 |
Post edited: |
— | about 2 months ago |
Edit | Post #293026 | Initial revision | — | about 2 months 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) |
— | about 2 months ago |
Edit | Post #293025 | Initial revision | — | about 2 months 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) |
— | about 2 months 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) |
— | 2 months 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) |
— | 2 months ago |
Edit | Post #292988 | Initial revision | — | 2 months 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) |
— | 2 months 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) |
— | 2 months 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) |
— | 2 months ago |
Edit | Post #292978 | Initial revision | — | 2 months 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) |
— | 2 months ago |
Edit | Post #292976 | Initial revision | — | 2 months 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) |
— | 2 months ago |
Edit | Post #284849 |
Post edited: |
— | 2 months ago |
Edit | Post #292934 | Question closed | — | 2 months 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) |
— | 2 months ago |
Edit | Post #289414 |
Post edited: |
— | 2 months 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) |
— | 3 months ago |
Edit | Post #292819 | Initial revision | — | 3 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) |
— | 3 months ago |
Edit | Post #292818 | Initial revision | — | 3 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) |
— | 3 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) |
— | 3 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) |
— | 3 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) |
— | 3 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) |
— | 3 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) |
— | 3 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) |
— | 3 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) |
— | 3 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) |
— | 3 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) |
— | 3 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) |
— | 4 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) |
— | 4 months ago |
Edit | Post #292695 | Initial revision | — | 4 months ago |