10 ms·
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.
by Animats 9y ago
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.
- revelation 9y agoC's lack of arrays strikes again. They are essentially syntactic sugar.
- frlnBorg 9y agoWhat do you mean by C not having arrays?
- JustSomeNobody 9y agoProbably referring to the fact that they are simply pointers into contiguous memory.
- astrange 9y agoC 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.
- DiThi 9y ago`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)`)
- nemetroid 9y agoThat'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.
- DiThi 9y ago> 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.
- deleted 9y ago[deleted]
- CobrastanJorji 9y agoI 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).
- kinkrtyavimoodh 9y agoIt'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.
- corndoge 9y agoIsn't that the definition of an array? Chunk of contiguous memory plus a notion of how to subdivide it into equal parts?
- kuschku 9y agoYes, 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.
- hedora 9y agoThe 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 };
- kbenson 9y ago> 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.
- 9y ago
- pmontra 9y agoThey are a pointer and an offset with no validation of bounds. But that's OK: C is little more than a high level assembly.
- armitron 9y agoI 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.
- pmontra 9y agoHigh 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.
- antoinealb 9y agoWhat ? Assembly has undefined behavior, for example wrt unaligned access on some processors.
- corndoge 9y agoProgrammer's mistake for not validating data, not the fault of C language mechanics. Yes it would be easier if <hll features>, still gotta be careful. I've made plenty of these mistakes but never blamed the language.
- orf 9y agoIf a manufacturer makes and sells a gun that keeps going off in people's holsters and shooting people in the foot, the answer is not to say "it's the user's fault for not using it properly. I've shot myself in the foot hundreds of times and I don't blame the manufacturer". Or something. That analogy sounded better in my head than written down. The point is that IMO the blame lies squarely with the C language: it's a language that's used in a lot of complex parsing code and provides pretty much nothing to help with this, and if anything actually puts roadblocks in the way.
- corndoge 9y agoI shot myself in the foot yet I don't blame the manufacturer for not putting a safety on the gun since I'm the one that bought it with full knowledge of the caveats
- teraflop 9y agoIf thousands of people were repeatedly making the same mistake, and frequently shooting other innocent bystanders' feet, I would definitely put some blame on the manufacturer.
- corndoge 9y agothe parents analogy is quite bad since C doesn't shoot you in the foot unless you pull the trigger closer to a gun without a safety, which plenty of manufacturers sell
- simonh 9y agoThat doesn't mean that gun is well designed or worth buying and using. Criticising the lack of such a valuable or even essential feature and advising people not to use it would be fair comment. Right?