Path: utzoo!utgpu!news-server.csri.toronto.edu!mailrus!wuarchive!usc!zaphod.mps.ohio-state.edu!rpi!sci.ccny.cuny.edu!phri!cmcl2!stealth.acf.nyu.edu!brnstnd From: brnstnd@stealth.acf.nyu.edu Newsgroups: comp.lang.misc Subject: Re: Pointers as 3-tuples Message-ID: <3778:Apr1500:01:2290@stealth.acf.nyu.edu> Date: 15 Apr 90 00:01:22 GMT References: <20145@megaron.cs.arizona.edu> <14340@lambda.UUCP> Reply-To: brnstnd@stealth.acf.nyu.edu (Dan Bernstein) Distribution: usa Organization: IR Lines: 63 X-Yo-Jim: make sure you read the second half of this In article <14340@lambda.UUCP> jlg@lambda.UUCP (Jim Giles) writes: > Using C, with you 3-tuple > idea, write the following routines: brk(), sbrk(), malloc(), and > free(). malloc() and free() are provided as library routines, so under a 3-tuple implementation of pointers they're defined to use the correct bounds. (Of course, 3-tuples are invisible to compliant programs.) They don't need to be implemented in portable C, as long as they act correctly. But I understand what you're aiming at, and I'll answer on that level. At least one of brk() and sbrk() must be provided by the OS for malloc() to get any memory in the first place. Now let's assume malloc(n) has set up its data structures, found a character array, and made a char pointer x that points to the n requested characters. The bounds of x are the bounds of the memory array malloc() took from sbrk(); the problem is to change its bounds into x lower, x + n upper. C doesn't provide any explicit casts to do the job; is that your point? In a more powerful C-like language, malloc() would finish off with return (*(char [n] at x)) x; ``char [n]'' is the type for character arrays of size n; ``at x'' is the array type qualifier for arrays starting at x; and so malloc() returns a pointer to a character array of size n---i.e., the 3-tuple (x,x,x+n). > > for (p = A; p < A + A_SIZE; p++) foo(*p); > > is more elegant than > > for (i = 0; i < A_SIZE; i++) foo(A[i]); > And I guess it's just a matter of taste that I consider: > for (i = 1; i < N-1; i++ ) > for (j = 1; j < M-1; j++ ) > A[i][j] = 0; > is better than: > for (p = A; ???; ???) [ proceeds to complain that he doesn't understand what to do ] Come on, Jim. foo A[N][M]; foo (*p)[M]; foo *q; for (p = A; p < A + N; p++) for (q = &(*p)[0]; q < &(*p)[0] + M; q++) *q = 0; I'm assuming array bounds of (0,0) through (N - 1,M - 1); you seem to have taken (1,1) through (N - 2,M - 2). Whatever. > What ever you come up with will either _not_ be as clear as the first > above, or it will use some macro (whose definition won't be clear). Care to repeat that, now that you've seen how simple the solution is? Ada types probably wouldn't mind using macro FIRST(p) for (&((*p)[0])); that definition is perfectly clear, and for (p = A; p < A + N; p++) for (q = FIRST(p); q < FIRST(p) + M; q++) *q = 0; is very readable. It also provides an advantage that you still haven't admitted: namely, that p and q can be bound-checked the moment they're assigned values. ---Dan