1. 4

    Another potential bug many might miss is the multiply by 2. On a 64-bit system where int is 32-bit (Windows for instance), it would be possible for this operation to overflow, resulting in undefined behavior.

    1. 2

      That would mean they had to allocate 2.1GiB * sizeof(int) (or at least 1.05GiB * sizeof(int)). Even so, calling malloc on a negative integer would just have it return NULL in new_data, so the assert would fail and the program would be terminated before anything bad happens.

      1. 2

        I think “that would require unlikely input” and “undefined behavior does nothing bad on my platform” can be dangerous when it comes to C.

        For instance, given the compiler knows the capacity starts at 1 (if we fix that bug) and is always multiplied by 2, since overflowing would be undefined behavior, it can assume that will never happen and generate a shift left for the multiply. That would result in overflowing to 0 (which could trap), which when passed to malloc could (implementation defined) return a non-NULL pointer that cannot be dereferenced.

        I know that’s all highly unlikely, but likely isn’t safe.

    1. 4

      Nice work, some thoughts:

      • Print line number where assertion failed
      • Way to compare doubles, possible with an optional precision
      • Way to compare blocks of memory
      • Consider renaming to snow.h
      1. 3

        I really would have liked to print the line number where the assertion fails, but I’m not sure if that’s possible. Because of the use of macros, everything ends up on the same line after the preprocessor, so __LINE__ will be the same for everything. If you know of a way to fix that, I’d love to hear it. (The "in example.c:files" message was originally supposed to be "in example.c:<line number>")

        More different asserts is a good idea, and so is renaming the header - the thing was under the temporary name “testfw” until right before I made this post.

        1. 2

          Looks neat! I feel that the line number of the end-of-block would still be useful, but don’t quite see how to word that without seeming incorrect.

          1. 2

            It’s not just at the end of the it block which the error occurs in; it’s the end of the entire invokation of the describe macro. In the example.c file for example, __LINE__ inside of any of those it blocks will, as of the linked commit, be 62.

      1. 6

        I really like the idea behind the theme and the general feel.

        For my personal taste, as a text editor theme, the background is a little too light and the comments are barely visible. And I say that as someone who uses Solarized.

        These things are highly subjective of course, and also depend on what room you’re in, what time of day, and what display.

        1. 2

          For my personal taste, as a text editor theme, the background is a little too light and the comments are barely visible.

          The emacs theme deals with this by adding nord-comment-brightness.

          I think I’m finally going to replace Zenburn (my previous all-time favourite theme)!

          1. 1

            Personally, I’ve tweaked my nofrils-like theme for emacs using these colors.

        1. 12

          I never understood the popularity of solarized. It lacks contrast and makes my eyes hurt.

          1. 12

            There was a blog post which said it was made with science or whatever. Science can’t be wrong.

            1. 3

              The implication that the goodness of something so subjective can be quantified really irks me. However, I think a lot of people ate this up, as I’ve seen people non-ironically citing this as a reason it is good.

              1. 3
                1. 2

                  I hear it’s Cave Johnson’s favorite IDE color scheme.

                2. 5

                  I’m more and more in favour of highlighting comments more than the individual parts in the code (variables, strings, …) – and I find that comments often have the least contrast :(

                  1. 2

                    In Visual Studio Code you can quite easily try this out since you can add your own customizations to the highlighting in the settings. For instance, you could add

                        "editor.tokenColorCustomizations": {
                            "comments": "#e1a866"
                        }
                    

                    to change the color of all comments.

                  2. 1

                    I think it depends a lot on lighting. I use the dark theme at evening/night, and don’t have a lot of light in the room. More contrast rich themes like Monokai hurt my eyes in that setting.

                    The Solarized theme that comes with Visual Studio Code actually uses a base color with more contrast than the original design. But I find that rather annoying in the light theme, especially since they also use bold.

                    1. 2

                      More contrast rich themes like Monokai hurt my eyes in that setting.

                      That makes sense. It’s funny, at night I will continue using typical white-on-black high-contrast color schemes but just drop the monitor brightness a lot if I happen to be hacking away in the dark. Usually I just turn the lights on, though.

                      1. 1

                        For me both variants of Solarized are difficult to read in the daytime on a nice display and borderline unusable at any time of day on a low-end display. On the other hand, I find high-contrast dark themes too harsh, so I tend to use dark themes that are somewhere in the middle (~#999 on ~#222) and higher-contrast light themes (~#222 on ~#f5f5f5).

                        1. 1

                          gruvbox dark works well for me :)

                          1. 2

                            I think the red is perhaps, well, a bit too red in gruvbox. The (over-)use of red/orange/pink in many Solarized themes was part of the reason I made this variant.

                            Darktooth is another interesting gruvbox-like theme.

                      2. 1

                        Agree on the importance of contrast. Lots of color themes are happy to use tons of different colors on things that aren’t completely semantically different (a numeric literal doesn’t always need to stand out a lot) while ignoring the more subtle details such as contrast.

                        I want the attention to detail Solarized has, but with more contrast, and something besides an ocean or a piece of parchment as the background. I’ve been using a version of Github’s color scheme in my editor for awhile, but have yet to really find a color theme that I really like.

                      1. 1

                        Some years back I made a syntax theme based on the Solarized color scheme for Sublime Text, and I’ve been working on making it available for Visual Studio Code and Atom. So I will probably spend some time fixing little issues and fine-tuning colors.

                        You might wonder why yet another Solarized theme (Yast)? there are plenty, and most editors even come with them. I find many of them look too busy for my taste, they assign colors to every possible syntax entity. Some of them also choose colors that are hard to see for things like selection highlighting (which is not directly specified by the original scheme). So I attempted to make a version that, as far as possible, only assigns colors to the root groups specified in the TextMate documentation (which highlighting in both editors originated from).

                        Besides that I am also in the process of moving some of my personal programming projects to meson.

                        1. 1

                          I am always happy to see alternatives to gitflow (which I think is overly complicated for many projects). This is a nice idea, but perhaps it works best with specific types of development. A few thoughts:

                          If two developers are working on separate features that affect the same piece of code

                          if (featureA) {
                            // changes from developer A
                          } else if (featureB) {
                            // changes from developer B
                          } else {
                            // old code
                          }
                          

                          How do you rename or remove a variable as part of refactoring, in a way that makes all four combinations of feature flags still work?

                          I guess it will depend on the types of changes, and how the developers communicate, if this is easier than feature branches (where the conflict resolution is deterministic at the end, and diff works), or if this leads to feature flag spaghetti, and time wasted adapting your changes to be able to run alongside another developers changes, which might end up not being merged.

                          Also, what if you add a file? then I guess your build system will need feature flags. What if your build system uses globbing and you remove or rename a file? Some changes can’t both be there and not be there.

                          1. 1

                            I know people are going to feel differently about this, but I lean heavily toward explicit being better than implicit, and the presence of magic should be minimal, even if it means some redundant work. Redundant work can be verified and semi-automated to keep explicit things up-to-date.

                            How do you rename or remove a variable as part of refactoring, in a way that makes all four combinations of feature flags still work?

                            A feature branch just delays this question to the big bang merge conflict. Forcing you to do this work upfront means you talk about this with the other people working in the same code region.

                            Like you say, the other side of the coin is that the feature branch might never be merged. Early merging optimizes for the happy path. But then again, if you merge your work early and discuss with others, it can’t remain in the twilight state of not merged, not discarded, which may improve coder efficiency.

                            Also, what if you add a file? then I guess your build system will need feature flags.

                            Just adding a file shouldn’t affect anything unless some other file references it.

                            What if your build system uses globbing

                            Please don’t, especially if you also automatically siphon those files into some automatic extension of functionality.

                            Merge conflicts are annoying, but clean merges that have semantic conflicts are even worse.

                            Of course plugin systems are super useful – when they are user accessible and are used for deployment. But then the API would be well-defined, restricted and conservative. Probably the plugins would even be in separate repos and the whole branch vs flag point is moot.

                            Testing plugin interactions is probably worth an article series of its own.

                          1. 5

                            Several people here are recommending CMake as an alternative. I’ve only interacted with CMake at a fairly surface level, but found it pretty unwieldy and overcomplicated (failed the “simple things should be simple” test). Does it have merits that I wasn’t seeing?

                            1. 3

                              CMake can generate output both for Unix and for Windows systems. That’s one (good) reason lots of C++ libraries use CMake.

                              1. 2

                                CMake is pretty nice and has nice documentation. You can also pick stuff up from reading other people’s CMakeLists. For simple projects the CMake file can be pretty compact.

                                1. 3

                                  I actually found the CMake documentation to be quite terrible for new users. The up-to-date documentation factually describes what the different functions do, but has very little examples of how to actually write real-world CMake scripts. There are a few official tutorials that try to do this, but they are made for ancient versions like CMake 2.6. So in order to learn how to use CMake, you are stuck reading through tons of other peoples scripts to try to deduce some common best practices.

                                  While modern CMake is not terrible, you often have to restrict yourself to some ancient version (2.8.6 I believe is common) in order to support certain versions of CentOS/RHEL/Ubuntu LTS (and there were some big changes in CMake around 2.8.12/3.0).

                                  Also, having string as the only data type has led to some absurd corner cases.

                              1. 1

                                I liked the introduction with the explanation of why we have the gamma functions, but I think it is important to note that sRGB is a color space. Having a section titled “What is sRGB?” and then only explaining the associated gamma function is a bit misleading (I know there is a comment at the end of the section about sRGB containing some other stuff).

                                I assume this is written from the viewpoint of DirectX, because sRGB is not limited to 8 bits per channel, you can use whatever precision you like for the components (PNG for instance supports both 8 and 16 bpp).

                                1. 5

                                  Titus Winters was asked about build times at this years CppCon:

                                  https://youtu.be/tISy7EJQPzI?t=1h23m10s

                                  1. 3

                                    He never considers the opportunity cost of using that massive build farm for C++. Think of all the bitcoin that could be mined.

                                    1. 4

                                      Opportunity cost? More like opportunity; without C++’ compile times, no such build farm would’ve been built. Because the build farm is built, and presumably doesn’t have to compile code 24/7, there’s now an opportunity to automatically mine bitcoin while the CPUs aren’t busy compiling!

                                      1. 3

                                        Bitcoin mining with CPUs hasn’t been profitable for years.

                                    1. 1

                                      Anyone else feeling this trend of hand-drawn illustrations is slowly getting out of hand?

                                      1. 12

                                        My apologies. All my approaches to diagrams were getting out of hand and nothing was cooperating, so I dropped these in more as “artistic sketches” than informational diagrams. I will probably update them at some point. (Also, I repeatedly redrew these but only had really bleedy paper and my fountain pen, and the scanner wasn’t cooperating, et cetera… excuses, excuses, I know.)

                                        1. 11

                                          I’ve gotten the feeling that it’s done for production speed rather than style, but they always give me a warm reminder of the 60s-mid 80s DIY manuals I grew up reading.

                                          1. 1

                                            There is also the problem of what to use for such drawings. I remember using xfig for such things a long time ago, I have no idea if there is anything suitable these days?

                                            1. 6

                                              I often make diagrams in ipe or inkscape, but in this case I was thinking the best thing would be through tikz, but it’s a fair bit of work. ipe in particular is really nice for quickly throwing together figures.

                                              1. 3

                                                I draw them on my tablet and take a screenshot. It’s a lot faster than trying to wrangle something in graphviz or a drag’n drop shapes.

                                                1. 2

                                                  I still sometimes use Xfig + transfig. Depending on the use case, I might use VUE (Visual Understanding Environment — it is nice for drawing graphs, even if it is only a part of its supposed purpose) or GraphViz. I think I haven’t used Dia for a long time; I do use Kig when I want to draw something geometrical. If I want to draw something complicated precisely, I generate it using Asymptote.

                                              2. 4

                                                I used to do them because I could sketch things quickly with my hand versus software. I was often writing things in notebooks and such.

                                                1. 3

                                                  Normally I find them charming, but I admit these ones are rather fuzzy.

                                                  1. 2

                                                    Usually I don’t care either way as long as they get the message across, but several of these are unreadable.

                                                  1. 1

                                                    I think it is perhaps worse that it seems that in Ruby:

                                                    • "-".split("-") is []
                                                    • "-x".split("-") is ["", "x"]
                                                    • "x-".split("-") is ["x"]

                                                    Looking at the documentation, trailing fields are suppressed unless you supply a negative limit parameter. Python and AWK return two elements for each of these.

                                                    1. 1

                                                      Perl acts like Ruby in this case, unless you supply a negative LENGTH parameter.

                                                      Edit edit AWK does return 2 elements, it’s only Perl and Ruby in the weird corner :D

                                                      My limited knowledge of AWK indicates it acts in the same way as Perl and Ruby, but this could be because I’m writing as

                                                      $ awk -F: ‘{split($0,a,”-”); print a[1],”:”,a[2]}’ <<< “x-”

                                                      so I’m implicitly assuming the array a will have 2 elements.

                                                      Edit the manual for my version of AWK (GNU Awk 4.1.3) states:

                                                      If string is null, the array has no elements. (So this is a portable way to delete an entire array with one statement. See Delete.)

                                                    1. 2

                                                      Note that OpenMP also works for C. However, in C, you need to set explicitly the loop variable as private, because C does not allow declaring it in the loop body:

                                                      int n;
                                                      #pragma omp for private(n)
                                                      for(n=0; n<10; ++n) printf(" %d", n);
                                                      printf(".\n");
                                                      

                                                      wat. The article was first written in 2007. Since C99, you have been able to declare variables in the for-loop preamble. The following works great for me, even when compiled with -std=c99:

                                                      #include <stdio.h>
                                                      #include <stdlib.h>
                                                      
                                                      signed
                                                      main (void) {
                                                      
                                                          #pragma omp parallel for
                                                          for ( int i = 0; i < 50; ++ i ) {
                                                              puts(i % 2 == 0 ? "Hello" : "World");
                                                          }
                                                      
                                                          return EXIT_SUCCESS;
                                                      }
                                                      
                                                      1. 2

                                                        It works fine with GCC, but MSVC does not support declaring the loop variable inside the for of parallel for in C. If you try to compile the above code as C, you get “error C3015: initialization in OpenMP ‘for’ statement has improper form”, whereas it compiles fine as C++.

                                                        On a side note, MSVC does not allow the loop variable to be unsigned either (which I believe might be due to only supporting OpenMP 2.0, but still can be annoying (think size_t)).

                                                        1. 4

                                                          MSVC is not a C compiler.

                                                          It is very explicitly a C++ compiler that just added enough C99 compatibility for the C++ compiler to stay correct.

                                                          It is unreasonable to include it in the category of C compilers.

                                                          1. 3

                                                            While that is true, it does seem somewhat random that it does support declaring variables in C for loops, except when they happen to be OpenMP loops.

                                                      1. 4

                                                        I quite like using a line under a heading in markdown, it’s the way I have always marked a heading in readme files before all these markup languages came along. To me it just looks more natural when viewed as plain text, while using a single asterisk in front of a heading could be confused as a list with one element if you do not know it’s markup.

                                                        Of course part of the reason is probably I rarely need more than one level of headings in readme files.

                                                        1. 2

                                                          Will be interesting to see if this release also means beta 4.0 comes out soon with little to no further updates to 3.0 final. Not complaining, I have a license and been using the 3.0 betas since 2.0 went final, just seems to be the way releases work for ST.

                                                          1. 3

                                                            Another thing (at least for me) – besides debugging, which was already mentioned – is defensive programming. I’ve worked on a number of libraries written in C, and at some point you develop a sense for when you need to ask yourself “how could this go wrong?”.

                                                            I know unit testing should solve most of these problems, but I think fuzzing has thoroughly shown this is not the case. Experience can help you predict at least some ways in which code could break. I guess to some extent that experience is related to that of debugging these same issues.

                                                            1. 5

                                                              I never liked the omission of the full stop at the end of the so called summary, and started adding it many years ago. There are others with similar experience. Let me link to antirez’s blog post to avoid explaining it myself: http://antirez.com/news/90

                                                              1. 1

                                                                Worth noting that the commit messages produced by git (like for merge commits) do not add a period (by default).

                                                                1. 1

                                                                  I often edit merge commits, and when I do I obviously add lacking period.

                                                                2. 1

                                                                  Thanks for the link, I’ll be saving it!

                                                                1. 0

                                                                  Save yourself the trouble and use Autotools: https://autotools.io/index.html

                                                                  1. 8

                                                                    spits out water in disbelief, record scratch to silence – the more common position that I hear is “autotools” is not very “auto”, and in fact more trouble than it’s worth.

                                                                    1. 1

                                                                      the more common position that I hear is “autotools” is not very “auto”, and in fact more trouble than it’s worth

                                                                      And it usually comes from those that never needed complicated things from their build system. The ones jumping at the chance of using some exciting new build system with a quarter of Autotools’ functionality because all they do is the simple stuff.

                                                                      Even if that’s you right now, pay some respect to the old software gods. You’re going to need them when the going gets tough.

                                                                      1. 1

                                                                        Personally, I’m in the camp of using make, and would probably reach for autotools when the going got tough. But build tools seem to be becoming as controversial, in some circles, as using C instead of the “safer” rust.

                                                                        1. 1

                                                                          Personally, I’m in the camp of using make, and would probably reach for autotools when the going got tough.

                                                                          Why not do it the right way from the start and get most of the portability for free?

                                                                        2. 1

                                                                          Autotools definately has its uses, but part of the reason for some of the new build systems is that portable sometimes means Windows (MSVC), OS X, and Linux, rather than POSIX.

                                                                          1. 2

                                                                            Autotools definately has its uses, but part of the reason for some of the new build systems is that portable sometimes means Windows (MSVC), OS X, and Linux, rather than POSIX.

                                                                            Autotools support all these operating systems: https://github.com/opentoonz/GTS/pull/4

                                                                            I did the Linux port and got an OS X port for free (and probably a Windows port through Cygwin or MinGW).

                                                                            1. 1

                                                                              While I appreciate what you are trying to say, I stuck the MSVC after Windows because that is the reality that many Windows developers have to deal with. Using a POSIX emulation environment like Cygwin or MSYS2 is not always an option. The OP article also mentions that if you are using Makefiles you will (most likely) need a separate one to support MSVC.

                                                                          2. 1

                                                                            I’ll ask the question I ask everytime autotools comes up: Can you name some of this functionality that autotools provides – which is actually still useful today?

                                                                            Respect is all very well, but people should know why they use the tools they use, and not just cargo cult things long after they’re obsolete.

                                                                            1. 1

                                                                              I’ll ask the question I ask everytime autotools comes up: Can you name some of this functionality that autotools provides – which is actually still useful today?

                                                                              Here’s one for you - it makes the hard things possible. Hard things like combining Go and C code in a library: https://github.com/stefantalpalaru/golib/blob/master/Makefile.am

                                                                              1. 3

                                                                                What is autotools doing here, as regards the problem of linking Go and C object files together, that vanilla GNU/BSD make couldn’t?

                                                                                If you want to limit the conversation to things which produce Makefiles, such a thing is fairly easily done in cmake, too (or, in the Rust world, with cargo). And it would likely “just work” as well with cmake’s ninja generator.

                                                                                1. 1

                                                                                  What is autotools doing here, as regards the problem of linking Go and C object files together, that vanilla GNU/BSD make couldn’t?

                                                                                  • conditional benchmark compilation if the default Go compiler is available (determined by the configure script).

                                                                                  • shared and static library creation for multiple platforms, thanks to libtool.

                                                                                  • enabling extra CFLAGS (and related features) if gcc is new enough to support them

                                                                                  such a thing is fairly easily done in cmake, too

                                                                                  Good luck with using gccgo as a compiler and passing it custom flags. You’ll probably end up writing one or more CMake modules.

                                                                      1. 1

                                                                        Given the license incompatibility issues with other projects licensed as Facebook BSD+patents (RocksDB), I wonder how the linux kernel maintainers will interpret it.

                                                                        1. 5

                                                                          Real color theory is very complex, however this author just gave up and dealt with the HSL colorwheel, further teaching another generation that we should not look beyond RGB color spaces, which in my opinion is a fallacy once display technology gets better.

                                                                          1. 4

                                                                            What resources do you recommend as an alternative? I’m interested in color theory and alternate color spaces, but haven’t really found any accessible resources for learning about it.

                                                                            1. 13

                                                                              I would recommend to get your hands dirty and dive into the theory. There’s lots of handwaving in most sources available. Try to understand what the motivation behind the XYZ primaries is (to keep it short, they are three imaginary colors that, combined linearily, can create any perceivable colour), which followed from the fact that there are colours which cannot be “mixed” from RGB. I found the video series “Color Vision” by Craig Blackwell 1 2 3 4 5 to be very helpful and enlightening.

                                                                              Now, once you’ve understood that, you have to understand that the “perfect” spectral colors (which means monochromatic light of different wavelengths) are the “boundaries” of human vision, so to say the convex hull of the XYZ-coordinates of all visible spectral colours (modulo lightness) is the realm of human vision. You can get those coordinates here. It turns out, these “coordinates” X(lambda), Y(lambda), Z(lambda) are called color matching functions (you can get them there as CSV’s).

                                                                              The final piece in the puzzle is to convert these XYZ coordinates to common normalized formats like xyY or even CIELab or CIELuv, or even polar coordinate forms like HCL(uv). All these formulas can be found here, an excellent resource of transformation functions between all kinds of different formats, including XYZ <-> xyY and so on, by Bruce Lindbloom. He is a really awesome guy. Now the only thing left to do is plot the xyY data in ParaView or something and if you look at just the xy-plot you’ll see the familiar horseshoe. :)

                                                                              If you look at the color transformations from RGB to XYZ, you’ll see that RGB data includes a lot of implicit information, including the reference white and which RGB primaries you actually choose. Most people, talking about RGB, are talking about sRGB, but you can “invent” an RGB color space easily by just defining three primaries. If you manage to convert an RGB coordinate to an XYZ, Luv or Lab coordinate, the latter are all “universal” in describing color perception. These spaces allow you to express any visible colour. With RGB, they over the years defined new colour spaces (Adobe RGB, ProPhoto RGB and so on), moving the primaries a bit to make the range of colours bigger, but in the end there are still quite a lot of colours left which just cannot be expressed within currently “popular” RGB colour spaces. There are drawbacks to working with anything other than sRGB coordinates (imaginary colours, no good software backing, lots of superfluous nomenclature, …), but you gain a lot of things. For instance, CIELuv is perceptually uniform, which means that if you select equidistant colors on a “line” within the uv-diagram, these colours will all be equidistant with regard to perception. If you work with HSL based on RGB, this does not work, which is very cumbersome if you want to select nice colours for diagrams and such. The reason for that is for instance that sRGB has a nonlinear gamma curve. You might want to work with Linear RGB then, but if you do any of that stuff on your data, you might as well work with the real thing.

                                                                              I hope this introduction was sufficient to get you started. Send me a mail if you want to see some code. I might give a talk about it this year, but it takes some preparation.

                                                                              1. 1

                                                                                Woah, thanks for the intro - I remember seeing your slcon talk a few months ago on color spaces and farbfeld, which is initially what got me interested. I’ll probably dive into all of it a bit more soon when I start fiddling with 3D graphics again. Thanks for the resources - I’ll definitely check them out :D

                                                                              2. 4

                                                                                not OP, but https://www.handprint.com/HP/WCL/wcolor.html is probably the most accessible treatment I’ve found.

                                                                                1. 1

                                                                                  Cool I’ll definitely take a look at this - thanks :)

                                                                                2. 3

                                                                                  Color theory is interesting, but (I feel) a bit of a rabbit hole. I learned a little about color spaces a few years back while writing a blog post about how the difference in color space and gamma value between older Mac and Windows computers might have influenced some of the popular color themes.

                                                                                  I found Danny Pascale’s “A Review of RGB Color Spaces” a useful (if somewhat terse) source of information about computer color spaces.

                                                                                  1. 1

                                                                                    Neat, I’ll have to read that paper, thanks for pointing it out

                                                                                3. 6

                                                                                  Are there non-RBG displays coming down the pipeline? It makes sense to design for the displays that exist rather than the ones that might. I think this article is, as the title says, going for practical color theory rather than comprehensive color theory.

                                                                                  1. 1

                                                                                    I assume you are referring to displays that can display colours beyond sRGB. Yes, these displays exist already. In fact, the current iPhone and most AMOLED displays fall within that group.

                                                                                  2. 3

                                                                                    Real color theory is very complex

                                                                                    I think the issue here isn’t really about “real color” theory as in “all there is no know about color” but “i am making a blog what color what I make all the stuff in it?” and choosing 2 colors then making a light and dark version of each is a solid strategy for an otherwise ‘design-illiterate’ developer.

                                                                                  1. 1

                                                                                    For anyone interested, here is the thread on encode.ru discussing this, and there is also LZ4X (Github, and forum thread) which was released around the same time.