Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > undefined behavior

Reply
Thread Tools

undefined behavior

 
 
deepak
Guest
Posts: n/a
 
      01-12-2007
Using 'char' as an array index is an undefined behavior?

 
Reply With Quote
 
 
 
 
Zara
Guest
Posts: n/a
 
      01-12-2007
On 12 Jan 2007 06:23:47 -0800, "deepak" <(E-Mail Removed)> wrote:

>Using 'char' as an array index is an undefined behavior?


No. the char will be promoted to int.
 
Reply With Quote
 
 
 
 
Clark S. Cox III
Guest
Posts: n/a
 
      01-12-2007
deepak wrote:

> Using 'char' as an array index is an undefined behavior?


No, though it might not be the best idea, as that limits you to being
able to (portably) index only 128 elements of the array.

--
Clark S. Cox III
http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
Hallvard B Furuseth
Guest
Posts: n/a
 
      01-12-2007
deepak writes:
> Using 'char' as an array index is an undefined behavior?


No, but if 'char' is 8-bit and signed, and contains an 8-bit character,
then it is negative. So

static char foo[256];
void bar(char ch) { ... foo[ch] ... }
can use a negative index to foo[], which yields undefined behavior.
Use foo[(unsigned char)ch] instead.

--
Hallvard
 
Reply With Quote
 
Richard Tobin
Guest
Posts: n/a
 
      01-12-2007
In article <(E-Mail Removed)>,
Hallvard B Furuseth <(E-Mail Removed)> wrote:

>> Using 'char' as an array index is an undefined behavior?


>No, but if 'char' is 8-bit and signed, and contains an 8-bit character,
>then it is negative.


Which is why some compilers (e.g. gcc) warn you about it.

-- Richard
--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
 
Reply With Quote
 
Richard Heathfield
Guest
Posts: n/a
 
      01-12-2007
deepak said:

> Using 'char' as an array index is an undefined behavior?


No, but indexing outside the bounds of an array does invoke undefined
behaviour. If the value stored in the char is negative (which it can be if
char is signed by default) or greater than or equal to the number of
elements in the array, the behaviour is undefined.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
 
Reply With Quote
 
Serve Laurijssen
Guest
Posts: n/a
 
      01-12-2007

"Richard Heathfield" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> deepak said:
>
>> Using 'char' as an array index is an undefined behavior?

>
> No, but indexing outside the bounds of an array does invoke undefined
> behaviour. If the value stored in the char is negative (which it can be if
> char is signed by default) or greater than or equal to the number of
> elements in the array, the behaviour is undefined.


int array[10] = { 0,1,2,3,4,5,6,7,8,9};

int *p = &array[4];

signed char index = -1; // signed for clarity

printf("%d\n", p[index]);



are you guys saying that any negative index is undefined behaviour or just
that if you dereference before &array[0]?


 
Reply With Quote
 
Clark S. Cox III
Guest
Posts: n/a
 
      01-12-2007
Serve Laurijssen wrote:
> "Richard Heathfield" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>> deepak said:
>>
>>> Using 'char' as an array index is an undefined behavior?

>> No, but indexing outside the bounds of an array does invoke undefined
>> behaviour. If the value stored in the char is negative (which it can be if
>> char is signed by default) or greater than or equal to the number of
>> elements in the array, the behaviour is undefined.

>
> int array[10] = { 0,1,2,3,4,5,6,7,8,9};
>
> int *p = &array[4];
>
> signed char index = -1; // signed for clarity
>
> printf("%d\n", p[index]);
>
>
>
> are you guys saying that any negative index is undefined behaviour or just
> that if you dereference before &array[0]?


Indexing an *array* with a negative index is undefined behavior, because
negative indices are, by definition, out of bounds. As you have
demonstrated, indexing a *pointer* with a negative index might not be;
because that pointer might actually point into a larger array.


--
Clark S. Cox III
(E-Mail Removed)
 
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
typeof x == 'undefined' or x == undefined? -Lost Javascript 13 01-31-2007 12:04 AM
undefined vs. undefined (was: new Array() vs []) VK Javascript 45 09-12-2006 05:26 PM
please explain why this is Undefined Behavior REH C++ 25 03-29-2005 10:19 PM
Is it an undefined behavior in C++ Standard? aka C++ 10 03-15-2005 09:21 AM
undefined behavior or not undefined behavior? That is the question Mantorok Redgormor C Programming 70 02-17-2004 02:46 PM



Advertisments