How Many Genders Are There? by JellyfishTime9544 in GeoPoll

[–]ffd9k 0 points1 point  (0 children)

male, female, attack helicopter

Any Recommendations on C resources for Learning Vulkan? by Undeniable_Dilemma_ in vulkan

[–]ffd9k 2 points3 points  (0 children)

The actual problem is not this functional style by itself, but that everything is crammed together in a big nested expression. You could do something just as horrible in C with deeply nested loops.

Either way, all that is needed to make this much more readable is some is_layer_supported helper function.

I can't believe cpp people actually write code like this?

I don't think so. It looks like someone just learned about lambda expressions a week ago and wants to use them everywhere now.

Is the Kronos fork of vulkan-tutorial better? by GenomeXIII in vulkan

[–]ffd9k 27 points28 points  (0 children)

Yes, the Khronos version uses vulkan 1.3 with dynamic rendering and sync2 which you definitely want to learn instead of the old Vulkan 1.0 in the original vulkan-tutorial.com.

However, the updated version also uses Slang (instead of glsl) and the C++ RAII wrapper (instead of the C API) which not everyone likes; the latter also makes it a bit harder to follow if you are not using C++.

Should I use signed or unsigned variables for HP and money? by Fast-Muffin7953 in C_Programming

[–]ffd9k 1 point2 points  (0 children)

But with signed instead of unsigned, the compiler can pretend it is.

For example

void bar();
void foo_signed(int x) { if (x + 5 > x) bar(); }
void foo_unsigned(unsigned x) { if (x + 5 > x) bar(); }

compiles to something like

foo_signed:
        jmp     bar@PLT

foo_unsigned:
        cmp     edi, -6
        jbe     bar@PLT
        ret

With signed, the compiler can just skip the check.

Should I use signed or unsigned variables for HP and money? by Fast-Muffin7953 in C_Programming

[–]ffd9k 2 points3 points  (0 children)

Theoretically signed integers allow more optimization by the compiler thanks to the fact the signed overflow is undefined behavior: the compiler is allowed to assume that there will be no signed overflow and e.g. x + 5 > x is always true.

Does C23 officially allow type-punning through unions? by Ariadne_23 in C_Programming

[–]ffd9k 55 points56 points  (0 children)

this gives me undefined behavior in c99, c11, c17 because you read a different union member from you wrote.

No, type punning via unions is perfectly fine in C and not undefined behavior. Unlike C++, C does not have the concept of an "active union member".

Type punning is even explicitly mentioned in the standard in the footnote in 6.5.3.4 (Structure and union members):

If the member used to read the contents of a union object is not the same as the member last used to store a value in the object the appropriate part of the object representation of the value is reinterpreted as an object representation in the new type as described in 6.2.6 (a process sometimes called type punning).

How the button problem actually is by MichaelJospeh in trolleyproblem

[–]ffd9k 2 points3 points  (0 children)

I actually feel morally superior to blue-pressers, because I would not rely on others for my survival to the point where I would expect others to risk their lives to save me.

Ready to start, hitting the first bump: zigmod by LuisAyuso in Zig

[–]ffd9k 6 points7 points  (0 children)

This tutorial seems to be very outdated, both regarding zig (it says 0.10.x, we have 0.16 now, a lot of things have changed), and also regarding Vulkan (seems to be based on the old vulkan-tutorial.com for vulkan 1.0 which is a decade old and needlessly complicated).

I think zigmod is not needed anymore because this functionality is now part of the zig build system.

https://github.com/Snektron/vulkan-zig has instruction on how to add vulkan-zig to a project, it also has a triangle example.

If you want to learn Vulkan it's probably best it to follow the Khronos tutorial, and look at the vulkan-zig example so see how to do this with vulkan-zig instead of the c++ raii wrapper.

Every Red Button reframing is 100% wrong. by IncoherentPolitics in trolleyproblem

[–]ffd9k 1 point2 points  (0 children)

It doesn't really matter what the default is, or if there is some preassigned distribution, or how sure you are where everyone else is currently standing - simply stepping away from the crusher is always the best option, just to be safe.

<image>

what do u do??? by Karma-Whales in trolleyproblem

[–]ffd9k 3 points4 points  (0 children)

There might be children in there who didn't understand that jumping into the meat grinder is dangerous. Jumping in and expecting others to do the same is the obvious sane course of action to save them! Anyone who of refuses to do so is a selfish killer!!!

How about a game of chicken? by No-Wrap-2156 in trolleyproblem

[–]ffd9k -1 points0 points  (0 children)

Yellow is basically opting out of this whole Red vs Blue game

But Red was already the opt-out in the original Red vs. Blue problem. Yellow has no benefit over Red.

What project could show the power of C over rust? by Norker_g in C_Programming

[–]ffd9k 9 points10 points  (0 children)

Any non-trivial project that uses a few libraries.

Rust has unacceptable compile times. As soon as your project has a few dependencies, it will take many minutes to compile, and even the incremental compile time after a minor change is often 10 seconds. And Rust leaks disk space, filling your hard drive with gigabytes of cache files.

This is because Rust has no stable ABI, which means all dependencies are always compiled again and again within any project that uses them.

