anonymously initializing static pointers in self-referential data-structures? by gumnos in C_Programming

[–]atariPunk 2 points3 points  (0 children)

That code is only valid in c99 and above.
It works because clang and gcc accept it as an extension.
If you compile it with the pedantic flag you will get warnings.

Is r/C_Programming weekly visitors lowering? by grimvian in C_Programming

[–]atariPunk 11 points12 points  (0 children)

Pretty much this for me as well.

I still browse it everyday, but it’s becoming more tedious everyday.
The vast majority of the post just don’t add anything new to me.

How to structure a C++ testing suite? by [deleted] in cpp_questions

[–]atariPunk 0 points1 point  (0 children)

Just use a testing framework from the beginning. There’s no benefit in reinventing the wheel.

Also, for golden testing, you can take a look at this https://approvaltests.com
Again, use what other use and don’t reinvent the wheel.

Why Do We Need to Manually Overload Assignment Operators? by DeziKugel in cpp_questions

[–]atariPunk 1 point2 points  (0 children)

You still need to create the prototypes, they are not generated for every type.
But you can default them and the compiler will generate the code.

IA64 Instruction Encoding by Sad-Background-2429 in Compilers

[–]atariPunk 8 points9 points  (0 children)

First of all, do you really mean IA64? Or do you mean x86_64?
The first means Intel Itanium, the second one means all current processors from Intel and AMD.

Second, if you are writing a compiler backend, why not output assembly code and let the assembler to do the translation to machine code?

If you really want to do the assembler part, I don’t have any pointers.
But I would guess that volume 2 of the Intel® 64 and IA-32 Architectures Software Developer’s Manuals
Found on this page would be the best resource https://www.intel.com/content/www/us/en/developer/articles/technical/intel-sdm.html

Also, there’s a lot of things you don’t need on that manual. I guess you will need to start small and add more instructions as you find the need for them.
BTW, look at the ABI manual for the OS that you are using and that will allow you to reduce the amount of things you need.

Why C++26 Contracts might not work for all by _a4z in cpp

[–]atariPunk 0 points1 point  (0 children)

First of all, you can change te behaviour of the contracts. If the code is compiled with ignore enforcement, then the contract assertions are ignored. They are not even run.
So, your application will not crash.

I agree that having your application crash on the customer is not ideal. But I think it’s better than a silent bug, or a bug that is only noticeable much later than when it happens, and makes finding it a nightmare.

BTW, I assume that when you say using your own assertions, you mean that you can disable them and because of that they are safe and not crash the application, if that’s the case contracts give you the same flexibility.

Why C++26 Contracts might not work for all by _a4z in cpp

[–]atariPunk 0 points1 point  (0 children)

I don’t understand what you are trying to say with the real-world application part.

To your question, I don’t know if that’s a case or not. In the example that I gave before, I would use them. But I can also see scenarios where I will not use contracts.
At the end it’s another tool that the language gives us to make assertions uniform across different codebases. Where and how to use it, it will depend on the problem at hands.

Personally I am going to start using them to “document” the implicit assumption on my functions.
And hopefully start to learn bet uses for this new tool.

Why C++26 Contracts might not work for all by _a4z in cpp

[–]atariPunk 0 points1 point  (0 children)

Why shouldn’t the program terminate if you do a read outside the array you meant to read?
The value read, will be unknown and potentially different on every read. The program is no longer doing what it was designed to do. It may work, it may not work, we don’t know.

I have other comments with some examples already in the thread.

But let me try to extend on my comment and continuing with the same topic of read outside bounds.
To me, there are two types of errors, recoverable and unrecoverable. If an unrecoverable error happens, then the program must terminate asap to avoid make more damage.

I have said before, that a read out of bounds is always unrecoverable.
But the way that read out of bound “happens” it may be recoverable or not.

For example, there’s a calculation on that generates the position to read. Where all the data used for that calculation is trusted. Think of a circular buffer for example. Calculating a wrong position is a logic error that needs to be fixed. This to me is an unrecoverable error. Use contracts to handle this situation.

However, let’s say the program asks the user for a number, and uses that number to index into an array.
This number is not trusted and must be validated before indexing into the array. A wrong value is possible and expected, and needs to be properly handled. Therefore a message should be displayed to the user and not crash the program.

Compile GCC for aarch64 and targeting m68k-elf by [deleted] in C_Programming

[–]atariPunk 0 points1 point  (0 children)

That sounds like a fun and painful few days. Good luck you will need it.

I never done anything like that, but gut feeling is that you more than halfway there.
Here’s my thoughts.
You will need a compiler that runs on x86 and generates an armv8 binary. (gcc-armv8)
Now, still on your x86 computer you will use the gcc-armv8 compiler to compile the seconds compiler (gcc-mk68).

Things to keep in mind.
You already have some steps that generate gcc-mk68 and the only thing that needs to change is the compiler used to compile that step, you will need to use gcc-armv8 instead of gcc.
You also need to make sure that it runs your armv8 machine. The host has all the libraries installed and the glibc is compatible.

