Hacker Newsnew | past | comments | ask | show | jobs | submit | mnaydin's commentslogin

I am not able to understand how

  int (*ap3)[900000] = malloc(sizeof *ap3);
is nicer than

  int *a = malloc(900000 * sizeof *a);
Notice that, in the former case, the array elements must be accessed as (*ap3)[i] whereas in the latter case the usual method a[i] is fine.


Me neither. In the VLA use cases page he does that with multidimensional array too.

    int (*arr)[n][m] = malloc(sizeof *arr);
which you have to access with

    (*arr)[i][j]
I prefer doing

    int (*arr)[m] = malloc(n*sizeof(*arr));
though this separates m and n to be one on left side while the other on the right side, it allows me to index directly

    arr[i][j]


You don't use this technique to define a new array, but to get a pointer to an array you already have.

The goal is to have a pointer to the array, and not a pointer to the first element of the array.

Whether this is "nicer" or not, and whether this is what you need in your application, are out of the scope of the fine article.


This is equivalent to

  while (1) {
    calc(x++);
  }
which is an infinite loop, since the expression x <= max is always true


Heh! Good catch! That is what I get for not testing. After UINT_MAX, x wraps around to 0.


I am reminded of Fortran 77. In Fortran 77, the first character of a record to be printed out determined vertical spacing as follows:

    Blank     One line
      0       Two lines
      1       To first line of next page
      +       No advance


There were no bitwise operators in the Atari BASIC, and numbers in the tokenized program were always stored as floating-point numbers in BCD (Binary Coded Decimal) format.


I wouldn't use awk for simple things such as

  cat sales.csv | awk -F',' '{print $1}'
but I'd prefer

  cut -d, -f1 sales.csv


The awk command

  cat sales.csv | awk -F',' '{print $1}' | sort | uniq
can even be further simplified to

  cut -d, -f1 sales.csv | sort -u


The proposed solution for C and C++

  mid = ((unsigned int)low + (unsigned int)high)) >> 1;
in the blog may still be wrong if the sum ((unsigned)low + (unsigned)high)) produces a carry.


Max 32-bit signed int is (1<<31)-1 . Twice that is 1<<32-2.

Max 32-bit unsigned int is (1<<32)-1.

Therefore, if 'low' and 'high' are non-negative 32-bit signed integers, it is impossible for their sum, when the values are first cast to 32-bit unsigned integers, to produce a carry.


True. I had thought low and high could be unsigned originally, in which case casting to unsigned is redundant, then the sum would produce a carry.


A modern compiler would warn about the condition (ii >= 0) being always true.

One correct way would be

  for (size_t i = len; i > 0; --i) {
     // use i-1 as the array index
  }
or,

  size_t i = len
  while (i-- > 0) {
    // use i as the array index
  }


This can be more shortened to

  ls -1 | uniq -u -w4
using GNU uniq, for these special filenames. Unfortunately, Posix does not define -w option for uniq.


"C99 provides a macro SIZE_MAX with the maximum value possible in size_t. C89 doesn’t have it, although you can obtain the value by casting (size_t)-1. This assumes a twos’ complement architecture,..."

The architecture does not have to be two's-complement. The value of the expression (size_t)(-1) will always be SIZE_MAX, the largest number that can be represented in the size_t type. The bit pattern may change depending on the architecture, but the value shall equal to SIZE_MAX.


Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: