address beyond array declaration

D

Douglas

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
 
M

Mike Wahler

Douglas said:
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
 
E

Eric Sosman

Douglas said:
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?
 
A

Andrey Tarasevich

Douglas said:
...
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.
 
J

JV

Mike Wahler said:
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
 
R

Richard Bos

JV said:
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
 
I

Irrwahn Grausewitz

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:).
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top