Live data from Hacker News

How to find size of an array in C without sizeof

arjunsreedharan.org

91–100 of 212 posts

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

#91
post #76

Earlier quoted context omitted.

>I think this article made a lot of people feel stupid I don't think so. Anyone with a solid understanding of C understands pointer arithmetic. I think the article isn't obvious only to those who have a weak understanding of the language.

There are many that have a weak understanding of the language. I got asked some years back why I defaulted to C in some interview questions -- I grew up with the language, understand the nuances and many of the implementations. It's now possible to make your way through a university education in CS without ever touching or understanding C. This is a problem.

> It's now possible to make your way through a university education in CS without ever touching or understanding C. This is a problem.

I did not study CS, but I had a number of CS modules/classes. LaTeX was the only programming language I recall using. Students with better handwriting could probably get away with not doing any programming at all.

It's not clear to me that this is a problem, but I imagine that the systems requirement of most CS programs will involve C.

For what it's worth, I default to python in interviews even though it's my least favorite out of the languages that I use frequently.

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

#92

For the completeness sake, the size of an array can also be computed via linker symbols, see for example: http://stackoverflow.com/questions/29901788/finding-the-last... . Same constraints apply (pointer arith). I am not sure why this method, applied to ordinary arrays, would be preferred to sizeof (), but since we're shedding light here... EDIT: pointer arith constraints only apply if we compute the difference (end…

Do all linkers guarantee not to round this up to a word size?

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

#93
post #31

Earlier quoted context omitted.

this is undefined behavior. &arr + 1 can overflow No. From 6.5.6 Additive operators: 7 For the purposes of these operators, a pointer to an object that is not an element of an array behaves the same as a pointer to the first element of an array of length one with the type of the object as its element type. 8 [...] if the expression P points to the last element of an array object, the expression (P)+1 points one past…

But arr != &arr even though they have the same value. #8 applies to arr (P), but in the post OP is using &arr which is a ptr to array[x] and doesn't apply to it.

I know it's not what you were trying to communicate, but actually "arr != &arr" is false.

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

#94
post #65

Earlier quoted context omitted.

Doesn't every language turns into insanity to decipher once you look close enough?

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-to-learn simplified version of itself that can be learned before re-reading the spec to complete your understanding of the language.

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

#95
post #91

Earlier quoted context omitted.

There are many that have a weak understanding of the language. I got asked some years back why I defaulted to C in some interview questions -- I grew up with the language, understand the nuances and many of the implementations. It's now possible to make your way through a university education in CS without ever touching or understanding C. This is a problem.

> It's now possible to make your way through a university education in CS without ever touching or understanding C. This is a problem. I did not study CS, but I had a number of CS modules/classes. LaTeX was the only programming language I recall using. Students with better handwriting could probably get away with not doing any programming at all. It's not clear to me that this is a problem, but I imagine that the sys…

> I default to python in interviews even though it's my least favorite out of the languages that I use frequently.

Why is that ? I would image that you'd use the language you are most comfortable with and trust the most during an interview ? What makes Python a good 'interview' language but a less good bread and butter language for you ?

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

#96

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 happens quite often if you're processing something like ~100k requests a second.

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

#97
post #81
post #71

Earlier quoted context omitted.

How so? If the array has five elements, you can pass &arr to a function that expects an int[5][], and that function certainly can build a pointer that points one past the last element. Likewise, the compiler ensures that you can build &arr[5] and that is the same address as &arr+1. &arr+1 cannot overflow.

The compiler guarantees that arr + 1 doesn't overflow by making sure arr's address is small enough to not overflow when accessing one element past the array size. &arr + 1 is not one past the array you asked the compiler to allocate. if you're on a 16bit system and you define char x[36], the compiler guarantees that x's address is not more than 65500. if you do &x + 1 then you'll overflow, x + 1 won't. You can pass w…

Then you have a broken compiler.

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

#98
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…

Sure, any language can have hard to understand parts or things that aren't designed the best way. But C is pretty old language. Creators of programming languages learned quite a lot since it was made, so they can avoid repeating known mistakes and can use newer design ideas. I.e. the quality of new languages can improve since they stand on the shoulders of giants.

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

#99
post #91

Earlier quoted context omitted.

> It's now possible to make your way through a university education in CS without ever touching or understanding C. This is a problem. I did not study CS, but I had a number of CS modules/classes. LaTeX was the only programming language I recall using. Students with better handwriting could probably get away with not doing any programming at all. It's not clear to me that this is a problem, but I imagine that the sys…

> I default to python in interviews even though it's my least favorite out of the languages that I use frequently. Why is that ? I would image that you'd use the language you are most comfortable with and trust the most during an interview ? What makes Python a good 'interview' language but a less good bread and butter language for you ?

If he's listing LaTeX as a programming language and talking about how little programming he had, I suspect he's stuck with R or Matlab.

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

#100
post #51

Earlier quoted context omitted.

The "how likely is it, really?" response to questions of technical correctness has always bothered me. It takes a mindset completely alien to mine to say "Here's a race condition. Sure, it's undefined behavior, but the race is narrow, so it's rare" or to say "Sure, memory allocation can theoretically fail, but in practice almost never does" or to say "fsync is too slow and most computers have batteries these days". S…

Engineering is about tradeoffs. How many once-in-a-thousand bugs do you fix before you tackle the one-in-a-million? Or one in a billion? What about if it takes $10/bug to fix every 1:1000 bug and $100,000 to fix one 1:1000000 bug? Correctness is great in theory, but in practice it's a matter of what's important.

If you are only looking at probability and cost-to-fix you are overlooking something important - the cost if/when it happens.
Post reply on HN