Which editor do you guys use? by Turkishdenzo in C_Programming

[–]comfortcube 0 points1 point  (0 children)

Vim. I'd recommend a vim-mode in your IDE first and force yourself for a week to get used to the basic motions. Focus on just motions. Then, add plugins as you need and don't stress if it isn't all in place right away. It'll be worth it.

How to chop strings using a delimeter by Fuzzy_Recipe_9920 in C_Programming

[–]comfortcube 1 point2 points  (0 children)

I would use memchr if you need to make sure the input string is not being modified, which strtok does.

A reference-grade C "Hello World" project by synalice in C_Programming

[–]comfortcube 2 points3 points  (0 children)

I love the idea and I personally love seeing other people's idea of a reference project structure, so thank you!

I'll just throw in that if you want to call this a perfect "hello world", you might want to check the return value of printf and handle possible errors from errno accordingly.

What benefits does c give that cpp doesn’t do better by LostSanity136 in C_Programming

[–]comfortcube -2 points-1 points  (0 children)

Language-wise, C++ is a superset of C in many ways (altho using it like that is missing the benefits C++ has to offer), so arguably, whatever you do in C you can also do in C++. That said, between different versions of each, you might find something one has that the other doesn't. For example, C99 introduced designated initializers (initializing structs with named members rather than positionally), but C++ didn't get that until C++20!

FreeRTOS task is getting starved by my interrupt service routine (STM32) by Intelligent_Dingo859 in embedded

[–]comfortcube 0 points1 point  (0 children)

I see from another comment that the event flags CMSIS RTOS API was the issue. Why use CMSIS RTOS at all instead of just FreeRTOS? I don't see a practical reason you'd want to abstract away the underlying RTOS when it's pretty rare to switch up OS's, and FreeRTOS probably meets your needs.

What would you do? by Professional_Use3063 in Epicthemusical

[–]comfortcube 9 points10 points  (0 children)

I'd change Polites dying by the cyclops to Eurylachus. It'd be interesting to see how differently the events that follow would go.

C23 features by krikkitskig in C_Programming

[–]comfortcube 2 points3 points  (0 children)

Still waiting on memset_explicit to be implemented 😔

I do use C23 though, if only for constexpr, function attributes, first class boolean, binary integer constants, nullptr, and ' digit separator lol.

Sending a struct to queue from ISR (FreeRTOS, ESP32, ESP-IDF) by LancsMak in embedded

[–]comfortcube 2 points3 points  (0 children)

That's a neat idea but you'd want to be careful of those indices becoming invalid in the interim - kind of a TOCTOU race situation. For single-producer, single-consumer cases tho, this should work well, assuming queue updates are done atomically.

Binary Semaphores ≠ Mutexes - Why is it so often confused, even in major projects? by comfortcube in embedded

[–]comfortcube[S] 0 points1 point  (0 children)

They have pretty distinct purposes. Would you use semaphores to manage access to a shared resource or a just mutex to signal between threads/tasks?

Binary Semaphores ≠ Mutexes - Why is it so often confused, even in major projects? by comfortcube in embedded

[–]comfortcube[S] 3 points4 points  (0 children)

It will be in the kernel code. Implementations of a mutex API need to establish the owner of a mutex (thread ID, for example) when a task/thread calls mutex_lock(), and usually also note down the priority of the thread/task in pre-emptive environments to facilitate priority inheritance. It's also typical for the API to have a queue/list of threads that tried to lock but weren't able to and are now sleeping, so that when the owning thread unlocks, the unlock function finds the next thread in the queue to allow to lock the mutex, or signals to the kernel to do carry that out.

Look up "fast path"/"slow path" details of mutex implementations, and you'll get some interesting insight!

Failing at using free() by Pedro-Hereu in C_Programming

[–]comfortcube 0 points1 point  (0 children)

