News for hackers. Stuff that matters.

mnaydin

Re:Few lesser known tricks, quirks and features of C on Monday February 20, 2023 @07:52AM (#34867615)
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.
Re:Few lesser known tricks, quirks and features of C on Monday February 20, 2023 @06:40AM (#34867211)
This is an infinite loop since the expression

    x <= max
is always true.
Re:Print this file, your printer will jam (2008) on Monday December 28, 2020 @12:46PM (#25561252)
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.
Re:An introduction to data processing on the Linux command line on Saturday November 23, 2019 @08:29PM (#21617762)
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
Re:An introduction to data processing on the Linux command line on Saturday November 23, 2019 @08:05PM (#21617668)
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
Re:Textbook errors in binary searching (1988) on Wednesday February 27, 2019 @11:37AM (#19264297)
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.
Re:Textbook errors in binary searching (1988) on Tuesday February 26, 2019 @10:55PM (#19260736)
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.
Re:Textbook errors in binary searching (1988) on Tuesday February 26, 2019 @10:29PM (#19260612)
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
  }
Re:Problem solving with Unix commands on Thursday February 14, 2019 @08:58PM (#19167887)
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.
Re:Inside the C Standard Library on Monday January 21, 2019 @09:12AM (#18960445)
"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.

Re:The Awk Programming Language (1988) [pdf] on Sunday January 22, 2017 @02:34PM (#13456845)
That's the expected behaviour. Quoting from spec:

If no expression is present, -print shall be used as the expression. Otherwise, if the given expression does not contain any of the primaries -exec, -ok, or -print, the given expression shall be effectively replaced by: ( given_expression ) -print

http://pubs.opengroup.org/onlinepubs/9699919799/utilities/fi...

Re:The Awk Programming Language (1988) [pdf] on Sunday January 22, 2017 @06:08AM (#13454684)
Neither -b nor -B option is defined in the POSIX ls utility. The -b option is a GNU extension to print C-style escapes for nongraphic characters, and it is useful for this case. The -B option is also GNU extension not to list implied entries ending with ~, and I don't understand how it is relevant to this case.
Re:The Awk Programming Language (1988) [pdf] on Sunday January 22, 2017 @06:06AM (#13454677)
I'd use the find command with the -printf option (GNU find has this option but POSIX find doesn't define it) instead of ls. For instance:

find /path/to/dir -type f -printf "%s\n" | awk ' { s += $0 } END { print s " bytes" } '

The find command has much powerful file filtering capabilities than that of the ls command and works better with weird characters in filenames.