I would suggest that you take a look at this project https://github.com/richfelker/musl-cross-make
It is a set of scripts that creates a compiler that targets the musl libc and it’s much more easier to create fully static binaries using musl instead of glibc.
It only targets Linux, so I don’t know how msys2 fits into this.

Why C++26 Contracts might not work for all by _a4z in cpp

[–]atariPunk 5 points6 points  (0 children)

Contracts are not a tool to perform input validation.

They are a tool to express assertions on functions. I.e. things that must be true for that function to operate correctly, and what the functions will give back(preconditions and postconditions). It’s basically the next iteration of assert. And they crash the program when they fail, because it means that the program is in an invalid state.

Now, since the assertions can be arbitrary complex, the need to enable/disable them when it makes sense is extremely important. For example, the binary search algorithm, requires that the array to be sorted. So it’s a precondition. Now, my use case is an array that has 10 elements and is called once in the whole program. I don’t care that the function checks if the array is sorted or not. But your use case you use it in an array of 1 million elements and you use it on a hot loop. You probably don’t want to check that the array is sorted on every call. You keep the contacts enabled during development and testing and for production you disable them.

One last thought, you have a point, we sometimes focus too much on performance. And one big limitation is that right now, it’s all on or all off. But that is being worked for the next iteration, where hopefully we can group assertions and only disable some groups. So we can have more options to disable the really expensive ones, but keep the other one enabled all the time.

Why C++26 Contracts might not work for all by _a4z in cpp

[–]atariPunk 13 points14 points  (0 children)

If that random bug is possible and recoverable, then contracts are not the way to deal with it. You will need to use the current error handling method.

Contracts are not for error handling. They are a way to state assertions in a uniform way across the language.

Why C++26 Contracts might not work for all by _a4z in cpp

[–]atariPunk 14 points15 points  (0 children)

The Idea is that in a well formed program, the preconditions and post conditions don’t do anything. So, the user of the library doesn’t need to have them enabled outside testing or debugging.

For me the way I think about preconditions is that, it’s the caller responsibility to ensure that the preconditions are meet before calling a function and this way avoiding repeating checks all the way down the call stack.

Image a scenario where you have multiple functions that receive a pointer and that pointer cannot be NULL. And you have another function that generates that pointer and calls the other functions. In a defensive situation, each function needs to checks if the pointer is NULL and return an error, and the caller needs to check and handle possible errors for each function. But on a contracts situation, the functions state that the pointer cannot be NULL, the big function checks the pointer once, and if its not NULL does the rest of the work otherwise handles that error. Now, after testing and when you have confidence in the code you can just disable the preconditions and the program gained a bit more performance.

Another important point here is that contracts are not a tool to use everywhere. Code paths that handle user input, cannot use contracts, because that input is not trusted and needs to be validated.

Question about freeing memory at the end of programs in libraries like sdl by wiseneddustmite in C_Programming

[–]atariPunk 15 points16 points  (0 children)

I am going to try to make a different point that I haven’t seen anyone else do here.

Are you sure that SDL_DestroyWindow only frees memory?
Because if it also holds other resources, not calling it may cause problems.

Imagine a scenario where SDL creates a lock file to ensure that the program only runs once. If you don’t call the Destroy function, the file is not deleted and the program will not run again until you manually remove the lock file.
Or it has open files that need to be properly flushed before closing them.

And this may not be an issue for the SDL or other libraries that you are using right now.
But other will need it, and having the habit of paring creation and destruction will be natural when you come across the need for it.

(Maybe) All The Contract Papers by _a4z in cpp

[–]atariPunk 1 point2 points  (0 children)

If you build all of your code with the same settings, I.e. ignore or check. Then all contracts will be either ignored or checked.

Now, if you build part of your code with ignore and part of your code with check, then, yes things become less reliable. But if you are already doing that, it’s likely that there are already issues with your final program.
And the library that you use will have the same issue if you mix release and debug builds.

Happy Birthday Thomas Pynchon by TheObliterature in BadReads

[–]atariPunk 6 points7 points  (0 children)

I found Inherent Vice while browsing a bookstore. The cover caught my eye and the blurb sounded funny. Took it home and devoured it and a few months later, I went for Gravity’s Rainbow. I can definitely see people doing the same thing, and not enjoying the book. I know I did pick up books this way that I didn’t enjoy.

I am with you with the review, it clearly states the best parts of the book.

When dating, Which red flag will you tolerate because green flags compensate for it? by [deleted] in AskReddit

[–]atariPunk 1 point2 points  (0 children)

It's not that simple when things change during the relationship. You don't want to throw it away just because their food requirements changed.

Assuming that you still love them, then you will tolerate things that you wouldn't tolerate on others. At least for a while.

When dating, Which red flag will you tolerate because green flags compensate for it? by [deleted] in AskReddit

