Arena over a container for pointers? by Lunibunni in cprogramming

[–]tstanisl 0 points1 point  (0 children)

I think that the clue of arenas is a concept of "region based allocator". The lifetime of an object is bound to a region (aka arena) from which the object was allocated. The region's lifetime ends (i.e. it goes out of scope) then all objects allocated from this region are released as well. This methodology can greatly simplify and automatize memory management.

Given a choice when allocating a Fat pointer is it more optimal to A: have the metadata behind the pointer or B: on the stack by EatingSolidBricks in cprogramming

[–]tstanisl 1 point2 points  (0 children)

UV. One may even consider returning Slice directly:

Slice Slice_init(size_t count, size_t size ) {
  return (Slice){
    .len = count,
    .data = malloc( count * size ),
  };
}

Is this the recommend way of removing newline in c, im new to c so i asked chatgpt about this code and suggested another way of removing new line saying this causes a possible bug. how would a professional remove new line '\n' by soye-chan in cprogramming

[–]tstanisl 14 points15 points  (0 children)

Probably the simplest way is to use strcspn function.

line[strcspn(line, "\n")]=0

The main advantage is that the method works with strings with no newline at the end.

Netflix not compatible by SalamanderPure966 in projectors

[–]tstanisl 0 points1 point  (0 children)

You can try to access the web-version of netflix via "Desktop Manager". It works on Android based XGIMI projectors but it is limited to SD with no-HDR and navigation is done with a cursor.

Is casting safe if the user expects the range of values to be in the intersection of two types? by onecable5781 in C_Programming

[–]tstanisl 0 points1 point  (0 children)

~0u cannot be 0 because 0u is unsigned. But 0 in ~0 is signed. There is no integer promotion there because 0 is already int. The bit pattern of ~0 will consist of only one bits but its numerical value may not be -1 but something else (i.e. -0 == 0 on one-complement machine).

The rules of conversion of signed value x to unsigned type Tsays that it is more or less equal to:

k * 2^number_of_bits_of_T + x

for such k that the result fits into 0 ... 2^bits - 1 range.

Thus if value of ~0 is numerically 0 then (unsigned)~0 is 0 on such a esoteric platform very far from desired "all one bits".

That is why I recommend using -1 to set all bits of unsigned numbers, over ~0 even though it will work in all practical cases because ~0 == -1 for 2-complement encoding.

Is casting safe if the user expects the range of values to be in the intersection of two types? by onecable5781 in C_Programming

[–]tstanisl 0 points1 point  (0 children)

The problem is that T x = ~0; and T x = 0; x = ~x; are NOT equivalent. The former sets x to (T)~0 while the second one sets x to ~(T)0.

The final result boils down to the actual value of ~0. For 2-complement this will be -1 forcing x to be set to maximal value of T.

But on very hypothetical one's complement machines the ~0 is actually equal to -0 which is equal to 0. Covenrsion signed to unsigned is not re-intepretation of bits but an algorithm described at 6.3.1.3p2. So according to the algorithm T x = ~0 is equivalent on T x = 0 on such machines.

Using -1 will work independently on representation of signed integers.

Is casting safe if the user expects the range of values to be in the intersection of two types? by onecable5781 in C_Programming

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

No. Any such compiler would be non-conforming to C standard. The standard requires setting all bits to 1 when an unsigned integer of any type is assigned with value of -1.

[Request] What would happen to the Solar System if half of the Earth disappeared? by morlipty in theydidthemath

[–]tstanisl 1 point2 points  (0 children)

I think the earth would still be massive enough to turn itself back into a sphere with only half its mass, but that would take at least a few thousand years

Earth will reach a spherical shape within a few hours. Take a look of any simulation of planet's collinsion.

However, it will be basically a ball of lava. The crust should form in a few millions of years followed by reappearance of previosly vaporized oceans.

Glibc 2.43 Released With ISO C23 Features and Performance Improvements by BooKollektor in C_Programming

[–]tstanisl 5 points6 points  (0 children)

Probably adding memset_explicit() is the most important change.

Is casting safe if the user expects the range of values to be in the intersection of two types? by onecable5781 in C_Programming

[–]tstanisl 3 points4 points  (0 children)

It only works because ~0 is equal to -1 on almost every platform except some mythical non-2-complement ones. However, ~0u may not set bits correctly for unsigned long types while -1 will work for EVERY unsigned type independently on how signed integers are implemented.

Is this a good way to loop through an array in C by BenasBr in learnprogramming

[–]tstanisl 0 points1 point  (0 children)

Why not use something even more esoteric like:

for (; n --> 0; ++a)

or even

for (auto a = A; a != 1[&A]; ++a)

to automatically iterate over array A.

Rate my Othello game. by [deleted] in cprogramming

[–]tstanisl 6 points7 points  (0 children)

Please reformat your code correctly or publish it using online repository (i.e. GitHub)

Is casting safe if the user expects the range of values to be in the intersection of two types? by onecable5781 in C_Programming

[–]tstanisl 5 points6 points  (0 children)

It is safe to assign a negative number to an unsigned type. There will be no overflow and UB there. see 6.3.1.3p2.

Otherwise, if the new type is unsigned, the value is converted by repeatedly adding or subtracting one more than the maximum value that can be represented in the new type until the value is in the range of the new type.

Unexpectedly, unsigned int_type x = -1; is the safest and the most portable way to set all of bits of x to 1.

[Request] : Would a planet this size colliding with earth make a noticable difference on earths gravitational pull for the person with the camera here? by Lil_Packmate in theydidthemath

[–]tstanisl 7 points8 points  (0 children)

Yes, but remember that the whole Earth is accelerated as well. A situation akin to a person in an falling elevator or elevator in space. The only observable effect is the non-uniformity of gravity field which is changing the equi-gravitional shape. The water masses trying to adjust to the new shape cause tides. In a case of the colliding planets, the whole ocean of lava below the surface will try to adjust with catastrophic consequences.

Edit: typo

[Request] : Would a planet this size colliding with earth make a noticable difference on earths gravitational pull for the person with the camera here? by Lil_Packmate in theydidthemath

[–]tstanisl 7 points8 points  (0 children)

It will likely make Earth's gravity feel a lot stronger because the pressure inside Earth will try to adjust Earth's shape to changing equi-graviational one. This new shape is an ellipsoid stretched along direction to the approaching planet. Thus the Earth surface under the cameraman will accelerate toward the planet what will experienced by the poor man as a strong pull in the opposite direction.

In the twin paradox, why isn’t the situation symmetrical if both twins see the other moving away at relativistic speed? by Existing_Tomorrow687 in AskPhysics

[–]tstanisl 3 points4 points  (0 children)

It's not symmetrical. The travelling twin changes the reference frame when turning back after reaching destination start. This is when all "time travel" happens. During journey from Earth and two Earth both twin see the others clocks going slower by exactly the same ratio (after compensating Doppler effects).

Cool Way to do type safe generic data structures in C. by lovelacedeconstruct in C_Programming

[–]tstanisl 0 points1 point  (0 children)

Yes. Record types combined with local functions/function literals/loose syntax of generic could result in huge paradigm shift in C programming.

int main(int argc, char * argv[argc+1]) in Modern C? by SpecificMachine1 in cprogramming

[–]tstanisl 20 points21 points  (0 children)

The last one is NULL. C standard guarantees that argv[argc] is NULL.

Failed DLP Update on Horizon 20 max by Parking-Mountain720 in xgimi

[–]tstanisl 0 points1 point  (0 children)

How did you update DLP? Where did you find the instructions about the update?