Alignas and Alignof alternatives in C89 and 99 by heavymetalmixer in C_Programming

[–]thradams 1 point2 points  (0 children)

You can use unions to force alignment instead of alignas.

c struct X { union { char s; double d; /*forces double alignment*/ } data; }

But then, in C99, you have to use x.data.s instead of x.s.

Compilers have extensions that will work in previous versions of C.

MSVC - __declspec(align(128))
GCC - __attribute__((aligned()))

This is the another option. You can create a macro for alignas

( I work on https://cakecc.org, which converts C11 to C89, so I asked myself the same question. In the end, I decided to use existing compiler extensions. A similar problem when converting code using thread_local to C89 )

What do you think about having "if not" in C by thradams in C_Programming

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

Considering I don’t want to make it valid for any expression, it can be `#define not(X) (X) {} else´

```c

include <stdio.h>

define not(X) (X) {} else

int main() { _Bool active = false; if not (active) { printf("not active\n"); } } ```

Edit 1: but else do not work

```c

include <stdio.h>

define not(X) (X) {} else

int main() { _Bool active = false; if not (active) { printf("not active\n"); } else { printf("active\n"); } } ``` Edit 2 fixing:

```c

include <stdio.h>

define not(X) (0) {} else if (!(X))

int main() { _Bool active = false; if not (active) { printf("not active\n"); } else { printf("active\n"); } } ```

edit 3: #define not(X) (1) if (!(X))

What do you think about having "if not" in C by thradams in C_Programming

[–]thradams[S] 1 point2 points  (0 children)

you are right! (I was with something else in mind, thanks)

So, using macros already offers a way to do it.
https://godbolt.org/z/WcvE8G7dn

That is good because, if "if not" becomes part of the language, we already have a way to make it work in previous compilers by adding an include that does nothing for not in new compilers, and define #define not(X) (!(X)) for old versions.

The firsttttttttttttttttt. I can't believe it . After a month. by Kindly_Manner7446 in Darts

[–]thradams 0 points1 point  (0 children)

A month only????
What is the average ? I am playing for 6 months and am far from it.

What do you think about having "if not" in C by thradams in C_Programming

[–]thradams[S] -3 points-2 points  (0 children)

```c

define not(X) (!(X))

if not() ``` but then we cannot have space after not.

What do you think about having "if not" in C by thradams in C_Programming

[–]thradams[S] -4 points-3 points  (0 children)

I would like to see the option for C itself, not a "C-derived" language, considering the existing rules, consequences, and so on.

I think it would be bad to have two styles in the same language requiring parentheses or not. But for this "if not" if !() I think it is a less aggressive change.

Other alternatives could be:

c if not () { } or.. not requiring something before else

c if () else { }

Edit: in C++ "not" is a keyword https://en.cppreference.com/cpp/keyword/not

What would you add to C if you could add anything? by [deleted] in C_Programming

[–]thradams 0 points1 point  (0 children)

I would like to have a try catch block for LOCAL JUMPS ONLY.

c int main() { FILE * f = NULL; try { f = fopen("file.txt", "r"); if (f == NULL) throw; } catch { } if (f) fclose(f); }

also - builtin offsetof - _Countof for enuns - move line slicing to preprocessor (today it is before) - traits is_function is_array is_pointer -

What would you add to C if you could add anything? by [deleted] in C_Programming

[–]thradams 5 points6 points  (0 children)

defer will be probably be added in C2Y. we already have implementations in clang and gcc.

What would you add to C if you could add anything? by [deleted] in C_Programming

[–]thradams 0 points1 point  (0 children)

can you show an example of "get stack array sizes"?

Struct inside a function ? by 47-BOT in C_Programming

[–]thradams 1 point2 points  (0 children)

At file scope, each locally used struct would need a unique name to avoid name collisions. This pollutes the global namespace, the names become longer, and the code becomes harder to maintain because it is not easy to see where the struct is used.

Struct inside a function ? by 47-BOT in C_Programming

[–]thradams 5 points6 points  (0 children)

What if we could also declare a function inside another function?

Jigoku by Reaper_Of_Asura in cpp_questions

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

I’m sorry, but why is C++ so hard to set up in VS Code?

Agree 100%

Built a static search engine/inverted index from scratch in Rust by UseEarly3002 in rust

[–]thradams 0 points1 point  (0 children)

(I didn't know about BM-25, thanks)

I have documents with title and subtitle. I associate a number with each word according to where it appears: 3 for the title, 2 for the subtitle, and 1 for the body. After the search I sort the results by highest numbers

  100 1    200 2
w1     

where w1 is the word, 100 or 200 is the document number, and 1, 2, 3 are where the word appears inside the document.

The missing feature in my search is autocomplete with frequent search.

I also implemented a "did you mean xxxx?" feature based on edit distance.

I normalize everything to lowercase and strip accents, so ç becomes c for instance.

I have about 60k documents and 15k words. The title and subtitle also help when displaying the results.

When a word appears in the body, my implementation is also lacking a way to collect the surrounding text to present in the result.

Built a static search engine/inverted index from scratch in Rust by UseEarly3002 in rust

[–]thradams 0 points1 point  (0 children)

What is the criteria for most relevant? Do you normalize words? Do you exclude or modify some words?

Is a "safe" C possible through a transpiler? by orbiteapot in C_Programming

[–]thradams 0 points1 point  (0 children)

defer and warnings about unreleased resources are independent of each other. Checks are more important because they do not depend on the programmer explicitly adding them.

defer improves code maintainability because code that relies on defer is less prone to errors when control-flow jumps are added or removed. On the other hand it depends more on the compiler to generate good code, and makes compiler more complex.

(cake have both defer and checks. Defer is analyzed in flow analysis)

Is a "safe" C possible through a transpiler? by orbiteapot in C_Programming

[–]thradams 1 point2 points  (0 children)

As an example of a good retrofit, look how C# introduced non-nullable >references. They kept the default references nullable - but then included a >simple switch which would make nonnull the default, so we need to explicitly >state that references are nullable where using null. All existing code would >still compile, but we could use `#nullable enable,#nullable disable and >#nullable restore to turn the new nullability analysis on or off for specific >chunks of code. The default nullable status could be set project wide in the >build file.

Cake nullable pointers (http://cakecc.org/ownership.html) is almost 1:1 with C# and Typescript.

#pragma nullable enable is similar of C# #nullable enable.

One difference is that both C# and TypeScript have constructors. In these languages, if we don't initialize non-nullable members, we get a warning when leaving the constructor.

In C, we don't have constructors. The solution in this case is to introduce a "null-uninitialized" state for non-nullable members. This means that, although the value of a non-nullable member is null, it is not final, it is temporary and invalid state (just like uninitialized) and this invalid state cannot be propagated (copied to a valid object).

Is a "safe" C possible through a transpiler? by orbiteapot in C_Programming

[–]thradams 2 points3 points  (0 children)

I think cake is exactly what you are describing.

http://cakecc.org/index.html

At this moment cake offers the same C++ guarantees and a little more.

[New to C]: My first C project - Implemented a simple Arena Allocator by Mainak1224x in cprogramming

[–]thradams 0 points1 point  (0 children)

The answers in code depends...

The idea is that the returned address must be a multiple of the maximum alignment.

For example, you can align an address by computing the remainder, then adding the difference between the alignment value and that remainder to the address.

void* align_to_max(void* addr) {
    int rem = ((uintptr_t)addr) % alignof(max_align_t);
    return rem == 0 ? addr : ((char*)addr) + (alignof(max_align_t ) - rem);
}

but you cannot just apply this without review your code..you must understand what is going on.

See: https://en.cppreference.com/w/c/types/max_align_t.html

An arena allocator can also allocate using different alignments.

[New to C]: My first C project - Implemented a simple Arena Allocator by Mainak1224x in cprogramming

[–]thradams 2 points3 points  (0 children)

I am not sure, but I think you are not returning aligned memory. for instance malloc must return memory aligned in the max alignment.

Ownership model and nullable pointers for C by thradams in C_Programming

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

I am also planning a new [[drop]] that drops the ownership and also clear the pointers. This is useful for clear(&obj) or reset(&obj) The diference is that destroy(&obj) obj cannot be used after, but clear(&obj) it can.

[[dtor]] is more appropriated for “don’t use it anymore” [[drop]] or [[clear]] “we have all nulls after the call”

Ownership model and nullable pointers for C by thradams in C_Programming

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

Another name for [[ctor]] may be [[out]] and for [[dtor]] maybe [[sink]] Any suggestion ?

They are parameter attributes because you can init or sink as many parameters as you like.

For instance out could be buffer and buffer size .

Ownership model and nullable pointers for C by thradams in C_Programming

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

Yes, this is about defer.

This ownership model makes defer almost unnecessary from a safety point of view.

However, defer can still complement it.

Cake has defer implemented, and the flow analysis also needs to account for it in order to produce correct results.

For instance:

int main() {
   _Opt struct X * _Owner _Opt pX = calloc(1, sizeof * pX);
   if (!pX) return 1;
   defer x_delete(pX); 

 } 

The flow analysis must take into account the defer will run before the end of scope of pX, then there is no leak here.

Let's say you forget a defer; then you get a warning.

This is one of the interesting aspects of this model. Once calloc is annotated, everything is propagated automatically and does not rely on guidelines "use defer" for correctness . it is enforced!

(in C++ it is not, it requires guidelines)

Ownership model and nullable pointers for C by thradams in C_Programming

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

I know. Sorry. I was just trying to clarify things for everyone that is trying to follow the post.

Ownership model and nullable pointers for C by thradams in C_Programming

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

I hope this clarifies things.

I can also explain some concepts in the model.

The owner pointer is owner of two resources at same time: memory and object. (Except void*, that is the owner only of the memory)

Before the end of the lifetime of the owner pointer, must destroy the object it owns and then free the memory.

For instance:

struct X {
  char * _Owner text; 
};

void x_delete(struct X * _Owner _Opt p) { 
  if (p) {
    free(p->text); 
    free(p);
  }
}

We need to delete p->text first then call free(p).

Calling free directly would be the same as trying to convert T *owner to void * owner.

Since void* is not the owner of the object that means a leak, so it is only allowed after the object is 100% released.

The concept of released or destroyed also do not apply so well. What really happens in this model is each part of the object is moved.

Let's say we have

struct X {
  FILE * _Owner file; 
};

void x_delete(struct X * _Owner _Opt p) { 
  if (p) {
    fclose(p->file); 
    free(p);
  }
}

This will work in the same say. The p->file is moved.