Live data from Hacker News

How to find size of an array in C without sizeof

arjunsreedharan.org

181–190 of 212 posts

Re: How to find size of an array in C without sizeof

#182
post #94
post #65

Earlier quoted context omitted.

I don't think that's universally true, some languages are quite sound and logical at their core.

Name a language and someone will point out WTFs and subtle issues you might have never considered. The problem is the same when designing a language: computers think best with a large set of simple rules, humans think best with a small set of complicated rules, each having exceptions and different tiers of complexity for different levels of each developer's understanding. I find C somewhat logical, but it has an easy…

Not all languages have equal wartiness. PHP and JavaScript are built on a swamp of inconsistencies whereas Haskell has a core axiomatic language. Scheme is fairly clean.

C evolved in a different age and is fraught with undefined behavior.

Re: How to find size of an array in C without sizeof

#183

Earlier quoted context omitted.

Is there a circle in hell reserved for C standards committee members who add to the number of cases where 'undefined behavior' occurs in the standards?

Just how would you make this case defined ? The alternatives to undefined behavior tend to be (1) being silent about the issue and letting users and implementers find out themselves (2) defining the behavior in a way that makes it difficult to implement on some architectures or (3) defining the behavior in a way that imposes costs on all architectures. None of these is particularly attractive.

I would go with option 1 because it does not suggest the problem is solved. As I write this, I realize that there is a counter-argument in that introducing ptrdiff_t gives you a place to issue a warning.

Re: How to find size of an array in C without sizeof

#184

Earlier quoted context omitted.

In many more theoretical CS classes, programming is not a requirement.

There's a word for that. It's "math".

Computer Science and maths are heavily intertwined [1], just like physics and maths are.

[1]: http://math.stackexchange.com/q/649408

Re: How to find size of an array in C without sizeof

#185

Earlier quoted context omitted.

I'll appreciate if you could provide a screenshot :)

(1)- Taken using chrome extension: http://imgur.com/a/PYMRD (2)- Print screen of chrome version 54.0.2840: http://imgur.com/a/dDwK9 (3)- Print screen of internet explorer version 11 : http://imgur.com/a/uxAv5 All running on 32bit windows 8.1

Author might have fixed it already, but it looks alright to me on Chrome 55.0.2883.95 / macOS 10.12.

http://imgur.com/a/fhDr1

Re: How to find size of an array in C without sizeof

#186

Author of the article here. There's no intention here to encourage people to use this in code (in fact the opposite). This article is more of a "Did you know cool shit like this exist?".

Please fix your site's header :)

I dug deeper and the problem was at my end. The computer I am using has a parental control software installed and configured to block certain websites including twitter which caused the author's site not to load all the needed assets and screwed up the page header. sorry for the inconvenience but I would have been able to figure it out quicker than this if people who down-voted my comment took the time to tell that the site is working fine for them!

Re: How to find size of an array in C without sizeof

#187
post #185

Earlier quoted context omitted.

(1)- Taken using chrome extension: http://imgur.com/a/PYMRD (2)- Print screen of chrome version 54.0.2840: http://imgur.com/a/dDwK9 (3)- Print screen of internet explorer version 11 : http://imgur.com/a/uxAv5 All running on 32bit windows 8.1

Author might have fixed it already, but it looks alright to me on Chrome 55.0.2883.95 / macOS 10.12. http://imgur.com/a/fhDr1

I dug deeper and the problem was at my end. The computer I am using has a parental control software installed and configured to block certain websites including twitter which caused the author's site not to load all the needed assets and screwed up the page header. sorry for the inconvenience but I would have been able to figure it out quicker than this if people who down-voted my comment took the time to tell that the site is working fine for them!

Re: How to find size of an array in C without sizeof

#188
post #88

Earlier quoted context omitted.

Technically, this needn't impact malloc, because dereferencing the "one past the end" address is still undefined. All you need is logic in your pointer arithmetic that essentially treats the past the end address as a special value (which normally would never need to be represented).

Not just pointer arithmetic; also pointer comparisons. One-past-the-end pointer must compare greater-than any other pointer into that array. So an implementation that could stick an array at the very end of the address space, and do wraparound for one-past-the-end so that it's represented by all bits zero, would then need to special-case that zero value when performing any pointer comparisons.

Oh, I was assuming a runtime that didn't do wraparound. If you are doing wraparound, you already have a ton of special case work you'd have to do.

But yes, you'd need logic for pointer comparisons as well.

Re: How to find size of an array in C without sizeof

#189

Earlier quoted context omitted.

The problem you're latching on to I think is how the context for caculating a probability can vary. If it were really as likely as, say, the sun exploding that X happened then it would be of no use to expend time on X. BUT very often people speaking about the probability of events given suspicious constraints. While a memory allocation might not fail in most situations it will fail often in some situations. And a one…

That's why it's called one in a million...

One in a million isn't just a typical statement of probability, it's a colloquialism used to refer to things that never happen in practice. It's highly misleading to use in the context of computers which, due to their natures, have one in a million events occurring constantly.

Re: How to find size of an array in C without sizeof

#190

Earlier quoted context omitted.

That's why it's called one in a million...

One in a million isn't just a typical statement of probability, it's a colloquialism used to refer to things that never happen in practice. It's highly misleading to use in the context of computers which, due to their natures, have one in a million events occurring constantly.

My comment was tongue in cheek.
Post reply on HN