UNIX Socket FAQ

A forum for questions and answers about network programming on Linux and all other Unix-like systems

You are not logged in.

  • Index
  • » C
  • » Looking for a bit of help.

#1 2009-05-02 08:50 PM

sjcc
Guest

Re: Looking for a bit of help.

Given the following array and pointer declarations, show two different code fragments to calculate and display the sum of the elements of the array x. One approach is to subscript the array in a loop over the elements of the array. The other approach is to use a combination of advancing the pointer ptr thru the array and dereferencing it.



#define SIZE_D(array) (sizeof(array) / sizeof(array[0]))
//...
double x[] = {3.14, 2.7, 2.1, 1.41, 100.1};
const int X_CAP = SIZE_D( x );
double *ptr = x;

hey guys i've been trying to figure out this problem for the last hour or so. Can anyone help me out?

thanks

#2 2009-05-03 12:11 AM

i3839
Oddministrator
From: Amsterdam
Registered: 2003-06-07
Posts: 2,239

Re: Looking for a bit of help.

What part of the question do you not understand?

It's hard to help you if we don't know what the problem is. (Answering the
actual assignment is easy but won't really help you solve similar ones in the
future.) But to get you moving in the right direction:

int a[] = {1, 2, 3, 4};
int i;
int* p;
int max = 4;
int total1 = 0;
int total2 = 0;

/* Array subscription */
for (i = 0; i < max; i++){
	total1 += a[i];
}

/* Pointer arithmetic */
for (p = &a[0]; p < &a[max]; p++){
	total2 += *p;
}

As you see, array subscription is the better way when having a choice.

Offline

#3 2009-05-03 05:28 PM

RobSeace
Administrator
From: Boston, MA
Registered: 2002-06-12
Posts: 3,839
Website

Re: Looking for a bit of help.

for (p = a; p < a + max; p++)

Offline

#4 2009-05-04 09:41 AM

i3839
Oddministrator
From: Amsterdam
Registered: 2003-06-07
Posts: 2,239

Re: Looking for a bit of help.

I suppose you're right. Though I'm not sure it makes much difference at all as
far as efficiency goes, at least practically. It would surprise me.

If you use the array element more than once and especially if it's a structure
then using a pointer can be much cleaner. But in trivial cases like this I think
the extra indirection just makes it slightly less clear what's going on. *shrug*

Offline

#5 2009-05-04 12:36 PM

RobSeace
Administrator
From: Boston, MA
Registered: 2002-06-12
Posts: 3,839
Website

Re: Looking for a bit of help.

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/time.h>

int main ()
{
    char    buf[4096];
    char    *p;
    int     i, j;
    struct timeval  start, stop, res;

    gettimeofday (&start, NULL);
    for (j = 0; j < 100000; j++)
        for (i = 0; i < 4096; i++)
            buf[i] = i%255;
    gettimeofday (&stop, NULL);

    timersub (&stop, &start, &res);
    printf ("Run time - index: %ld.%06ld\n", res.tv_sec, res.tv_usec);

    gettimeofday (&start, NULL);
    for (j = 0; j < 100000; j++)
        for (p = buf; p < buf + 4096; p++)
            *p = i%255;
    gettimeofday (&stop, NULL);

    timersub (&stop, &start, &res);
    printf ("Run time - pointer: %ld.%06ld\n", res.tv_sec, res.tv_usec);

    exit (0);
}

Offline

#6 2009-05-04 01:48 PM

i3839
Oddministrator
From: Amsterdam
Registered: 2003-06-07
Posts: 2,239

Re: Looking for a bit of help.

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/time.h>
#include <string.h>

int main ()
{
    char    buf[4096];
    char    *p;
    int     i, j;
    struct timeval  start, stop, res;

    memset(buf, 0, sizeof(buf));
    gettimeofday (&start, NULL);
    for (j = 0; j < 100000; j++)
        for (i = 0; i < 4096; i++)
            buf[i] += j % start.tv_usec;
    gettimeofday (&stop, NULL);
    timersub (&stop, &start, &res);
    printf ("Run time - index: %ld.%06ld\n", res.tv_sec, res.tv_usec);

    memset(buf, 0, sizeof(buf));
    gettimeofday (&start, NULL);
    for (j = 0; j < 100000; j++)
        for (p = buf; p < buf + 4096; p++)
            *p += j % start.tv_usec;
    gettimeofday (&stop, NULL);
    timersub (&stop, &start, &res);
    printf ("Run time - pointer: %ld.%06ld\n", res.tv_sec, res.tv_usec);

    exit (0);
}

Offline

#7 2009-05-04 07:46 PM

RobSeace
Administrator
From: Boston, MA
Registered: 2002-06-12
Posts: 3,839
Website

Re: Looking for a bit of help.

Offline

#8 2009-05-04 07:59 PM

RobSeace
Administrator
From: Boston, MA
Registered: 2002-06-12
Posts: 3,839
Website

Re: Looking for a bit of help.

I changed the assignment of each element to rand() to remove all possibility of magic
optimization of the assignment by the compiler...  I get very slightly better times for the
pointer version with no optimization and -O1, and slightly worse in -O2 and -O3...  So,
it's still doing something smart with array indexing in -O2 and higher, which apparently
is smarter than simple pointer arithmetic...  (I'm really curious WTF that is, though...)

Offline

#9 2009-05-04 09:07 PM

i3839
Oddministrator
From: Amsterdam
Registered: 2003-06-07
Posts: 2,239

Re: Looking for a bit of help.

Compilers are advanced enough that the one to one mapping between C code
and assembly is gone, so it's all about rubbing the compiler in the right spot.
Any small variation in the surrounding code has more effect than whether
pointer arithmetic or array indexing is used.

A diff between the two versions didn't show any interesting differences in
the assembly output.

Offline

  • Index
  • » C
  • » Looking for a bit of help.

Board footer

Powered by FluxBB