Python is just as good as C++ for real apps

Bengt Richter bokr at oz.net
Sat Jan 26 04:56:42 EST 2002


On Sat, 26 Jan 2002 03:36:14 GMT, grante at visi.com (Grant Edwards) wrote:

>In article <d0845u844213smjc0f70l2kl0ceso34c57 at 4ax.com>, Courageous wrote:
>> 
>>> (*p) + 1  adds one to it.
>> 
>> No it doesn't! (*p) acquires the integer value of whatever is
>> sitting at the address referred to by p and creates a
>> temporary; this temporary is an int. You're adding 1 to the
>> temporary. You're certainly not adding anything at all to
>> either p, or the region in memory region in memory which it
>> refers to.
>
>So?
>
>int i;
>The expression "i + 1" doesn't change the value of i either.
>
>I'll try one more time:
>
>int *p,i;  // both i and *p are ints
>
>i = 7;     // i has the value 7;
>*p = 7;    // *p has the value 7;
Oops, you're thinking Pythonic binding? You are actually using
an uninitialized pointer and storing an int value of 7 wherever it points.
You might get a GPF and not get to the rest ;-)
>
>// *p acts like an "int" variable in those statements, right?
>
>(*p) + 1;  // expression with value 8;

>i + 1;     // expression with value 8;
>
>// You have to add parens, but (*p) seems to act like an int in that
>// expression also.
You don't have to add parens unless you are using ++ or -- and want the
latter to apply to the pointer's target rather than the pointer itself,
or if you want pointer addition to happen before dereferencing, like *(p+1).
Here are some artifical illustrations:
--
#include <cstdio>
void main(){
    int i,j[3],*p;
    i=7;  j[1]=70;
    p = &j[0];      // or p = j; would also work
    *p++ = i + 1;   // j[0] <- i+1 (i.e., 8),
                    // then p is incremented to point to j[1]
                    // where we purposely preset a 70
    *(p+1) = *p+10 ;// j[2] <- j[1]+10, which 70+10, i.e.,80
    p[1] += (*p)++; // j[2] <- j[2]+j[1] (80+70)
                    //then j[1] is incremented to 71 (p still at j[1])
    printf("i=%d, j[0]=%d, j[1]=%d, j[2]=%d\n", i,j[0],j[1],j[2]);
}
--
Output:
i=7, j[0]=8, j[1]=71, j[2]=150

Can't show machine code for this, since with Ox optimization it all
disappears and becomes a printf call with five constants ;-)

>//
>// Neither one changes the value of a region of memory, both
>// return one more that the last value saved.
>
>(*p) += 1;  // increments an integer value by one
>  i  += 1;  // increments an integer value by one
>
>// *p is now equal to 8
>//  i is now equal to 8
>
>// (*p) and i both seem to behave the same there also.
>
>I still claim that with the declartion:
>
>int i;
>int *p;
>
>That both i and (*p) are ints.
>
And so is *p without parens. But: "int *p;" declares a pointer to
an int value, and reserves space for a pointer. It doesn't declare
an integer slot to point at. You have to make sure to initialize
the pointer so it points to a valid integer value (which needs its
own declaration) as in "p = &j[0];" above.


I'll spare you C++ references in the above examples ;-)

Regards,
Bengt Richter




More information about the Python-list mailing list