Live data from Hacker News

How to find size of an array in C without sizeof

arjunsreedharan.org

101–110 of 212 posts

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

#101
post #30

Earlier quoted context omitted.

Quite. This exactly the sort of thing that makes C such a fun language.

I'm not sure if it's a praise for C though. Arcane design and lack of clarity might be fun to decipher, but it's not something that you'd want to see in the programming language.

Understanding pointers and pointer arithmetic is fundamental to understanding c. Most books and courses would spend a considerable amount of time and effort to make sure the student understands that. So 'arcane' is the wrong word I think.

You just need to get it, and really its no harder than, say context managers in python, or promises in js. Its not relevant at what 'level' those constructs are. They are novel in they way in which they model and solve real problems in context.

So 'lack of clarity' is really due to misunderstanding the context and problem space the langue was made to operate in.

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

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

If you're taking CS classes and boasting about how you don't know how to program, it is a problem.

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

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

Wait, wait.

  char *p = x;
  p += 36; // overflow?
As arr == &arr, so are pointers P and Q that point just after last array item (1+&x[35]) and just after entire array (1+&x). As 6.5.6.8 above said, P is okay, and so must be Q. They said about last element, not second. Can you please explain why is x+1 even an argument?

>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 65499?

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

#104

Earlier quoted context omitted.

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

On a 64-bit platform (anything modern), ptrdiff_t is going to be 64-bit so this will not be an issue (ok, 63-bit... but you get my point.)

In this context, the difference between 63 bits and 64 bits is not trivial.

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

#105
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.

Just because you grew up with C and know many of the details doesn't make it necessary for others to know that much, especially when the job doesn't call upon it. I grew up with C as well, but I understand it is possible to make meaningful contributions without understanding C.

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

#106

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?

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

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

If you're taking CS classes and boasting about how you don't know how to program, it is a problem.

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

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

#108
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.

>I think the article isn't obvious only to those who have a weak understanding of the language. Hi! Could you take a guess at what percentage of C programmers who write C professionally fit your definition of that (I realize you were being hasty in your phrasing, but still)? Obviously your answer should be betweeen 0% (no programmer who writes C professionally) and 100% (every programmer who writes C professionally.)…

I think less than 15% of professional C programmers have a weak understanding of the language. One only needs a basic understanding of pointer arithmetic to understand why `(&arr + 1) - arr` is the size of the array.

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

#110
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 suppose that this is the case. But really, to me this is article does not reveal anything beyond what I already knew from basic pointer arithmetic.

Post reply on HN