Most active commenters

    ←back to thread

    387 points pedro84 | 21 comments | | HN request time: 0.63s | source | bottom
    Show context
    Animats ◴[] No.14860964[source]
    C's lack of array size info strikes again:

        memcpy(current_wmm_ie, ie->data, ie->len);
    
    where "ie" points to data obtained from the net.
    replies(2): >>14861129 #>>14861284 #
    revelation ◴[] No.14861129[source]
    C's lack of arrays strikes again. They are essentially syntactic sugar.
    replies(1): >>14861235 #
    1. frlnBorg ◴[] No.14861235[source]
    What do you mean by C not having arrays?
    replies(5): >>14861254 #>>14861256 #>>14861257 #>>14861262 #>>14861281 #
    2. JustSomeNobody ◴[] No.14861254[source]
    Probably referring to the fact that they are simply pointers into contiguous memory.
    replies(1): >>14862377 #
    3. ◴[] No.14861256[source]
    4. CobrastanJorji ◴[] No.14861257[source]
    I assume they mean that C's support for "arrays" is essentially just C's support for pointer arithmetic plus a mapping of a[b] to *(a+b).
    replies(2): >>14861354 #>>14862014 #
    5. kinkrtyavimoodh ◴[] No.14861262[source]
    It's syntactic sugar in the sense that arr[i] is just shorthand for *(arr+i)

    There's no abstraction or 'concept' of arrays there. You are literally just telling the compiler to take a certain pointer and move i steps ahead.

    replies(1): >>14861315 #
    6. pmontra ◴[] No.14861281[source]
    They are a pointer and an offset with no validation of bounds. But that's OK: C is little more than a high level assembly.
    replies(1): >>14861607 #
    7. corndoge ◴[] No.14861315[source]
    Isn't that the definition of an array? Chunk of contiguous memory plus a notion of how to subdivide it into equal parts?
    replies(2): >>14861327 #>>14861945 #
    8. kuschku ◴[] No.14861327{3}[source]
    Yes, exactly. A chunk of memory. A chunk has an end.

    But "arrays" in C aren't a chunk of memory, just the info where it starts and how large elements are.

    replies(1): >>14862551 #
    9. Kubuxu ◴[] No.14861354[source]
    You can even swap it a[b] equivalent to b[a].
    10. armitron ◴[] No.14861607[source]
    I hate the term "high level assembly" when applied to C since it's loaded against assembly (in the sense of C as its superset) which is obviously not true.

    C is full of undefined behavior, assembly is not.

    replies(2): >>14863893 #>>14864773 #
    11. shitgoose ◴[] No.14862014[source]
    shouldn't it be:

    * (a + b * sizeof(T))

    where T is array elements' type?

    replies(1): >>14862368 #
    12. astrange ◴[] No.14862368{3}[source]
    That's implied in + on a pointer. uint32_t* + 1 actually adds 4.
    13. astrange ◴[] No.14862377[source]
    C doesn't have "memory" in the standard. They're pointers into a contiguous object, but anything before a[-1] or after a[sizeof(a)-1] is undefined aka it actually doesn't exist.
    replies(1): >>14885621 #
    14. hedora ◴[] No.14862551{4}[source]
    The problem is that a frightening number of people don't bother to write the half-dozen obvious wrappers around this, and stdlib doesn't provide them either:

    struct buf { uint8_t * base, size_t size };

    replies(2): >>14862626 #>>14862983 #
    15. kbenson ◴[] No.14862626{5}[source]
    > The problem is that a frightening number of people don't bother to write the half-dozen obvious wrappers around this

    That's likely because having to pass it in and out of functions and libs that don't expect your special structure might cause it to have an invalid length, and then all your special wrappers can become a liability and not an advantage through either assuming your bufs are valid, or defensively checking more than is necessarily because they can't know whether it was altered or not.

    > stdlib doesn't provide them either

    Which is the real problem. That would make them a de facto standard, and a lot (but probably not all) of the problems would be mitigated by people accepting the performance trade offs needed to make them safe.

    16. kbart ◴[] No.14862983{5}[source]
    That doesn't solve the problem because you still have to set 'size' manually.
    17. pmontra ◴[] No.14863893{3}[source]
    High level != Superset

    Actually we often give up features and specialize with high level languages. That's why there are things easier to do in Ruby than in C++ and vice versa.

    They are all (usually much more convenient) subsets of assembly.

    18. antoinealb ◴[] No.14864773{3}[source]
    What ? Assembly has undefined behavior, for example wrt unaligned access on some processors.
    19. DiThi ◴[] No.14885621{3}[source]
    `sizeof(a)` only gives the size of the array when the size is specified at compile time. Either you accept e.g. `int[16]` as a type, or you pass a pointer (for which `sizeof` just returns `sizeof(intptr_t)`)
    replies(1): >>14887915 #
    20. nemetroid ◴[] No.14887915{4}[source]
    That's not quite right. Arrays always have a knowable length, and sizeof will give a correct result for variable length arrays as well.

    However, arrays that are passed as arguments to functions decay into raw pointers, at which point you lose information about its length.

    replies(1): >>14890975 #
    21. DiThi ◴[] No.14890975{5}[source]
    > sizeof will give a correct result for variable length arrays as well.

    In C99 with rather spotty support. And never with malloc and similar, which is how the vast majority of arrays are (and can be) created. And you can't return or store those dynamic arrays somewhere else without losing the size info, nor it can be declared static.

    In other words, you're right but for very limited situations.