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 #285968 |
Post edited: |
— | about 3 years ago |
Edit | Post #285968 | Initial revision | — | about 3 years ago |
Answer | — |
A: Is it OK to use scanf with a void pointer? Void pointers are compatible with every other object pointer type and as mentioned in another answer, 7.21.6/10 speaks of the type of the pointed at object, not the type of the pointer. This is consistent with the rules of effective type/pointer aliasing (6.5/6), which have to be applied as well, sin... (more) |
— | about 3 years ago |
Comment | Post #285957 |
@#8196 All of these are artificial examples. The whole point of using forward declaration in the context of opaque types is to block the caller from knowing anything about that type - which as it happens means that they won't know the struct size either. As for dynamically allocating an array of func... (more) |
— | about 3 years ago |
Comment | Post #285956 |
@#8196 And how exactly is it less duplication to increase the references to the `p` identifier from 3 times to 6 times? Imagine if it isn't called `p` but `long_contrived_name_foo`, which happens to have a sibling `long_contrived_name_bar` and if we mix up these identifiers, everything will crash & b... (more) |
— | about 3 years ago |
Comment | Post #285956 |
@#53305 That's an excellent argument actually. The argument in favour of this `sizeof *p` style is that someone would aimlessly change the _type_ of a variable without checking the code using that variable. But what if they change the _identifier_ of that variable instead - again without checking the... (more) |
— | about 3 years ago |
Edit | Post #285957 | Initial revision | — | about 3 years ago |
Answer | — |
A: When does it not work to dereference the pointer for sizeof during malloc? In addition to all the examples I gave in my answer to the linked post, all scenarios where `p` is a pointer to incomplete type fails. Not just the `void` scenario, but also when `p` is a pointer to an array of incomplete type: int (p)[] = malloc(n sizeof p); // will not compile cleanly An... (more) |
— | about 3 years ago |
Comment | Post #285956 |
Actually, after giving this some thought... a compiler must yield a diagnostic message when de-referencing a void pointer _and passing it to `sizeof`_. Both gcc and clang do this in `-pedantic` mode. They are required to do so by constraints 6.5.4.3 regarding the sizeof operator. So yes something is ... (more) |
— | about 3 years ago |
Comment | Post #285956 |
De-referencing void pointers is not allowed as per 6.3.2.2 "The (nonexistent) value of a void expression (an expression that has type void) shall not be used in any way, and implicit or explicit conversions (except to void) shall not be applied to such an expression." Clang seems to fail in issuing a... (more) |
— | about 3 years ago |
Comment | Post #285956 |
`p[i][j]); // Add asterisk to p[i][j]` Shouldn't it be `*p[i][j]`? (more) |
— | about 3 years ago |
Comment | Post #285946 |
`_Nonnull` don't use this. First of all, convention gives that this ought to be the caller's responsibility. Second, if you insist on using such features, the standard already has support for this through `static`. And lately compilers also have added support for it. So if you wish to protect against... (more) |
— | about 3 years ago |
Edit | Post #285952 | Initial revision | — | about 3 years ago |
Answer | — |
A: stpecpy(): Design a better string copy function that truncates - Performance-wise, I'd benchmark this vs `if(memchr(src,'\0',n)==src+n) memcpy(dst, src, n);` because it isn't obvious at least to me if that's faster or slower than your custom function. - Regarding where `end` is pointing, I think that's the right call since it's convenient to have a pointer to t... (more) |
— | about 3 years ago |
Comment | Post #285910 |
@#8196 The equivalent `int (*p)[5]` would have been just as problematic. There's plenty of examples where the style simply doesn't work well. (more) |
— | about 3 years ago |
Comment | Post #285899 |
@#53937 Really? I haven't heard about that and the C committee hates fixing language flaws at the expense of backwards compatibility. Such a radical change would be nice but it also means that all C code out there won't be compatible with C2x. (more) |
— | about 3 years ago |
Comment | Post #285899 |
Expect casting to the expected type is a common way to dodge implicit promotions, so doing so might actually be considered good practice depending on context. For example MISRA-C wouldn't allow this code `unsigned short a,b; ... signed int c = a + b;` but require `c = (signed int)(a + b)`. Though thi... (more) |
— | about 3 years ago |
Comment | Post #285908 |
You could take the live production database and have it copied to a second instance on regular basis, for example at the same time when backups are running.
This identical, continuously updated copy of the real database becomes your test one, where you are free to play around. This also allows you... (more) |
— | about 3 years ago |
Edit | Post #285912 | Initial revision | — | about 3 years ago |
Answer | — |
A: Are static pointers implicitly initialized to NULL? Yes, it is guaranteed to evaluate to true. All variables with static storage duration are set to zero in case of arithmetic types or set to null in case they are pointers. The relevant part is C17 6.7.9/10: > If an object that has automatic storage duration is not initialized explicitly, its value... (more) |
— | about 3 years ago |
Comment | Post #285899 |
The forgetting stdlib.h argument was what once started the whole "don't cast the result" business, where otherwise people wouldn't care much about the cast. Bugs caused by C90 implicit int are _not_ fun to debug, they are subtle as sin. It was a very valid argument back in the 1990s. Today, not so mu... (more) |
— | about 3 years ago |
Comment | Post #285899 |
The difference between `(int*)malloc` and `(int)42` is that malloc does actually return a different type. In your integer examples, the type is already the expected `int`. Furthermore, the C language does actually not allow _any_ implicit pointer conversions except during pointer assignment. 6.5.4 _C... (more) |
— | about 3 years ago |
Edit | Post #285910 | Initial revision | — | about 3 years ago |
Answer | — |
A: How to properly use malloc? Should we cast the result of malloc? The cast to the intended type is not necessary in C, since during assignment, the `void` returned by `malloc` can be implicitly converted to any other object pointer and vice versa (as per C17 6.5.16.1). So the cast does add clutter. However, there's nothing wr... (more) |
— | about 3 years ago |
Comment | Post #285826 |
It's the very same thing as I mentioned here: https://software.codidact.com/posts/278910/278932#answer-278932. In fact I've stopped writing self-answered Q&A on SO and post all such content here instead. Because such posts tend to be of significantly higher quality than the average Q&A (and require a... (more) |
— | about 3 years ago |
Comment | Post #285875 |
So how about dropping the latter part of that sentence and go with something like "Best practices as long as a clear 'best' criteria is present. Examples could be: what code gives fastest execution, least memory use, widest tool support for a target, most beginner-friendly IDE for a certain language ... (more) |
— | about 3 years ago |
Comment | Post #285875 |
I think the rationale for the "best" criteria needs to be there to prevent really bad questions like "what compiler is best". Asking "what compiler is best _for a beginner using Windows_" is however quite some improvement of that question. Perhaps this should be rephrased focusing on the quality of t... (more) |
— | about 3 years ago |
Edit | Post #285870 | Initial revision | — | about 3 years ago |
Question | — |
A cleanup of "What type of questions can I ask here?" What type of questions can I ask here? has gotten rather cluttered since we have added/removed a lot of things along the way. In particular, I think it is hard to get an idea of what's on-topic by just taking a glance at it. Proposed changes: 1) Move the text directly below on-topic to a separa... (more) |
— | about 3 years ago |
Comment | Post #285836 |
Since cyclomatic complexity is the number of possible execution paths, it is expected behavior. What one can do is to keep switches simple - if they only check an integer enum which has a contiguous sequence, the switch will get optimized well. In case of floating point or strings etc, not so much. T... (more) |
— | about 3 years ago |
Comment | Post #285813 |
Similarly, how can you explain passing a function to a function, is this pass by value: `void func (void f(void));` Of course not. (more) |
— | about 3 years ago |
Comment | Post #285813 |
"if you want to pass a reference to an object you always have to be explicit about it" How do you explain this then? `void func (int arr[n]);` (more) |
— | about 3 years ago |
Comment | Post #285800 |
@#53177 Maybe give it a week at least to let more people vote and give feedback? (more) |
— | about 3 years ago |
Edit | Post #285800 | Initial revision | — | about 3 years ago |
Answer | — |
A: Should we allow UI/UX questions in our community? I think it should be on-topic with the following distinction: On-topic - Technical questions regarding to UI, including mark-up language syntax, how to set properties in RAD tools, how to use graphic libraries etc. - Algorithm questions for how to draw graphics, including at some extent the math... (more) |
— | about 3 years ago |
Edit | Post #285724 | Initial revision | — | about 3 years ago |
Answer | — |
A: Why object-oriented instead of class-oriented? As with anything computer science-related that dates back to the 1960s and 70s, things just happened at a whim. Everything was new and highly experimental back then. Nobody knew how to write or design programs the best way, how to organize them or what coding styles that were most readable. The whole... (more) |
— | about 3 years ago |
Edit | Post #285716 |
Post edited: |
— | about 3 years ago |
Edit | Post #285716 |
Post edited: |
— | about 3 years ago |
Edit | Post #285716 | Initial revision | — | about 3 years ago |
Answer | — |
A: Are there references in C? Yes there are references and pass-by-reference in C, though the language has no explicit syntax item called "reference" like C++. In a C context, it is irrelevant that C++ happens to have something called references, which are basically glorified, read-only pointers. The general computer science t... (more) |
— | about 3 years ago |
Edit | Post #285715 | Initial revision | — | about 3 years ago |
Question | — |
Are there references in C? When reading posts at programming sites such as this one, I frequently encounter people saying things like: "There is no pass-by-reference in C, everything is passed by value." People claiming such tend to be of the C++ programmer variety and are used to the C++ syntax distinction between poi... (more) |
— | about 3 years ago |
Comment | Post #285575 |
@#54720 You could ask that as a separate question. (more) |
— | about 3 years ago |
Edit | Post #285575 |
Post edited: |
— | about 3 years ago |
Edit | Post #285575 |
Post edited: |
— | about 3 years ago |
Edit | Post #285575 | Initial revision | — | about 3 years ago |
Answer | — |
A: Question regarding an error message in my compiler to do with my code on linked list. To use the identifier `Node` without typing `struct Node`, you must use a `typedef`: typedef struct Node{ // this here is a stuct tag int data; struct Node next; // this has to be struct Node, refers to the above tag }Node; // but from this line and onward we can use Nod... (more) |
— | about 3 years ago |
Edit | Post #285544 | Initial revision | — | about 3 years ago |
Question | — |
How do I search for "i++"? I once wrote this post here: Why can't we mix increment operators like i++ with other operators? When using Codidact search looking for that post, I tried to type `i++` in the search but it didn't like that syntax. Then I tried `"i++"` but then the search gave no results. How should I search for t... (more) |
— | about 3 years ago |