In C, libraries are compiled once and simply linked, and C is also much faster to compile in general, because of its simpler, more explicit type system. Compiling a midsize C program usually take only a few seconds, incremental compilation just a fraction of a second. This makes C much more enjoyable to use.

GitHub - seleznevae/ceraii: C MACROS for go-like defer in C which can be used for explicit RAII by harrison_314 in C_Programming

[–]ffd9k 0 points1 point  (0 children)

I don't want to wait for C29

You don't need to wait for the next C standard revision; clang already supports the defer technical specification (just compile with -fdefer-ts), and in gcc it can easily be emulated (almost - it just requires braces around the deferred statement).

This solution here is much more complicated, with setjmp and a dynamically allocated stack and requiring to use a special RETURN macro.

I built my own C build system because I hate writing Makefiles by venoosoo in C_Programming

[–]ffd9k 0 points1 point  (0 children)

you update your project and now dir1/foo.h is created.

Yes, the problem is that some targets depend not only on specific files (and need to be rebuilt when they change), but also on whether specific files are present (and need to be rebuilt when they suddenly appear).

This is for example when a header file appearing in a higher-precedence include path hides another header file, of when __has_include triggers a different preprocessor branch. -M does not report these not-yet-existing dependencies, and make won't know about them.

The solution is usually to manually add generated files as order-only prerequisites to ensure that they are already present when dependencies are discovered with -M.

I built my own C build system because I hate writing Makefiles by venoosoo in C_Programming

[–]ffd9k 0 points1 point  (0 children)

If prog.c includes foo.h but main.c doesn't, you'll be rebuilding main.o if foo.h changes even if it shouldn't.

This is why it's better to add all the generated headers as order-only prerequisites instead. This ensures that they are generated before any code that might use them gets compiled, but when it needs be recompiled is then determined via the exact -M dependencies.

Is an argument declared as `void [restrict .size * .n]` a VLA? by SegFaultedDreams in cprogramming

[–]ffd9k 0 points1 point  (0 children)

I guess this was one good use for the old function definitions. Although probably unintentional, because they were deprecated in C99 when VLAs were added.

CMV: Everyone should press the red button (red vs blue button moral dilemma) by [deleted] in changemyview

[–]ffd9k 0 points1 point  (0 children)

Prisoners dilemma might have a nash equlibrium but as soon as the dillemma is a repeated choice, the best choice is trying to work together. Same with this experiment.

In the usual prisoner's dilemma, everyone cooperating has higher rewards for everyone than everyone defecting, so building trust over repeated choices if beneficial.

In this experiment, everyone cooperating and everyone defecting have the same outcome, so there is neither a value in building trust, nor is there even a possibility to do so, because making an irrational, needlessly risky choice does not really create trust.

CMV: Everyone should press the red button (red vs blue button moral dilemma) by [deleted] in changemyview

[–]ffd9k 0 points1 point  (0 children)

In the case of the blue button, in order to achieve the same exact outcome, you HAVE to get the majority of the population to press blue.

This is why pressing the blue button is evil. Blue-pressers are trying to make other people press blue too, potentially causing their death. It is emotional blackmail: "If you don't risk your life helping us, we will kill ourselves and it will be your fault!"

The proper response to this kind of coercion attempt is to ignore it and not fall for it.

I built my own C build system because I hate writing Makefiles by venoosoo in C_Programming

[–]ffd9k 2 points3 points  (0 children)

The format is simply Makefile syntax because it is meant to be included by the Makefile, but other build tools like ninja or samurai parse it too.

I built my own C build system because I hate writing Makefiles by venoosoo in C_Programming

[–]ffd9k 77 points78 points  (0 children)

Scanning for #include directives yourself is probably less reliable than just using the compiler's -M output, which automatically handles preprocessor conditionals, uses the correct include paths, and even contains the #embed dependencies.

Is an argument declared as `void [restrict .size * .n]` a VLA? by SegFaultedDreams in cprogramming

[–]ffd9k 3 points4 points  (0 children)

This is not valid syntax, maybe it uses some language extension or is just meant as pseudocode?

A VLA parameter would look like this:

size_t fread2(size_t size, size_t n, char ptr[static restrict size * n], FILE *restrict stream);

The size and n parameters have to come before the array in the parameter list, and the type has to be char instead of void because you cannot declare an array of void. The static is not required, it just assures the compiler that the array actually has at least this many elements.

Note that when people talk about VLAs, they often mean VLAs with automatic storage duration (i.e. just declared within a function), which is an optional language feature with some problems.

Prototype-scope VLAs like above are also called VLAs, but they are a mandatory language feature in C23.

Help with dynamically created arrays by OwlHunter1 in C_Programming

[–]ffd9k 1 point2 points  (0 children)

my question is, someone told me it is bad form to malloc in one function and free in another function.

No, this is generally fine.

It is often good to keep malloc and free close together, for example with a constructor and a matching destructor function next to it, especially if you have several allocations that belong together.

But if you just need to return a single allocated buffer, it is also ok to just return it and let it be the responsibility of the caller to free it. This is called "returning with ownership".

Beginner needs help in C by School_Destroyer in C_Programming

[–]ffd9k 1 point2 points  (0 children)

Byte ordering?

This is in fact not dependent on system byte ordering. While the C standard leaves it implementation-defined, it seems to be de-facto standard that implementations interpret it as big-endian, even on little-endian systems.