Live data from Hacker News

How to find size of an array in C without sizeof

arjunsreedharan.org

141–150 of 212 posts

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

#141
post #84

Earlier quoted context omitted.

Which kind of explains so much about the problem with C. ;-)

I think it more explains that you can do a lot without fully understanding what it is you are working with. Which can be good or bad.

> I think it more explains that you can do a lot without fully understanding what it is you are working with.

That's the same thing I'm saying. :-)

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

#142

The result you get with this trick is signed, while the result you get with sizeof is unsigned. Edit: Just to clarify, what you get is ptrdiff_t instead of size_t. So if array size is greater than PTRDIFF_MAX, you get undefined behavior [1]. [1] http://en.cppreference.com/w/c/types/ptrdiff_t

How likely do you run into array bigger than 2gb?

Oh surprisingly easily. Say you're handling a few billion cookies in RAM or manipulating DNA data.

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

#143
post #112
post #82

Earlier quoted context omitted.

Where is it dereferencing the array? *(&arr + 1) - arr That translates to taking the address one point past the array and subtracting the address of the array from it. It doesn't actually dereference the location past the end of the array. While: (&arr)[1] - arr might appear to be doing something different, it actually isn't.

&arr is a pointer to an array (it points to the existing array). &arr + 1 is a pointer to an array that begins just after the existing array. * is the dereference operator, so it seems to me that *(&arr + 1) dereferences the pointer to the array, resulting in an array (or a reference to an array), which then decays to a pointer.

Ah, but since it is dereferencing an array... you haven't actually dereferenced to the memory location yet. If you had, you wouldn't be possible to subtract a pointer from it.

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

#145
post #84

Earlier quoted context omitted.

Which kind of explains so much about the problem with C. ;-)

I think it more explains that you can do a lot without fully understanding what it is you are working with. Which can be good or bad.

I think the important irony that perhaps wasn't plainly obvious in my statement is that C is often cited by programmers as a preferred tool (particularly over Java or C++) because they "know exactly what is going on with each line of code". ;-)

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

#146

Earlier quoted context omitted.

But it's extremely unlikely when you're processing 10 requests a week, such as might be the case for the web server in a consumer-grade router.

It's amazing how skilled blackhats are at converting "rare bug that doesn't affect the UI" into "massive DDoS cannon".

And you can see how risk analyses by senior engineers with tons of embedded experience who are used to working with systems that are not networked leads to problems when their systems are later networked.

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

#147

Earlier quoted context omitted.

How likely do you run into array bigger than 2gb?

A few months ago I was doing FFTs on arrays larger than 4GB. Amusingly, this uncovered a bug in the LLVM optimizer: It was looking at stride lengths to figure out if accesses were independent, and truncated a 4GB stride down to 0.

I would be extremely interested to hear how you found this bug. Sounds like a difficult bug to track down, and I always learn from good debugging stories.

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

#148

The result you get with this trick is signed, while the result you get with sizeof is unsigned. Edit: Just to clarify, what you get is ptrdiff_t instead of size_t. So if array size is greater than PTRDIFF_MAX, you get undefined behavior [1]. [1] http://en.cppreference.com/w/c/types/ptrdiff_t

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?

I hope not. One of the reasons for allowing undefined behaviour is to allow room for compilers to perform certain optimizations that may not be possible if a specific result were required.

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

#149

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'll appreciate if you could provide a screenshot :)

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

#150

The result you get with this trick is signed, while the result you get with sizeof is unsigned. Edit: Just to clarify, what you get is ptrdiff_t instead of size_t. So if array size is greater than PTRDIFF_MAX, you get undefined behavior [1]. [1] http://en.cppreference.com/w/c/types/ptrdiff_t

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.
Post reply on HN