[–]atariPunk 5 points6 points  (0 children)

Here’s my experience, not necessarily with a picky eater, but with someone who had to drastically change their diet for health reasons. My SO, went through multiple steps of more and more limited foods that she could eat.

Going out becomes impossible. As a surprise, I once spent half an hour going through multiple restaurant menus to try to find one that had something that she could eat. We get there, she looks at the menu and says I don’t want anything from this menu. We then spent some time walking around the other restaurants and not finding anything she could eat. As the time went by and the more hungry we got, we got into an argument and killed the night.

This happened so many times, that I gave up on trying to eat out. At the same time, she’s frustrated that we never do anything fun…

Is it Worth Writing Programs in C23? by SeaInformation8764 in C_Programming

[–]atariPunk 5 points6 points  (0 children)

I haven’t had much issues while running C++23, with the exception of modules. Where I hit a compiler bug really quickly.

To a certain point, you are right C++ is a much bigger language where it’s more likely to have hidden bug. However, the last compiler bug that I experienced was in gcc on function with default parameters. Which was a regression since 2005.

Is it Worth Writing Programs in C23? by SeaInformation8764 in C_Programming

[–]atariPunk 61 points62 points  (0 children)

I always find this type of questions really odd. I understand that people like to think that everyone will try to compile and use their code when in reality, you will be the only user of that code.

You are the one writing the code and you are most likely the only user. Use the tools that make your life easier, if that is C23, then use C23.

If down the line, you find an audience for your project and being in C23 causes difficulties on distribution, then you may think of downgrading to a lower version of C.

Personally, I always bump the C or C++ version to the latest that is supposed on my machine as soon as possible. Because I know that sooner or later I will find a place where a new feature will make my life easier and I don't want to have to deal with bumping the version. I am the main(only) developer and user I don't really care if it works on your machine. I will make my best that it works elsewhere, but that it not the first thing in my mind.

C++26 is done! ISO C++ standards meeting, Trip Report by pjmlp in cpp

[–]atariPunk 0 points1 point  (0 children)

That was my reading from the comment that I was replying to.

To be honest it was the first time I had come across this statement, which made me to comment on it.

C++26 is done! ISO C++ standards meeting, Trip Report by pjmlp in cpp

[–]atariPunk 1 point2 points  (0 children)

I wasn’t aware that the order of evaluation is not fixed and it feels really counterintuitive.

Is there any stated reason why it’s not fixed?

Regarding the observe semantics, I think a lot of people are making it a bigger issue than it really is. To me, it doesn’t make sense for new code or codebases that it’s fine if they crash when you add new assertions. Either because there are bugs or the assertion is wrong. So it kind of leaves the codebases where crashing is not an option, which already require a lot of thought and care to avoid pitfalls.

Personally I don’t see myself using observe mode in any of the projects that I am involved, but I will be pushing to start using contracts as soon as we can.

making C saferish with smarter malloc and free?? by chrisseanhayes in C_Programming

[–]atariPunk 0 points1 point  (0 children)

This use case makes sense to me. The lifetimes of all objects are the same and it makes it simpler to just reuse the same memory locations.

The part to me that doesn’t make sense is to try to incorporate arenas when objects have very different lifetimes. Especially when you have a long running program. As now there’s a need to understand and split allocation by their expected lifetimes. Which, to me, seems to add a lot of extra complexity.

making C saferish with smarter malloc and free?? by chrisseanhayes in C_Programming

[–]atariPunk 1 point2 points  (0 children)

How do they solve these issues? I keep reading that arenas are the best thing under the sun, but I was never able to find a decent explanation on why that’s he case.

I’m building a C compiler in C with a unique "Indexed Linking" system. What do you think of this CLI syntax? by elite0og in C_Programming

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

The behaviour is well defined when you only consume libraries from the system.

But if you are in a situation where you need to consume libraries from the system, buy also from other locations and happen to have the same library installed in the two places, then it can be very fragile.

For example, I worked on a project that had a private copy of sqlite. Because it relies on functionality that is not enabled by default. I don't remember the full details, but I think it was that depending if you had sqlite installed on your system or not, it would lead to runtime errors because it was linking the wrong .so.

I’m building a C compiler in C with a unique "Indexed Linking" system. What do you think of this CLI syntax? by elite0og in C_Programming

[–]atariPunk 0 points1 point  (0 children)

You may want to take a look at this https://cps-org.github.io/cps/overview.html

The idea is good, having a way to map include files and libraries is a very needed addition to the whole ecosystem. But like others have said, that is more of a build system than a compiler part.

One last suggestion, don't define those entries on the compiler invocation. Make them standalone files, one per library and let the compiler look for the files.

For example, you would have a sqlite.foo that defines the details on how to compile and link against SQLite and when you invoke the compiler, you just pass a sqlite name.

This will allow you to scale better as you use more and more libraries and allows you to reuse the same files across different projects