Velocity Reviews > Pointer difficulties

# Pointer difficulties

Richard
Guest
Posts: n/a

 02-07-2009
CBFalconer <(E-Mail Removed)> writes:

> spasmous wrote:
>>
>> Below are two versions of code that I expected to do the same
>> thing. However they don't. I'm having difficulty seeing what is
>> causing the different behaviour - can someone please explain it
>> to me! Thank you!!
>>
>> int n = 10,m=100;
>> double y[n];
>> short *p[n]; // pointers to arrays of length m
>>
>> // this gives the desired result
>> for(int j=0; j<m; j++) {
>> for(int k=0; k<n; k++) y[k] = *(p[k]+j);
>> do_stuff(y); ^-- ** This is 0 thru 99

So what?

>> *(p[0]+j) = (short)y[0];
>> *(p[1]+j) = (short)y[1];
>> }
>>
>> // this does not give the desired result
>> for(int j=0; j<m; j++) {
>> for(int k=0; k<n; k++) y[k] = *(p[k]++);
>> do_stuff(y); ^-- ** This incr is 1
>> *(p[0]) = (short)y[0];
>> *(p[1]) = (short)y[1];
>> }

>
> See the '**' annotations above.

Note to the OP : the code above is hard to read and hard to
debug. Never, ever put the body of something like a for loop on the same
line. It's hard to use a debugger on it for a start.

for(int k=0; k<n; k++){
y[k] = *(p[k]+j);
}

Is much more readable, easier to extend later and works in a debugger
properly usually.

If we look at the second loop:

for(int k=0; k<n; k++){
y[k] = *(p[k]++);
}

... one has to ask what you expected. p[k] returns a value which then
dereferenced. After that the value is post incremented but ignored
(likely optimised out anyway).

Since you dont express what the desired behaviour is once must wonder if
you are sure the first does indeed produce it.

Kaz Kylheku
Guest
Posts: n/a

 02-07-2009
On 2009-02-06, spasmous <(E-Mail Removed)> wrote:
> Below are two versions of code that I expected to do the same thing.
> However they don't. I'm having difficulty seeing what is causing the
> different behaviour - can someone please explain it to me! Thank you!!
>
> int n = 10,m=100;
> double y[n];
> short *p[n]; // pointers to arrays of length m
>
> // this gives the desired result > for(int j=0; j<m; j++) {
>
> for(int k=0; k<n; k++) y[k] = *(p[k]+j);
> do_stuff(y);
> *(p[0]+j) = (short)y[0];
> *(p[1]+j) = (short)y[1];
>
> }
>
> // this does not give the desired result
> for(int j=0; j<m; j++) {
>
> for(int k=0; k<n; k++) y[k] = *(p[k]++);
> do_stuff(y);
> *(p[0]) = (short)y[0];
> *(p[1]) = (short)y[1];
>
> }

In addition to the other comments, I recommend array notation over
*(x + y). Watch:

for(int j=0; j<m; j++) {
for(int k=0; k<n; k++)
y[k] = p[k][j];
do_stuff(y);
p[0][j] = y[0];
p[1][j] = y[1];
}

for(int j=0; j<m; j++) {
for(int k=0; k<n; k++)
y[k] = p[k]++[j];
do_stuff(y);
p[0][-1] = y[0];
p[1][-1] = y[1];
}

Look, Ma, no *(p[0]+j) and *(p[k]++) obfuscation.

Kaz Kylheku
Guest
Posts: n/a

 02-07-2009
On 2009-02-06, Han from China <(E-Mail Removed)> wrote:
> for(k=0; k<n; k++) y[k] = *(p[k]+j), showit();
> //do_stuff(y);
> *(p[0]+j) = (short)y[0], showit();
> *(p[1]+j) = (short)y[1], showit();

Oh yeah, /don't/ show the poor noob the secret of *(p[k]+j) being p[k][j]!

Ike Naar
Guest
Posts: n/a

 02-07-2009
In article <gmj8jj\$2da\$(E-Mail Removed)>,
Richard <(E-Mail Removed)> wrote:
>Note to the OP : the code above is hard to read and hard to debug.

Looks like code from the "real world"

>Never, ever put the body of something like a for loop on the same line.

That is not what makes the code difficult to understand.

>It's hard to use a debugger on it for a start.

Solving (see elsethread) this particular problem through code reading
turned out to be easier than trying to solve it with a debugger.

>for(int k=0; k<n; k++){
> y[k] = *(p[k]++);
>}
>
>.. one has to ask what you expected. p[k] returns a value which then
>dereferenced. After that the value is post incremented but ignored
>(likely optimised out anyway).

The incremented value is used in the next iteration of the main loop.
For k=0,1 the value is used later in the same iteration as well.