News for hackers. Stuff that matters.
Re:Walter Bright on C's Biggest Mistake by rbranson on Thursday December 24, 2009 @07:55PM (#1014617)
I don't think arrays are "converted" to pointers. Arrays are simply a cleaner way of doing pointer arithmetic and allocating large(r) blocks of the stack. Nothing is lost in this "conversion." The array never knows it's own dimensions beyond the time you declare it. It's up to the developer to keep track of that.
Re:Walter Bright on C's Biggest Mistake by agazso on Thursday December 24, 2009 @08:28PM (#1014653)
Here is an example that caused me a few bugs.

  // define a new type called md5_t
  typedef char md5_t[33];
  md5_t g_md5;
  // here sizeof(g_md5) == sizeof(md5_t)
  
  void f(md5_t md5)
  {
    // here sizeof(md5) == sizeof(char*)
  }
The type information definitely lost inside functions.
Re:Walter Bright on C's Biggest Mistake by ori_b on Thursday December 24, 2009 @10:46PM (#1014737)
Simple rule: Don't use sizeof on pointers. And don't typedef arrays to make them look like simple types.
Re:Walter Bright on C's Biggest Mistake by agazso on Friday December 25, 2009 @06:57AM (#1014965)
A better simple rule: always use sizeof on typename. That always works therefore you can use typedef arrays too.
Re:Walter Bright on C's Biggest Mistake by rikthevik on Friday December 25, 2009 @10:44AM (#1015049)
Totally agree. If you start relying on the subtle behaviour of sizeof, it's just asking for a world of trouble. It's so easy to get mixed up with whether it's going to return the length of the array, the size of the elements in the array, the total number of bytes the array is using or the size of the pointer. Come to think of it, I think that's completely scope dependent and depends on whether the array has lots its array-ness. I have no idea. :)
Re:Walter Bright on C's Biggest Mistake by rbranson on Thursday December 24, 2009 @08:45PM (#1014663)
This works, sure, but if you do:

        char test[1024][32];
        printf("%i\n", sizeof(test));
You'll get 1024 * 32 (32,768). It didn't know that it's a 2D array. When you pass a stack-allocated array, it passes by pointer. It works this way because C is portable assembler.
Re:Walter Bright on C's Biggest Mistake by barrkel on Thursday December 24, 2009 @09:04PM (#1014676)
There is no logical connection between the use of the `sizeof` operator and differentiating between 1D and 2D arrays, so "it didn't know that it's a 2D array" makes no sense.

Secondly, it's not really meaningful to talk about "pass by pointer". Parameters are generally either passed by value, by reference, or by name. C only does pass by value; to emulate pass by reference, you need to pass the address by value, as a pointer - or as an "array". But the only array type you can declare is missing the crucial dimension length aspect.

Finally, saying that C works the way it does because it's portable assembler does not have strong explaining power. Arrays are not usually machine-level concepts; pointers with appropriate declaration syntaxes for static and automatic block allocations could take their place in C. Since C does have a higher-level construct called an array, it's not a big leap to imagine it being better designed.

Re:Walter Bright on C's Biggest Mistake by nitrogen on Friday December 25, 2009 @01:47AM (#1014824)
sizeof() is used to determine the actual in-memory size of something for use by memcpy/malloc/etc., not the number of elements. There are commonly-used array size macros that can help with this, such as

  #define ARRAY_SIZE(x) sizeof(x)/sizeof((x)[0])
Re:Walter Bright on C's Biggest Mistake by ori_b on Thursday December 24, 2009 @10:45PM (#1014736)
Arrays are converted to pointers on the first use. You can't ever use an array in C.