467,169 Members | 962 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,169 developers. It's quick & easy.

address beyond array declaration

Hi,

What is the need for the inaccessible pointer address beyond the end of an array?

Eg. could

for(ip = &array[0]; ip < &array[arraySize]; ip++)....

not be rewritten as

arraySize--;
for(ip = &array[0]; ip <= & array[arraySize]; ip++)....
Douglas
Nov 14 '05 #1
  • viewed: 1263
Share:
6 Replies
"Douglas" <mm***@yahoo.co.uk> wrote in message
news:4c**************************@posting.google.c om...
Hi,

What is the need for the inaccessible pointer address beyond the end of an array?

It's sometimes convenient when iterating through an array.
But be sure not to try to dereference it.

Eg. could

for(ip = &array[0]; ip < &array[arraySize]; ip++)....

not be rewritten as

arraySize--;
for(ip = &array[0]; ip <= & array[arraySize]; ip++)....

Yes, it could also be written e.g.:

for(ip = &array[0]; ip <= & array[arraySize - 1]; ip++)....

However the first form above is the most 'idomatic'
(i.e. most quickly recognized by most C coders).

Use whichever form most clearly expresses your intent.
I don't like your second form, because if possible,
I'd declare 'arraySize' as 'const' (typically it would
be a function parameter), in which case the expression
'arraySize--' would not be allowed.

Personally, if I'm using an array's size as my
'boundary condition' (as opposed to using an array
element value, such as a string terminator), I'll use
indices rather than pointers, e.g.:

size_t i = 0
for(i = 0; i < arraySize; ++i)
/* etc */

-Mike
Nov 14 '05 #2
Douglas wrote:
Hi,

What is the need for the inaccessible pointer address beyond the end of an array?

Eg. could

for(ip = &array[0]; ip < &array[arraySize]; ip++)....

not be rewritten as

arraySize--;
for(ip = &array[0]; ip <= & array[arraySize]; ip++)....


Yes, you could write this and it would work. But it
still uses "one past the end" pointer: What is the value
of `ip' when the test fails?

--
Er*********@sun.com

Nov 14 '05 #3
Douglas wrote:
...
What is the need for the inaccessible pointer address beyond the end of an array?

Eg. could

for(ip = &array[0]; ip < &array[arraySize]; ip++)....

not be rewritten as

arraySize--;
for(ip = &array[0]; ip <= & array[arraySize]; ip++)....
...


Firstly, not in case when 'arraySize' is initially zero.

Secondly, your version still produces a pointer that points beyond the
end of an array.

--
Best regards,
Andrey Tarasevich

Nov 14 '05 #4
JV

"Mike Wahler" <mk******@mkwahler.net> wrote in message
news:_e*****************@newsread2.news.atl.earthl ink.net...
"Douglas" <mm***@yahoo.co.uk> wrote in message
news:4c**************************@posting.google.c om...
Hi,

What is the need for the inaccessible pointer address beyond the end of
an array?

It's sometimes convenient when iterating through an array.
But be sure not to try to dereference it.

BTW why the later sizeof (a) causes segmentation fault in the code below? I
guessed that if would have resulted still 5*sizeof (int). Now I just found
out that there really is difference between arrays and pointers that
matters:). Althought in practice I use arrays if the length is fixed and
just declare a pointer if I the number of elements varies runtime.
-Jyrki
int a[5];
int main(void){
printf("%i\n",sizeof(int));

printf("%i\n",sizeof(a));
realloc(a,10*sizeof(int));
printf("%i\n",sizeof(a));
}

-jyrki

Nov 14 '05 #5
"JV" <n.*@n.com.invalid> wrote:
BTW why the later sizeof (a) causes segmentation fault in the code below?
It doesn't. That is, that may be the line that triggers the segfault,
but the actual cause is...
int a[5];
int main(void){
printf("%i\n",sizeof(int));

printf("%i\n",sizeof(a));
realloc(a,10*sizeof(int));
....here. You cannot use realloc() on memory you didn't get from *alloc()
in the first place. In this case, you cannot use realloc() on a, which
is a static file-scope array.
This call invokes undefined behaviour; after it is executed, _nothing_
can be relied on. In theory, your program is now allowed to send love
letters to your supervisor. In practice, you've probably corrupted your
allocation arena...
printf("%i\n",sizeof(a));


....leading to a segfault next time you refer to this object, or use
another allocation function. But this line _in itself_ is not the
problem.

Richard
Nov 14 '05 #6
"JV" <n.*@n.com.invalid> wrote:
<snip>
BTW why the later sizeof (a) causes segmentation fault in the code below? I
guessed that if would have resulted still 5*sizeof (int). Now I just found
out that there really is difference between arrays and pointers that
matters:). <snip>
int a[5];
int main(void){
printf("%i\n",sizeof(int));

printf("%i\n",sizeof(a));
realloc(a,10*sizeof(int));
printf("%i\n",sizeof(a));
}


There's one more /very/ important difference: any attempt to
reallocate an array results in undefined behaviour, of which
the segfault you observed is only one incarnation.

BTW: your code fails to #include two necessary standard headers,
and the invocation of realloc would still be horribly wrong, even
if the first argument were a pointer previously returned by one
of the *alloc functions. Plus, omitting the return statement
in main is only sanctioned by ISO C99.

Regards
--
Irrwahn Grausewitz (ir*******@freenet.de)
welcome to clc: http://www.ungerhu.com/jxh/clc.welcome.txt
clc faq-list : http://www.faqs.org/faqs/C-faq/faq/
clc OT guide : http://benpfaff.org/writings/clc/off-topic.html
Nov 14 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

27 posts views Thread by Adam Warner | last post: by
19 posts views Thread by DarelRex@gmail.com | last post: by
17 posts views Thread by =?Utf-8?B?U2hhcm9u?= | last post: by
16 posts views Thread by somenath | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.