12 votes

Fortnightly Programming Q&A Thread - 2019W40

General Programming Q&A thread! Ask any questions about programming, answer the questions of other users, or post suggestions for future threads.

Don't forget to format your code using the triple backticks or tildes:

Here is my schema:

```sql
CREATE TABLE article_to_warehouse (
  article_id   INTEGER
, warehouse_id INTEGER
)
;
```

How do I add a `UNIQUE` constraint?

15 comments

  1. [5]
    top
    Link
    I have a C question that I had trouble looking up. Is there a way to define a union in a header, then in the implementation redefine it with more members (as long as the public and private unions...

    I have a C question that I had trouble looking up. Is there a way to define a union in a header, then in the implementation redefine it with more members (as long as the public and private unions have the same size)?

    I want to publicly present a uint16, but privately be able to refer to the upper and lower uint8. Right now I just define the private union separately and cast between them, but that feels bad.

    Before using unions, I used macros with bitshifts to set and get the lower and upper portions, but that's even less safe.

    6 votes
    1. [3]
      wirelyre
      Link Parent
      No, it's undefined per the standard because new members could increase the size. I'm pretty sure no implementation supports that either. You could define a private union with the public union as a...

      Is there a way to define a union in a header, then in the implementation redefine it with more members?

      No, it's undefined per the standard because new members could increase the size. I'm pretty sure no implementation supports that either.

      You could define a private union with the public union as a member, then try to keep references to the private union. Or you could define the public interface with a note "do not use the 8-bit members".

      But you probably should not:

      I used macros with bitshifts to set and get the lower and upper portions, but that's even less safe.

      I strongly encourage you to write functions that use bit shifts. That is the only portable way to interact with integers like this. Your compiler will figure out the rest.

      void set_upper(uint16_t *to, uint8_t from)
      {
          *to = (*to & 0x00ff) | (uint16_t) from << 8;
      }
      
      5 votes
      1. [2]
        top
        Link Parent
        Interesting! Thanks for the reply. I suppose because the sizes of uint8_t and uint16_t are implementation-specific? There might also be padding in the struct of uint8s that would prevent it from...

        Interesting! Thanks for the reply. I suppose because the sizes of uint8_t and uint16_t are implementation-specific? There might also be padding in the struct of uint8s that would prevent it from lining up.

        As for the issue of having a union defined differently in the header and implementation, why not just throw a compiler error if they're different sizes? I don't see how it would be any worse than having a function signature in the header that doesn't match.

        Anyway, I guess I'll go back to bit shifts! Why use functions instead of macros, though? The inline keyword can be ignored by the compiler, so macros are the only way to prevent unnecessary stack usage.

        2 votes
        1. wirelyre
          (edited )
          Link Parent
          ¯\_(ツ)_/¯ I'm not a compiler maintainer. But I would think of it more like defining the same function twice, which definitely should be an error. Matching function signatures is probably closer to...

          why not just throw a compiler error if they're different sizes?

          ¯\_(ツ)_/¯ I'm not a compiler maintainer. But I would think of it more like defining the same function twice, which definitely should be an error. Matching function signatures is probably closer to declaring an incomplete union a; twice.

          (Also, now that I think about it, alignment would need to match too.)

          Why use functions instead of macros, though?

          Macros are quite a lot harder to get right than functions. In my opinion it's usually not worth the hassle. You could flip the reasoning around — why macros instead of functions? If you can get what you want from a first-class language feature, why not use it?

          The inline keyword can be ignored by the compiler

          It can, and often is, but in practice the compiler is better than anyone at figuring out when it's worth it to inline functions.

          See this example.

          In the compiled code for set_upper, no bit shifts are actually performed. Instead it writes from directly to the high byte of to, [rdi+1].

          In with_five_upper, the compiler doesn't even store n in the stack! It inlines set_upper automatically, even without the inline keyword. It also doesn't perform any bit shifts, writing to the high byte ah.

          macros are the only way to prevent unnecessary stack usage.

          Well, the compiler is also free to do whatever it wants with the stack. And as you see above, in practice modern compilers are very smart. (Try them all! The Compiler Explorer goes back to GCC 4.1.2 for x86.)

          But yes, if you have an unusual or very old C compiler you might get better code from a macro. But be wary of premature optimization here: if you don't know if you're losing stack space, and that you need that stack space, then you shouldn't worry about it.

          2 votes
    2. [2]
      Comment deleted by author
      Link Parent
      1. top
        Link Parent
        The data type is supposed to be an opaque handle; if it was modified, it'd be like changing a returned pointer. Therefore, I don't want any public interfaces to modify it or even describe its...

        The data type is supposed to be an opaque handle; if it was modified, it'd be like changing a returned pointer. Therefore, I don't want any public interfaces to modify it or even describe its internal structure.

        1 vote
  2. [8]
    Gyrfalcon
    Link
    Hello! I have two questions today. One is a question of languages. I have been spending a lot of time thinking about the results that are shown here. I've been working on brushing up on C++ and...

    Hello! I have two questions today.

    One is a question of languages. I have been spending a lot of time
    thinking about the results that are shown here. I've been working on brushing up on C++ and Python for professional use, and I have always liked using Python for personal projects since it means that I don't usually need to spend so much time to get where I want to go. However, according to the results linked, Python is one of the most energy intensive languages, so I am a bit interested in learning something else to use for personal projects. I have been interested in web development recently, so I will likely pick up some Javascript along that path. I have been wondering if Javascript would be sufficiently flexible to use it for a wide variety of non-web projects. Should I spend more time on something else? If so, what? I have been looking at Go and Nim as potential alternatives for non-web applications. Or maybe I am just making too big of a deal about energy efficiency?

    The other thing is trying to determine if I am on a bit of a fools errand for an idea I had. I would like to make a time tracker that can keep track of not only what application is currently in focus, but also what applications are out of focus but visible on the current desktop. I have been looking at tools like wmctrl and xdotool, but neither of the seem to be able to determine the layering or visibility of inactive windows that are not minimized. I feel like this must be stored somewhere, or the window manager wouldn't be able to draw multiple windows layered over each other. Is this kind of information something I would have to go directly to the window manager to view? Or it a bit ambitious in general to get that kind of information?

    3 votes
    1. [5]
      unknown user
      Link Parent
      Re. energy: Can I ask you, why do you care? I'm not trying to ask that in an arsehole way, I'm asking because the answer to this question depends on the why. If you want to choose a differrent...

      Re. energy: Can I ask you, why do you care? I'm not trying to ask that in an arsehole way, I'm asking because the answer to this question depends on the why. If you want to choose a differrent language because you care a lot about environment, then buying a used car next time you need a replacement (or ditching the car alltogether if your city planning allows that) sounds to me like it would probably have a much more positive effect on your carbon footprint. On the other hand, if you care about energy because you want to make software for low-energy devices, then you really don't have a lot of choice: It's either C, or C++, or maybe Rust if you're very lucky.

      Re. time tracking: IANA X expert, but you'll probably going to have to dive into the X protocol to get all layers that are being drawn to the screen. I'm talking C API, unless there exists an extensive-enough binding for Python or what-have-you.

      5 votes
      1. [4]
        Gyrfalcon
        Link Parent
        For energy, I am a bit inspired by the work going on at the Low Tech Magazine Solar website, (it might be down right now, it's cloudy!) and some of the discussion surrounding it. I know in general...

        For energy, I am a bit inspired by the work going on at the Low Tech Magazine Solar website, (it might be down right now, it's cloudy!) and some of the discussion surrounding it. I know in general it's not about to make a huge difference to program my own little things in something efficient, but it's cool. Embedded and low power devices are intriguing, though right now I think web stuff and a time tracker have caught my eye.

        For X, I don't think any of the existing bindings cover exactly what I need, so I will probably leave that part out until the end and decide if it's something I would like to dive into. If not, I'll just have to rely on convincing myself I actually cannot focus with a video on my other screen!

        6 votes
        1. [3]
          unknown user
          Link Parent
          Oh, if you're looking for a bit of a challenge and a bit of a coolness factor, then you really should just “follow your arrow” instead of asking the “anonymous experts” on the Internet :-) Create...

          Oh, if you're looking for a bit of a challenge and a bit of a coolness factor, then you really should just “follow your arrow” instead of asking the “anonymous experts” on the Internet :-) Create freely, get things done, have fun.

          Also, that low-tech solar website looks amazing, even just from pure styling point of view. I am definitely stealing some of their CSS. You should consider posting a topic about it, if no one has yet.

          4 votes
          1. [2]
            Gyrfalcon
            Link Parent
            I guess my question was really, is Javascript a general use enough language that I should spend time learning it to use for many projects, or should I just learn enough for my web projects and...

            I guess my question was really, is Javascript a general use enough language that I should spend time learning it to use for many projects, or should I just learn enough for my web projects and focus on something else for other thing? After doing more of my own research I think I am better off with the later case, and Nim looks pretty neat. Thanks for the encouragement.

            I am actually pretty sure I found out about Low Tech from ~, here is a discussion on their article about the site over on ~enviro when it first went up.

            2 votes
            1. unknown user
              Link Parent
              Ah, I see now. People use JavaScript for a lot of things these days, so technically you could write a lot of different stuff with it. My personal opinion is that JavaScript belongs in the browser,...

              Ah, I see now. People use JavaScript for a lot of things these days, so technically you could write a lot of different stuff with it. My personal opinion is that JavaScript belongs in the browser, and even there there shouldn't be too much of it. For personal projects Nim sounds as good as everything else to me.

              5 votes
    2. [2]
      DataWraith
      Link Parent
      arbtt keeps track of what windows are on the screen and logs it to a file you can later query; I'm not sure whether it tracks inactive windows or how it does it, but this might be a program to dig...

      The other thing is trying to determine if I am on a bit of a fools errand for an idea I had. I would like to make a time tracker that can keep track of not only what application is currently in focus, but also what applications are out of focus but visible on the current desktop.

      arbtt keeps track of what windows are on the screen and logs it to a file you can later query; I'm not sure whether it tracks inactive windows or how it does it, but this might be a program to dig into for inspiration.

      But, are you sure you need that level of granularity? I found arbtt to be pretty invasive because it keeps track of everything (e.g. browsed websites via the window titles). Writing the rules required for fine-grained tracking (e.g. "if I'm in $EDITOR and working in ~/code/foo then that is project X") is also a bit of a pain.

      My favorite time tracker is TagTime, which just pops up a "What are you doing?"-window at random. It isn't precise enough for billing clients, but I found it to be great for getting a coarse view of where time goes.

      4 votes
      1. Gyrfalcon
        Link Parent
        TagTime looks interesting, and would probably be a helpful tool for me to avoid distraction. Perhaps I will try it out, since my tool is nowhere near complete. Part of what I want to do is to...

        TagTime looks interesting, and would probably be a helpful tool for me to avoid distraction. Perhaps I will try it out, since my tool is nowhere near complete.

        Part of what I want to do is to combine my current to do list software, Todoist, and a focus gamification tool called Forest. So I would feel the pressure of my tree or whatever losing progress if I don't stay on it, and I could track what I work on to see how long the things I put on my to do list. I suppose it's not strictly that I need exactly this, but I think it would be neat, and it gives me something to program.

        2 votes
  3. [2]
    ruspaceni
    Link
    I've been working on something on/off for a few months now but seemly am making no progress. I've got the individual bits of the project mostly sorted, but this one aspect has me hung up for...

    I've been working on something on/off for a few months now but seemly am making no progress. I've got the individual bits of the project mostly sorted, but this one aspect has me hung up for months...

    What I'm hoping to make is a globe-plotted news blotter. Picture google maps but instead of 'McDonalds' having a marker, its a news story pinned in Chicago. Plus a time slider to go back through the days. There's libraries out there to make that stuff easier like mapbox and cesiumjs.

    My issue is programatically getting the location from news stories. I've tried so many different approaches but I can't really come up with anything better than hand sorting it. The closest I got was using prnewswire, or a service similar to that. But webscraping was a bit too unreliable and I still haven't heard back from the API key request ;c

    I had a bit of a wacky idea of using RSS feeds and just trying to write a program that scans an article and identifies all locations referenced within it. It's been proving to be chronically slow to check reliably.

    This is mostly just a vent post, since I'm fairly sure I'm burnt out on the idea for now. It would have been pretty cool imo though, but maybe I'm just not there yet as a programmer.

    3 votes
    1. PetitPrince
      Link Parent
      I think standardised press release / report from organism such as AP or AFP begin with a statement of where the event has happened. Maybe you should look into that? Both agencies have their own...

      I think standardised press release / report from organism such as AP or AFP begin with a statement of where the event has happened. Maybe you should look into that?

      Both agencies have their own API; maybe it's even a simple attribute to fetch somewhere.

      3 votes