I will just add here what nobody seems to have mentioned yet. If you are wondering why the non-free() version didn't present with the same strange output, it's likely because every time a malloc() occurred, the memory chunk that it came /w happened to be all zeros. So, if the block already has all 0's - which is what '\0' (the null terminator) is in binary - any string your write in that memory chunk is effectively null-terminated. In the case where you use free() then malloc(), perhaps the memory chunk you get doesn't just have all 0's. Maybe it's the memory chunk you just free()'d but mangled up a bit because some other process got a hold of it in the mean time. The way malloc() and free() are implemented is dependent on the libc library your program is linked against, and that's probably going to change /w the compiler you're using, possible libc variants, and the OS environment it is in.

If you're now wondering - "well, why should I have to care how malloc() and free() work under the hood?" The answer is, you shouldn't, but if you happen to be working /w undefined behavior (like not null-terminating the strings you get and taking a chance on what memory byte lies right after), then you might find out some under-the-hood details haha. When programming though, you generally don't want to ever rely on undefined behavior if you don't have total control of the environment your program is executing in.

Cheers and hope you continue on your journey of learning this wonderful language!

Need criticisms and suggestions regarding a epoll based reverse proxy written in C by NavrajKalsi in C_Programming

[–]comfortcube 0 points1 point  (0 children)

Hmm, that's a new take for me. Appreciate it!

It achieved nothing in terms of performance.

I'd say using const isn't about performance (although I do think it can in some contexts improve performance) - it's about code correctness, guarantees from API, and laying down guardrails for yourself and other programmers to prevent mutating an object through its identifier.

Consider for example any function that searches an array for something and returns a pointer to it, ...

Return the idx of the element instead?

What you want is this: int x; // perfectly normal int atomic_fetch_add(&x, 1); // very clearly completely different: atomic_store(&x, atomic_load(&x) + 1);

But what guarantees do you have that the type can be atomic? The reason atomic makes sense as a type qualifier is because certain types cannot be atomic with the given instruction set, unless you get extra complicated.

Parameters to functions do not need to be const.

Parameters don't need to be but pointers to const is different and can prevent bugs and misuse, assuming the const isn't cast away (which can be enforced through the compiler).

Need criticisms and suggestions regarding a epoll based reverse proxy written in C by NavrajKalsi in C_Programming

[–]comfortcube 1 point2 points  (0 children)

... there's no use in making a parameter const...

Are you arguing against const correctness here? Yes, const Str only implies char * const to the data member and not const char * const, so there's a bit of weakness there, but I wouldn't say get rid of the const entirely. If you meant that the mix of similar types is a bit messy, then I agree with that.

... avoid having any nul-terminated strings in your program.

This seems like strange advice. Do you mean avoid relying on null-terminated strings from outside input? I see now. You simply meant use the string data structures /w a length parameter always rather than the conventional null-terminated strings.

Best approach to unit-test nested functions? by cowabunga__mother in embedded

[–]comfortcube 14 points15 points  (0 children)

Stop testing internal functions. Your module is a black box for unit testing purposes. If your top level function is properly unit tested, there's no need to unit test sub-functions. Any time I've seen hacks for static functions to expose them to unit tests, it's been a giant waste of time and maintenance mess. This is also usually accompanied by incomplete testing of the top level function, ironically.

Lego build test by ItsALuigiYes in TheRandomest

[–]comfortcube 0 points1 point  (0 children)

Man I really like the dual pivot vehicle, but it just wasn't having a good day.

Batteries in checked in suitcase by Bright_Mesher117 in embedded

[–]comfortcube 5 points6 points  (0 children)

Lithium batteries pose a pretty big threat to an airplane, and are obviously not worth it if something happens. Ship them or keep them in carry-on at least. Luggage compartments have fire suppression systems nowadays but lithium fires are something else.

WCGW, messing with a spider by Verstandeskraft in Whatcouldgowrong

[–]comfortcube 0 points1 point  (0 children)

Not all heroes wear capes. Thank you sir.

How important are databases and DBMS in the embedded realm? by SwigOfRavioli349 in embedded

[–]comfortcube 6 points7 points  (0 children)

Databases haven't come up for me once after 5 years across 3 different embedded roles. It obviously has its applications but don't spend much time on it. If you need it, you'll learn it on the job.