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 #296669 |
"which doesn't even work well for restrict" Indeed - first you promise "I will only change the object through this pointer" then suddenly you go "...but I might change the pointer itself, muahaha!". It might be difficult to even make sense of `type*restrict*` code.
(more) |
— | about 15 hours ago |
| Comment | Post #296682 |
In the ring buffer example specifically - suppose that the members are protected by race condition bugs by code executing internally in the driver, ensuring that data is taken from the struct without an interrupt kicking in and overwriting the data in the middle of a read.
Now if the caller g... (more) |
— | about 16 hours ago |
| Comment | Post #296682 |
I believe this is a duplicate of [Why is global evil?](https://software.codidact.com/posts/291186) and there's not really anything C specific here either - private encapsulation is recommended in all programming languages.
If there's anything C specific here then maybe it is: what if the user ... (more) |
— | about 16 hours ago |
| Comment | Post #296669 |
@#53937 It sounds like they were indeed just talking nonsense then, not aware of the C17 clarification to _Generic. Although it _would_ technically be possible in [contrived ways](https://godbolt.org/z/PYvssbs4q) to write a _Generic checking if something is restrict... Since nothing like that is ... (more) |
— | 1 day ago |
| Comment | Post #296669 |
@#53937 Yeah sure you can do pointer-to-pointers or function pointers but was that really the meaning of 6.7.4? It rather seems like the new text was added by someone who's slept to the changes of _Generic in C17 and then those who made said changes in C17 were asleep while voting in this text.
... (more) |
— | 2 days ago |
| Edit | Post #296669 |
Post edited: |
— | 3 days ago |
| Edit | Post #296669 | Initial revision | — | 3 days ago |
| Question | — |
How to use _Generic on restrict pointers like C23 tells me to do? I just noticed that ISO 9899:2024 6.7.4 has this weird text added, which wasn't there previously: > The intended use of the `restrict` qualifier (like the `register` storage class) is to promote optimization, and deleting all instances of the qualifier from all preprocessing translation units co... (more) |
— | 3 days ago |
| Comment | Post #296654 |
@#36396 The number of cores matter because there are co-processor architectures that split the memory area used by each core, so that the co-processor can work with "DMA like" processing without stalling the main core. In that context, the concept of "saving memory for someone else" might become ... (more) |
— | 4 days ago |
| Comment | Post #296662 |
@#36396 Except it doesn't really have any pros in this context. The main advantage on a PC is that you can heap allocate large objects instead of storing them on the stack and risk stack overflow. But on a microcontroller that's nonsense: you get less total memory if you include a heap and that m... (more) |
— | 4 days ago |
| Comment | Post #296648 |
@#36396 The problem is as illustrated in the linked post to EE.SE - C cannot guarantee atomicity unless you specifically use `atomic` language features from C11. And the resulting race condition bugs aren't necessarily always of a data corruption nature, it may also manifest itself as in the prog... (more) |
— | 4 days ago |
| Comment | Post #296648 |
@#36396 The "mutex" is rather a feature in this case, meaning that the caller won't need to worry about it manually. Anyway, the question and this answer is about C. If you write assembler then obviously you have a whole lot more options, including guaranteed atomicity of operations, which is alw... (more) |
— | 4 days ago |
| Edit | Post #296662 | Initial revision | — | 4 days ago |
| Answer | — |
A: Embedded C - memory pool types The TL;DR is: use the correct tool for the task. Dynamic memory allocation in the context of embedded microcontroller system is indeed pretty much never the correct tool, for a long list of reasons. Ask yourself what your actual task is - what is the reason you need dynamic memory? If the answer... (more) |
— | 4 days ago |
| Comment | Post #296654 |
If you wish to use dynamic memory allocation in an embedded system then you have to address _all_ of the concerns in this list: [Why should I not use dynamic memory allocation in embedded systems?](https://electrical.codidact.com/posts/286121) Some of them are pretty much impossible to argue agai... (more) |
— | 4 days ago |
| Comment | Post #296595 |
@#53937 I don't get why you are so hung up on lvalue conversion if you now agree that the operand must be evaluated for side effects. The question was never if the left operand undergoes lvalue conversion or not. You started this whole comment thread with "`=` doesn't evaluate the left operand". (more) |
— | 8 days ago |
| Comment | Post #296645 |
In a professional context, floating point should never be used for currency, because of floating point calculations. [Why is it considered bad practice to use float for representing currency?](https://software.codidact.com/posts/284175) (more) |
— | 8 days ago |
| Edit | Post #296648 |
Post edited: |
— | 8 days ago |
| Edit | Post #296648 | Initial revision | — | 8 days ago |
| Answer | — |
A: Declaring a callback function in `usart.h`, using it in `usart.c` but defining it in `main.c` Indeed this is not a good design since it doesn't use private encapsulation. It is using a global variable which means it is pretty much by definition spaghetti code. I don't see why you would use callbacks in this case either, since the copying of data is no business of the program outside the ... (more) |
— | 8 days ago |
| Comment | Post #283890 |
@#54288 The easiest is to use caller allocation if possible. It won't be possible for opaque types though, then you'd use some manner of memory pool instead. [Example](https://stackoverflow.com/a/70667901/584518). You can have one memory pool per object type or a shared one between all such objec... (more) |
— | 8 days ago |
| Comment | Post #296595 |
To illustrate:
```
#include <stdio.h>
int x;
int* func (void)
{
puts("evaluated");
return &x;
}
int main()
{
*func() = 1;
sizeof(puts("not evaluated"));
}
```
This code prints "evaluated" but it does not print "not evaluated". (more) |
— | 8 days ago |
| Comment | Post #296595 |
@#53937 6.5.17.1 "The type of an assignment expression is the type the left operand would have after lvalue conversion." This is a bit ambiguous, but the rest of that paragraph is not: "The evaluations of the operands are unsequenced." So the left operand is evaluated. Constraints already states ... (more) |
— | 8 days ago |
| Comment | Post #290881 |
@#54288 Generally no, you just clutter down the tag namespace for no good reason. Though it _can_ make code more compact in some special case situations like self-referencing structs. Consider a linked list: `typedef struct node { int data; struct node* next; } node_t;` versus the more hard to re... (more) |
— | 9 days ago |
| Comment | Post #296588 |
@#119116 Annex J is not normative hence the text "informative" in the title. And it is also well-known that Annex J is full of defects everywhere. Also the bullet in C23 J2 §11 is about something else entirely, namely the special case addressed in my linked SO post: when you have an automatic sto... (more) |
— | 11 days ago |
| Comment | Post #296595 |
@#53937 No not that text. C23 6.5.17.1 "The evaluations of the operands are unsequenced". There was a recent question on SO regarding if the left operand of = is evaluated and my answer [here](https://stackoverflow.com/a/79998572/584518) explains why. If it isn't enough that the standard explicit... (more) |
— | 11 days ago |
| Edit | Post #296624 | Initial revision | — | 14 days ago |
| Answer | — |
A: Checking if an array is partially initialized at compile-time Disclaimer: There's just so many clever tricks the programmer can use to save the code from themselves: from off-by-1 bugs or code repetition, or whatever one tries to avoid. In the end - if the programmer doesn't know or isn't paying attention to what they are doing, no clever tricks or tools w... (more) |
— | 14 days ago |
| Edit | Post #296595 |
Post edited: |
— | 17 days ago |
| Comment | Post #296588 |
Regarding the C++ standard, you found that one "unrelated" place I found too and mentioned in a previous comment. The wording "may" is important, it doesn't say will or shall etc. But this whole section is not really important either since this isn't the part of the C++ standard defining scope, l... (more) |
— | 17 days ago |
| Comment | Post #296588 |
The example code in 6.5.3.6 and theory of what would happen if goto was replaced by a loop is more of the same, a (compound literal) object going out of scope in that scenario. This isn't relevant to the question what happens with the pointer itself. The theoretical, convoluted example becomes UB... (more) |
— | 17 days ago |
| Comment | Post #296588 |
The quoted 6.2.4 from C23 goes "If an object is referred to outside of its lifetime, the behavior is undefined" - that refers to a value stored on the heap after it was freed, or a local variable going out of scope. Nothing strange. "If a pointer value is used in an evaluation after the object th... (more) |
— | 17 days ago |
| Comment | Post #296587 |
@#53937x Trap == non-value representation. Reading a value which is a trap representation _is_ undefined behavior, nobody argued about that, but reading a value which is _indeterminate_ does not necessarily mean you are reading a trap representation. More likely you are just reading unspecified g... (more) |
— | 22 days ago |
| Comment | Post #296600 |
Overall the rationale for adding auto is as described in my post and this is very well-documented, so there isn't even any argument about it. Pick your favorite C++ guru and go read a blog from them around the C++11 era. For example [Stroustrup about auto](https://www.stroustrup.com/C++11FAQ.html... (more) |
— | 22 days ago |
| Comment | Post #296600 |
Similarly, lambdas didn't exist either. You seem overall very confused about the order when things were added to the C++ language. (more) |
— | 22 days ago |
| Comment | Post #296600 |
`auto` was changed in C++11. Before that we had C++03.
"But you could also just make an alias using it = std::vector<std::string>::iterator;, then do this: using it = ..."
No you couldn't, that feature wasn't around in C++03. You cannot justify the reason for a language change with a featu... (more) |
— | 22 days ago |
| Comment | Post #296586 |
https://software.codidact.com/posts/288017 (more) |
— | 22 days ago |
| Comment | Post #288043 |
6.2.6.1 §5 is about what happens if something happens to be a trap representation. But nothing in the standard ever claimed that an indeterminate value _must_ be a trap representation. Rather, it is bloody unlikely not to be a trap representation since there was no lvalue access changing the poin... (more) |
— | 22 days ago |
| Comment | Post #296588 |
It doesn't matter if you cast because when doing so, the conversion reads the value of the pointer and if that was a trap representation you still access the pointer. Very likely it will make no difference as many systems do not trap in this scenario. I think some like Power PC and MIPS might do ... (more) |
— | 22 days ago |
| Comment | Post #296588 |
As for pre C++26, I don't think it says anywhere that reading indeterminate values are UB either. Nowhere in C++11 does it say so. At one (unrelated) place it notes that reading an indeterminate value _may_ cause UB but that's it. So I believe this answer would still be wrong if the question was ... (more) |
— | 22 days ago |
| Comment | Post #296587 |
Also the OP has already done what all you didn't: quoted the relevant passage in the standard, which remains the same in ISO 9899:2024 6.2.4. The other relevant part being terms in 3.23: "**indeterminate representation** object representation that either represents an unspecified value or is a no... (more) |
— | 22 days ago |
| Comment | Post #296587 |
@#119299 Notably he never said **where in the C standard this is claimed to be UB**. Simply because no such section in the C standard exists and never existed either. (more) |
— | 22 days ago |
| Comment | Post #296587 |
@#53937 No, there is nothing in the C standard claiming that this is UB. There are various corner cases regarding trap representations and uninitialized variables with automatic storage but they do not apply here. (more) |
— | 22 days ago |
| Comment | Post #296587 |
"What happens to the memory after that is not your business" is irrelevant, the OP isn't accessing the memory. They are accessing the pointer address itself.
"Yes! That's the point of deallocating memory. You are saying that you're done with it." The pointed-at memory, yes. The pointer itself,... (more) |
— | 22 days ago |
| Edit | Post #296595 | Initial revision | — | 22 days ago |
| Answer | — |
A: Is it UB to read value of heap pointer after freeing? It may or may not be undefined behavior: - Depending on if the ABI of the given target system has trap representations for pointers and the value we ended up now happens to be a trap representation. - Otherwise it is unspecified behavior and will work fine, but print garbage. (C23 renamed trap ... (more) |
— | 22 days ago |
| Comment | Post #296588 |
@#119116 "The rationale: Reading an indeterminate value is always UB." Your rationale is wrong and you don't have any trustworthy sources for it. (more) |
— | 22 days ago |
| Comment | Post #296588 |
The question isn't about C++ and you will not be able to come up with a trustworthy source that verifies that "Any read of an indeterminate value is undefined behaviour" in C. It depends on the circumstances. It was never implementation-defined behavior either, it was always _unspecified_ behavior. (more) |
— | 22 days ago |
| Edit | Post #291982 |
Post edited: Grammar fixes |
— | 25 days ago |
| Comment | Post #282566 |
@#53937 No, when reading _streams_. They are usually text, but not necessarily sanitized. It could be a file, command prompt input or incoming data from a serial port. Quite likely unknown amounts. What am I to do when I want to verify a chunk of text of an unknown amount? Well, first of all I pr... (more) |
— | about 1 month ago |
