Re: OK folks, corrected
- From: Harald van Dijk <truedfx@xxxxxxxxx>
- Date: Sun, 09 Mar 2008 12:04:42 +0100
On Sun, 09 Mar 2008 11:25:42 +0100, jacob navia wrote:
Harald van Dijk wrote:
On Sun, 09 Mar 2008 09:34:34 +0100, jacob navia wrote:
Harald van Dijk wrote:
On Sun, 09 Mar 2008 01:14:38 +0100, jacob navia wrote:No, since this tells the compiler that this pointer can be 1 or more
In standard C you can say that a pointer is non null withTrue, but this also disallows other valid non-null pointer values
int function(int tab[static 1]);
tab must point to at least one element, hence it can't be NULL.
from being passed to that function, such as a pointer to the end of
an array.
elements, nothing more.
So when it's really 0 elements, you have a problem.
Yes of course. Every day I pass arrays of zero elements (that can't be
null) to functions. I have only problems.
Consider a simple function to zero out a block of memory. You might give
it a size parameter, but another equally legitimate declaration is
void clearmem(void *start, void *end);
where neither start nor end is allowed to be a null pointer, _but_ end
will usually not point to anything.
Let's try your suggestion.
void clearmem(void start[static 1], void end[static 1]);
This is not valid, since you can't have arrays of incomplete type. (By
itself, this is another argument for a separate indicator that an argument
must not be null.) Okay, let's try giving it unsigned char *, because we
happen to know that in this program, it will only be used with byte arrays
anyway, and it's possible to cast if required.
void clearmem(unsigned char start[static 1], unsigned char end[static 1]);
As you can test any day too
int tab[0];
is illegal C.
Obviously.
And if you pass a pointer to 1 past the end of the array, you can't
access it until you decrement it. Since the compiler will NOT verify the
address anyway, the syntax I proposed will work.
The behaviour will be undefined, and the compiler will be permitted to
make the function access *end. This will be allowed even if the function
does not actually access *end.
Suppose the compiler can tell that for your processor, it would be more
efficient to save *end, then clear from *start to *end (inclusive), and
finally restore *end. You told the compiler that *end exists, so this is a
perfectly legitimate optimisation. Now suppose that the access to *end
aborts your program.
The result is a visible bug in the program. This bug will need to be
fixed. This bug can be fixed by dropping the misuse of '[static 1]' as
meaning 'nonnull'.
.
- Follow-Ups:
- Re: OK folks, corrected
- From: jacob navia
- Re: OK folks, corrected
- References:
- OK folks, corrected
- From: jacob navia
- Re: OK folks, corrected
- From: Ian Collins
- Re: OK folks, corrected
- From: Keith Thompson
- Re: OK folks, corrected
- From: Ian Collins
- Re: OK folks, corrected
- From: Richard Heathfield
- Re: OK folks, corrected
- From: santosh
- Re: OK folks, corrected
- From: Richard Heathfield
- Re: OK folks, corrected
- From: santosh
- Re: OK folks, corrected
- From: Richard Heathfield
- Re: OK folks, corrected
- From: santosh
- Re: OK folks, corrected
- From: Richard Heathfield
- Re: OK folks, corrected
- From: Keith Thompson
- Re: OK folks, corrected
- From: jacob navia
- Re: OK folks, corrected
- From: Harald van Dijk
- Re: OK folks, corrected
- From: jacob navia
- Re: OK folks, corrected
- From: Harald van Dijk
- Re: OK folks, corrected
- From: jacob navia
- OK folks, corrected
- Prev by Date: Re: I need help please!
- Next by Date: Re: Braces or not [Re: cat]
- Previous by thread: Re: OK folks, corrected
- Next by thread: Re: OK folks, corrected
- Index(es):
Relevant Pages
|