←back to thread

387 points pedro84 | 4 comments | | HN request time: 0.001s | source
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 #
frlnBorg ◴[] No.14861235[source]
What do you mean by C not having arrays?
replies(5): >>14861254 #>>14861256 #>>14861257 #>>14861262 #>>14861281 #
JustSomeNobody ◴[] No.14861254[source]
Probably referring to the fact that they are simply pointers into contiguous memory.
replies(1): >>14862377 #
1. 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 #
2. DiThi ◴[] No.14885621[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 #
3. nemetroid ◴[] No.14887915[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 #
4. DiThi ◴[] No.14890975{3}[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.