Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > address beyond array declaration

Reply
Thread Tools

address beyond array declaration

 
 
Douglas
Guest
Posts: n/a
 
      06-17-2004
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
 
Reply With Quote
 
 
 
 
Mike Wahler
Guest
Posts: n/a
 
      06-17-2004
"Douglas" <> wrote in message
news: 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


 
Reply With Quote
 
 
 
 
Eric Sosman
Guest
Posts: n/a
 
      06-17-2004
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?

--


 
Reply With Quote
 
Andrey Tarasevich
Guest
Posts: n/a
 
      06-18-2004
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

 
Reply With Quote
 
JV
Guest
Posts: n/a
 
      06-18-2004

"Mike Wahler" <> wrote in message
news:_eiAc.15239$ ink.net...
> "Douglas" <> wrote in message
> news: 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



 
Reply With Quote
 
Richard Bos
Guest
Posts: n/a
 
      06-18-2004
"JV" <> 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
 
Reply With Quote
 
Irrwahn Grausewitz
Guest
Posts: n/a
 
      06-18-2004
"JV" <> 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 ()
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Address of array && address of pointer to array Stanley Rice C Programming 33 09-20-2011 12:47 AM
maxplusII error: a deferred constant declaration without a full declaration is not supported Noah VHDL 5 04-07-2006 02:34 PM
"virtual outside class declaration" and "declaration does not declare anything" kelvSYC C++ 6 05-17-2005 08:58 AM
Function declaration in class declaration Ovidesvideo C++ 4 12-10-2004 06:36 PM
Intel C++ 8.0 : declaration hides declaration Alex Vinokur C++ 4 04-05-2004 09:49 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57