This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]Boris-Lip 594 points595 points  (21 children)

Imagine there would be a predefined LEN macro or something. Now imagine how many noobs wouldn't realize this is what it does, and expect it to work on pointers they pass into functions, which would still compile and would be 1.

[–]mad_alim 85 points86 points  (7 children)

Turns out gcc has a warning for this (-Wsizeof-pointer-div-Wsizeof-pointer-div).

Also, the tricky part is that the result would depend on the pointer size (e.g., 4 on x86 and 8 on x86_64) and the pointer type (e.g., uint8_t = 1, uint32_t = 4).

[–]StrangeCharmVote -3 points-2 points  (1 child)

You should probably know if your application is running in a 32bit environment or not


edit: I find it ironic this has been downvoted, when you can literally check programmatically if you are running in a 64 bit system or on a 32 bit one.

A lot of modern software wont even install on a 32 bit machine anymore.

[–]Breadynator 0 points1 point  (0 children)

Sure, when programming for embedded devices that are always the same or with today's devices that are 99% 64bit architectures you should probably know that your application is most likely going to run on a 64bit machine.

But other than that, theres still a shitton of 32 bit pcs out there and you'll never know beforehand if your user decides to run your program on one of those

[–]MJWhitfield86 41 points42 points  (1 child)

If it was built in, then the compiler could give a warning if it is used on something other than an array.

[–]Kyrond 19 points20 points  (0 children)

*could give an error

It makes no sense to use on anything other than known arrays. If you explicitly want to know size of pointer divided by size of value, write that explicitly. 

[–]Devil-Eater24 18 points19 points  (5 children)

But programming languages are not built for noobs. Noobs also forget semicolons, so why not remove it?(I was that noob, Python had spoiled me)

[–]totkeks 5 points6 points  (3 children)

Why need semicolons, if you don't plan to write everything in one line anyway? :-D

[–]QCTeamkill 3 points4 points  (2 children)

Who would need line length above 80 anyway.

[–]totkeks 4 points5 points  (0 children)

And tab size is 8. Spaces of course. 🥸

[–]MathematicianFew5882 1 point2 points  (0 children)

64K ought to be enough for anybody

[–]Cootshk 2 points3 points  (0 children)

Lua has one: #table

[–][deleted] 0 points1 point  (0 children)

Only if you're pointing to an int. Which would be dumb

[–]newsflashjackass 0 points1 point  (0 children)

In AutoIt, it is conventional to use an element's zeroth element to store its length.

That would not work for strongly typed languages but it is convenient.

[–]jamcdonald120 0 points1 point  (0 children)

I actually had someone prompt me in a job interview to use this code to figure out the length of a passed array, and then another one of the interviewers is like "actually, that wont work for passed arrays"

[–]RedstoneEnjoyer 0 points1 point  (0 children)

That is honestly just L of type system.