Forums
New posts
Search forums
Members
Current visitors
Log in
Register
What's new
Search
Search
Search titles only
By:
New posts
Search forums
Menu
Log in
Register
Install the app
Install
Forums
Archive
Archive
Perl
Perl Misc
to RG - Lisp lunacy and Perl psychosis
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
[QUOTE="Pascal J. Bourguignon, post: 4872114"] Well, since C is weakly-to-not typed, you cannot enforce it at the variable site, however, it is specified indeed that it is invalid or undefined to derefer a pointer that doesn't point to allocated memory, and to compare pointers that don't point to elements of the same array or one beyond. This can be enforced by heavy pointers and run-time checks. Of course, since they prefer to have their results fast than correct, this is rarely enforced. One way is to define pointers as: typedef struct { address arrayBase; int elementSize; int elementCount; int index; } Pointer; Pointer NULL={0,0,0,0}; void Pointer_incr(Pointer* p){ if(p.index<p.elementCount){ p.index++; }else{ error("Trying to increment a pointer out of bounds"); }} void Pointer_decr(Pointer* p){ if(0<p.index){ p.index--; }else{ error("Trying to decrement a pointer out of bounds"); }} int Pointer_minus(Pointer p,Pointer q){ if(p.arrayBase!=q.arrayBase){ error("Incompatible pointers"); }else{ return(p.index-q.index); }} bool Pointer_equalp(Pointer p,Pointer q){ return(Pointer_minus(p,q)==0); } bool Pointer_lessp (Pointer p,Pointer q){ return(Pointer_minus(p,q)<0); } T Pointer_deref(Type T,Pointer p){ if(p.index<p.elementCount){ return(deref(T,p.arrayBase+p.index)); }else{ error("Trying to dereference out of bound pointer."); }} char a; char b; int c[10]; int d[10]; char* p1=&a; /* <=> p1.arraybase=address_of(a); p1.elementSize=1; p1.elementCount=1; p1.index=0; */ char* p2=&b; /* <=> p2.arraybase=address_of(b); p2.elementSize=1; p2.elementCount=1; p2.index=0; */ int* p3=&c; /* <=> p3.arraybase=address_of(c); p3.elementSize=sizeof(int); p3.elementCount=10; p3.index=0; */ int* p4=&(d[9]); /* <=> p4.arraybase=address_of(d); p4.elementSize=sizeof(int); p4.elementCount=10; p4.index=9; */ int* p5=&d; /* <=> p5.arraybase=address_of(d); p5.elementSize=sizeof(int); p5.elementCount=10; p5.index=9; */ char* n=NULL; p2++; /* <=> Pointer_incr(&p2); */ p3++; /* <=> Pointer_incr(&p3); */ p1==p2; /* <=> Pointer_equalp(p1,p2); <=> error */ p3<p4; /* <=> Pointer_lessp(p3,p4); <=> error */ p4<p5; /* <=> Pointer_lessp(p5,p4); returns false. */ *p4=*p5; /* <=> copies the int from d[0] to d[9]. */ *n=0; /* <=> error (dereferencing NULL) */ [/QUOTE]
Verification
Post reply
Forums
Archive
Archive
Perl
Perl Misc
to RG - Lisp lunacy and Perl psychosis
Top