Pointer and array processing

Recommended for you: Get network issues from WhatsUp Gold. Not end users.
 The first comparison function following two clear array elements:
  void zero_out_array(int *p, int n)
    *p++ = 0;

  void zero_out_array(int *arr, int n)
  for(int i = 0; i<n; i++)
    arr[i] = 0;
  Obviously, the first function than the second function faster, the reasons are as follows:
  *p++ = 0; Equivalent to *p = 0; p++;     P refers to the contents of variables after clearing, automatic back plus the last constant point to a variable, the constant depends on the size of your variable type.
  arr[i] = 0; * (arr + I) = 0; but the actual operation situation much worse, at the bottom of the machine code level runs like this, * (arr + (i*4)) = 0; (if the variable type is int type words), thus, the address must be run again and again in the calculation, and the pointer version, address arr only needs to value a, P during the cycle every time is incremented by 4, this is just a simple operation.
  Image: pointer version, is the first step towards zero, second step, second step third step to clear,……
  While the second version is the first step, clear, the next, from the first step back to find (scaling) position clear, the second step of the next, from the first step back to find (proportional operation) the third step.……
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download

Posted by June at November 18, 2013 - 10:48 AM