News for hackers. Stuff that matters.

gandalf013

Re:Climate change: IPCC report is 'code red for humanity' on Monday August 09, 2021 @10:43AM (#28117523)
India's population is approximately 4.5 times that of USA, so the actual ratio is "one American is emitting 35 Indians worth of carbon emissions".
Re:How Many Decimals of Pi Do We Really Need? (2016) on Monday September 28, 2020 @12:24PM (#24618350)
bc calculator doesn't (or didn't) define pi, but has atan. I've used "4*atan(1) method" in bc to get pi.
Re:Math Overflow users resolve PhD thesis crisis on Tuesday August 04, 2020 @12:56PM (#24051931)
Hobbes: Do you have an idea for your project yet?

Calvin: No, I'm waiting for inspiration. You can't turn on creativity like a faucet. You have to be in the right mood.

Hobbes: What mood is that?

Calvin: Last-minute panic.

Re:C is not your friend: sizeof and side-effects on Wednesday April 19, 2017 @06:59PM (#14152540)
A very common idiom is "T p = malloc(n sizeof *p)". That only works if sizeof doesn't evaluate its arguments.
Re:Simple Dynamic Strings library for C on Thursday February 06, 2014 @06:21PM (#7193622)
The standard actually specifies (or specified?) that the flexible array member must come after all the members, including the padding. But, from my reading, that wasn't their intent, and is certainly not how compilers implement it. The link in my original post is an acknowledgement from the committee about this and that the standard needs to be updated.

I think the main reason the OP got away with it is because in his structure, "sizeof(struct sdshdr)" is equal to the offset of "buf" in the struct. This is not necessarily true.

In particular, see the latest C standard draft (http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf), section 6.7.2.1 (Structure and union specifiers), paragraph 18 and 20-21. An example in the standard uses this code:

    struct s { int n; double d[]; };
and says that "but it is possible that"

    sizeof (struct s) >= offsetof(struct s, d) + sizeof (double)
Re:Simple Dynamic Strings library for C on Thursday February 06, 2014 @02:18PM (#7192329)
A more correct version might be:

    #include <stddef.h>

    int buf_offset = (int)offsetof(struct sdshdr, buf);
    struct sdshdr *sh = s - buf_offset;
This is because the compiler might insert padding between your struct elements and the flexible array member. In your case, you're using only int types in the header, so padding shouldn't be an issue on most architectures, but consider the following:

    struct header {
        int i;
        char c;
        char data[];
    };
sizeof(struct header) on my machine is 8, but "data" starts at an offset of 5 from the beginning of the struct. So, to go from "data" pointer to the pointer representing the beginning of the struct, you will need to subtract 5, not 8. Here is a test program:

    #include <stdio.h>
    #include <stddef.h>

    struct header {
        int i;
        char c;
        char data[];
    };

    int main(void)
    {
        struct header h = {0};
        printf("offsetof %zu\n", offsetof(struct header, data));
        printf("sizeof %zu\n", sizeof h);
        printf("start %p, data start %p, delta %d\n",
                (void *)&h, (void *)(h.data), (int)((char *)h.data - (char *)&h));

        return 0;
    }
http://std.dkuug.dk/JTC1/SC22/WG14/www/docs/n983.htm is relevant for this case as well.