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 #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) |
— | 6 months ago |
Edit | Post #292819 | Initial revision | — | 6 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) |
— | 6 months ago |
Edit | Post #292818 | Initial revision | — | 6 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) |
— | 6 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) |
— | 6 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) |
— | 6 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) |
— | 6 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) |
— | 6 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) |
— | 6 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) |
— | 6 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) |
— | 6 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) |
— | 6 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) |
— | 6 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) |
— | 6 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) |
— | 6 months ago |
Edit | Post #292695 | Initial revision | — | 6 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) |
— | 6 months ago |
Edit | Post #291960 |
Post edited: |
— | 7 months ago |
Edit | Post #292618 |
Post edited: |
— | 7 months ago |
Edit | Post #292618 | Initial revision | — | 7 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) |
— | 7 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) |
— | 7 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) |
— | 7 months ago |
Edit | Post #292264 |
Post edited: |
— | 8 months ago |
Edit | Post #292264 |
Post edited: |
— | 8 months ago |
Edit | Post #292264 | Initial revision | — | 8 months ago |
Answer | — |
A: New elementsof() operator I don't really see what good `elementsof` would do since `sizeof(arr)/sizeof(arr)` is pretty idiomatic C and problem-free given that you know what you are doing, as is the case with most C code. IMO the C committee should be focusing on fixing known problems and weeding out poorly-defined behavio... (more) |
— | 8 months ago |
Comment | Post #291982 |
@#53937 I don't see why `auto ap_ = ap;` is any better than `typeof(ap) ap_ = ap;` though. And in either case you risk getting parameters that have gone through implicit conversion by accident, so this would be better off as a function. (more) |
— | 8 months ago |
Edit | Post #291982 |
Post edited: |
— | 9 months ago |
Edit | Post #291982 |
Post edited: |
— | 9 months ago |
Comment | Post #291981 |
@#53549 It doesn't really assume anything, it just asks why someone would consider it dangerous. This was actually the beginning of a self-answered Q&A and I had considered to show plenty of examples in the answer part.
It would indeed to a very good question to ask for the committee(s) intent or ... (more) |
— | 9 months ago |
Edit | Post #291982 | Initial revision | — | 9 months ago |
Answer | — |
A: Why is the new auto keyword from C++11 or C23 dangerous? The `auto` feature was indeed mainly meant to solve long cumbersome template container declarations in C++. But when introduced to C23 - where there are no templates let alone template containers - it just ends up as a solution without any problem that it solves. `auto` can create new problems ju... (more) |
— | 9 months ago |
Edit | Post #291981 | Initial revision | — | 9 months ago |
Question | — |
Why is the new auto keyword from C++11 or C23 dangerous? In older C and C++ standards, the `auto` keyword simply meant automatic storage duration. As in the compiler automatically handles where the variable is stored, typically on the stack or in a register. And it was a pretty useless keyword since it can only be used at local scope, where all variables d... (more) |
— | 9 months ago |
Edit | Post #291960 |
Post edited: |
— | 9 months ago |
Edit | Post #291960 | Initial revision | — | 9 months ago |
Answer | — |
A: Closing self-answered question due to not being clear enough A question should be closed when it cannot be meaningfully answered by someone who knows the topic. Which might indeed mean that the one(s) casting the close vote(s) would need domain knowledge. That is, it would be impossible answering it without speculating or coming up with opinions. Or the q... (more) |
— | 9 months ago |
Comment | Post #291898 |
This question is getting discussed at meta: [Closing self-answered question due to not being clear enough](https://software.codidact.com/posts/291929). (more) |
— | 9 months ago |
Edit | Post #289415 |
Post edited: |
— | 9 months ago |
Edit | Post #291855 | Initial revision | — | 9 months ago |
Answer | — |
A: Are these two function pointer declarations equivalent? Normally, whenever a function designator (a function's name) is used in an expression, it decays to a function pointer to that function. `typeof` is one of those exceptions to the "decay" rules of C (C23 6.3.2.1 §4), so if you give it a function type as operand, the result is a function type and ... (more) |
— | 9 months ago |
Edit | Post #291854 |
Post edited: |
— | 9 months ago |
Comment | Post #291854 |
What exactly is `foo`? Did you mean to use one of the previously mentioned function identifiers or did you mean `operation`? (more) |
— | 9 months ago |
Edit | Post #291853 | Initial revision | — | 9 months ago |
Answer | — |
A: Should beginner-oriented Q&A here include basic use of a terminal (command line) for developers? Shell scripts, BASH, batch files, PowerShell etc etc are all on-topic here and so questions about command line commands ought to be as well. We may however require the question to include enough relevant details like which OS, which version/distro or otherwise the question is probably off-topic. ... (more) |
— | 9 months ago |
Comment | Post #291705 |
@#78383 Not at all, probably. Dates like that (Or __STDC_VERSION__) which are existing as integer constants might be candidates to pass to a macro which performs "stringification" with `#`. If you add `'` to the constant then you would screw up the indices for finding year, month and date in that str... (more) |
— | 9 months ago |
Comment | Post #291805 |
Unless the amount of data is huge, simply copy it down to a separate buffer and let another thread take it from there? Then only the RAM copy time will lag down the GUI and not the file I/O. At which point the optimization should focus on slimming down the copy time. (more) |
— | 9 months ago |
Comment | Post #291805 |
Is there any particular use-case scenario you have in mind? Because no matter system, one way to work around file I/O bottlenecks is just to outsource it all to a separate thread. Then let that thread chew away at the files at whatever pace it likes and be done with it. In which case performance of t... (more) |
— | 10 months ago |