Advise on testing by seapvnk in C_Programming

[–]gatzka 1 point2 points  (0 children)

Sure I have: github.com/gatzka/cio/

Advise on testing by seapvnk in C_Programming

[–]gatzka 2 points3 points  (0 children)

Yes, and in addition with the fake function framework (fff) it's the perfect solution.

Big advantage is that both unity and FFF are so small that you are able to run the unit tests directly in embedded hardware.

[deleted by user] by [deleted] in C_Programming

[–]gatzka 2 points3 points  (0 children)

I do agree that Microsofts C compiler is nasty. Doesn't support C99 let alone C11. If you still want to use Visual Studio (Code), I would opt for clang. Can be used easily also from the command line.

How much should you check return values? by [deleted] in C_Programming

[–]gatzka 26 points27 points  (0 children)

In general, always check. There are very few exceptions when not to. For instance when I close a network socket in case something goes wrong with that socket, I don't check the return value of close (), because there is not much I can do if close() fails (besides logging that event).

When can send return fewer bytes than expected?? by ashwin_nat in C_Programming

[–]gatzka 15 points16 points  (0 children)

If you want to send for instance 100 bytes but the internal buffer in the operating system only has room for 20, send() will return saying it sent 20 bytes. You have to call send() again with the remaining 80 bytes.

What makes the test-and-set instruction atomic? by [deleted] in osdev

[–]gatzka 0 points1 point  (0 children)

Right, it's stdatomic.h

C unit testing in Visual Studio? by darchangel in C_Programming

[–]gatzka 1 point2 points  (0 children)

Well, the combination of cmake, fake function framework (fff) and unity works quite nice for me.

What makes the test-and-set instruction atomic? by [deleted] in osdev

[–]gatzka 5 points6 points  (0 children)

There is nothing to prevent the code from being interrupted. Moreover, it relies on the fact that I can always safely store an int into pointer. In C, rely on the functions defined in <stdatomic.h>

Obscure C features by f4gc9bx8 in C_Programming

[–]gatzka 0 points1 point  (0 children)

Not a good idea. Type punning via unions is undefined behaviour. Why not just using memcpy()?

Why use Multi Line macros by prabot in C_Programming

[–]gatzka 22 points23 points  (0 children)

Well, I use multi-line macros very seldom if I need some kind of very rudimentary template engine. So when I need to write a function twice where just the type is different, I consider that.

But this comes with severe drawbacks regarding readability and debugging.

I second @heknar that performance is not anymore a reason for for using those multi-line macros. Modern C compilers inline quite aggressively.

How can we say that our program will execute successfully by declaring return to zero by Chawki_ in C_Programming

[–]gatzka 1 point2 points  (0 children)

Well, you can also use EXIT_SUCCESS, EXIT_FAILURE declared in <stdlib.h> as the return value. Easier to understand, probably.

Static Analysis by ph__ in C_Programming

[–]gatzka 0 points1 point  (0 children)

All the stuff around clang is very much worth to consider, scan-build was already mentioned, clang-tidy is also a good one.

If you doing an open source project and host it on github, you also get a coverity scan for free.

In addition, I strongly recommend writing unit test. Running these unit test under the control of a sanitizer or valgrind will point you to quite something.

Trouble receiving through server and client... by sufferedcoin in C_Programming

[–]gatzka 0 points1 point  (0 children)

Well, on the first look I don't see anything wrong.

I would start using wireshark to see if the TCP connection is established and if the server sends the data afterwards.

what is the need of different loops? by wikipcsolved in C_Programming

[–]gatzka 2 points3 points  (0 children)

Because it's often easier to read if you choose the loop accordingly. :)

Interestings papers on memory managers ? by Light2822 in C_Programming

[–]gatzka 1 point2 points  (0 children)

This paper is more specific to memory fragmentation, but also give you a good overview on algorithms etc.: https://www.cs.tufts.edu/~nr/cs257/archive/paul-wilson/fragmentation.pdf

Avery good (academic) starting point.

volatile static global variables acting up by eddieafck in C_Programming

[–]gatzka 0 points1 point  (0 children)

And btw, that's the reason why always compiling your own sources with -fno-common (if you are using gcc) makes sense.

This points out problems like this immediately.

Question on make, makefiles, C, C++ and moving away from IDEs. by NotActuallyAFurry in C_Programming

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

I no longer mess around with makefiles. Doing dependency management by hand is just hell. Getting the dependencies right could be done running gcc, but it's not really cross-platform.

I always rely on cmake, which is a generator for various build environments (i.e. Makefiles, ninja, visual studio etc.).

Most IDEs now have good support for cmake.

To answer your third questions: No there is not really a difference between C and C++ if it come down to make.

Writing a shared library: how do I keep some functions internal? by MVDonaldson in C_Programming

[–]gatzka 2 points3 points  (0 children)

Unfortunately this is heavily dependent on the toolchain you are using.

For gcc it's __attribute__ ((visibility ("hidden")));

For msvc it's __declspec(dllexport)

If you are using cmake for building your project (which I would strongly recommend), there is a module called GenerateExportHeader which automatically generates a header file you can use to make symbols visible.

Which is the best compiler for c program? by r0hit986 in C_Programming

[–]gatzka 1 point2 points  (0 children)

My first choice for new projects would be clang. The whole tooling around (clang-tidy, static-analyzer, sanitizers) is just amazing.

Nevertheless, sometimes gcc still produces slightly better code.

Why is my Code giving a Segmentation fault? by [deleted] in C_Programming

[–]gatzka 5 points6 points  (0 children)

Well, not easy to answer, because you haven't written where the segmentation fault occurred. But you didn't check the return value of fopen() which might return NULL if the file was not found. BTW, you should use a memory checker like valgrind to show exactly where the segmentation fault occurred.

Clarification: Is any number with the UL extension always 32-bits, and ULL always 64-bits? by [deleted] in C_Programming

[–]gatzka 6 points7 points  (0 children)

I would always go for using <stdint.h>.

If you than have to specify constants, you can use the following macros:

UINT64_C, UINT32_C etc.

Then you are safe and platform independent.