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 #293509 | Initial revision | — | 26 days ago |
Answer | — |
A: Casting a non-`void` pointer to `uintptr_t` If reading the standard strictly by the letter then you are correct. And therefore both CERT and MISRA are picky with these kind of conversions because they strive to cover all poorly-defined behavior. However, I think this is one of the cases when one can assume a sensible compiler implementation... (more) |
— | 26 days ago |
Edit | Post #293493 |
Post edited: |
— | about 1 month ago |
Edit | Post #293493 |
Post edited: |
— | about 1 month ago |
Edit | Post #293493 |
Post edited: |
— | about 1 month ago |
Edit | Post #293493 |
Post edited: |
— | about 1 month ago |
Edit | Post #293493 | Initial revision | — | about 1 month ago |
Answer | — |
A: What are X macros and when to use them? Purpose and use X macros is a design pattern used for the purpose of centralizing data & code maintenance to a single point in the program. Instead of maintaining code based on some data set in multiple places, we can gather the data at one single place, in a macro. And it's all done at compile-ti... (more) |
— | about 1 month ago |
Edit | Post #293492 | Initial revision | — | about 1 month ago |
Question | — |
What are X macros and when to use them? Occasionally I run into some strange pre-processor code with a list like this: ```c #define LIST \ X(1) \ X(2) \ X(3) \ ``` And then code followed by other obscure macros and macro calls like: ```c typedef enum { #define X(n) ITEM##n, LIST #undef X ... (more) |
— | about 1 month ago |
Comment | Post #293449 |
We shouldn't compare unsigned numbers against signed ones. That `i != -1` check is brittle. Consider what will happen if we just change from 64 to 16 bit: for (uint16_t i = 15; i != -1; i--) { printf("%"PRIu16 " ",i); } Oops. Here `i` actually gets promoted to a signed type before the comparison, but... (more) |
— | about 1 month ago |
Edit | Post #293461 |
Post edited: |
— | about 1 month ago |
Edit | Post #293461 | Initial revision | — | about 1 month ago |
Answer | — |
A: Why is a for loop getting stuck when using a uint64_t counter, whereas a while loop isn't? This is a bit of a well-known problem when converting from an up-counting to a down-counting loop and using an unsigned loop iterator. Since unsigned numbers are always positive and have well-defined wrap-around when going below zero, `i >= 0` will always be true for an unsigned loop iterator. Com... (more) |
— | about 1 month ago |
Edit | Post #293433 | Initial revision | — | about 1 month ago |
Answer | — |
A: A simple implementation of a mutable String in C Design/API - Consider hiding all details of the String internals to the caller by implementing opaque type - How to do private encapsulation in C? (As it happens, that post contains a example taken from another string handling class.) - The code responsible for allocation should also be responsible... (more) |
— | about 1 month ago |
Comment | Post #293256 |
@#64656 It is not possible to declare a VLA inside a struct. (more) |
— | 2 months ago |
Edit | Post #293256 |
Post edited: |
— | 3 months 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) |
— | 3 months ago |
Edit | Post #293256 | Initial revision | — | 3 months 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) |
— | 3 months 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) |
— | 3 months 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) |
— | 3 months 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) |
— | 3 months ago |
Edit | Post #292988 |
Post edited: |
— | 3 months 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) |
— | 4 months 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) |
— | 4 months ago |
Edit | Post #293091 | Initial revision | — | 4 months 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) |
— | 4 months ago |
Edit | Post #293090 | Initial revision | — | 4 months 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) |
— | 4 months ago |
Edit | Post #293026 |
Post edited: |
— | 4 months ago |
Edit | Post #293026 | Initial revision | — | 4 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) |
— | 4 months ago |
Edit | Post #293025 | Initial revision | — | 4 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) |
— | 4 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) |
— | 4 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) |
— | 4 months ago |
Edit | Post #292988 | Initial revision | — | 4 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) |
— | 4 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) |
— | 4 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) |
— | 4 months ago |
Edit | Post #292978 | Initial revision | — | 4 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) |
— | 4 months ago |
Edit | Post #292976 | Initial revision | — | 4 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) |
— | 4 months ago |
Edit | Post #284849 |
Post edited: |
— | 5 months ago |
Edit | Post #292934 | Question closed | — | 5 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) |
— | 5 months ago |
Edit | Post #289414 |
Post edited: |
— | 5 months ago |