undefined behavior

C

Clark S. Cox III

deepak said:
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.
 
H

Hallvard B Furuseth

deepak said:
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.
 
R

Richard Tobin

Using 'char' as an array index is an undefined behavior?
[/QUOTE]
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
 
R

Richard Heathfield

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.
 
S

Serve Laurijssen

Richard Heathfield said:
deepak said:


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]?
 
C

Clark S. Cox III

Serve said:
Richard Heathfield said:
deepak said:

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.
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top