Unconstrained behavior in arrays.

C

Chad

Can someone tell my why:

char i;
int a;

Causes unconstrained behavior.

Thanks in advance.


Chad
 
A

A. Sinan Unur

Can someone tell my why:

char i;
int a;

Causes unconstrained behavior.


I don't know what you have in mind when you say 'unconstrained behavior'.
I have not heard that expression before.

D:\Home> cat t.c
char i;
int a;

D:\Home> gcc -Wall -std=c99 -pedantic -c t.c
t.c:2: error: variable-size type declared outside of any function

Sinan
 
K

Keith Thompson

Chad said:
Can someone tell my why:

char i;
int a;

Causes unconstrained behavior.


Do you mean "undefined behavior"?

The code fragment you posted doesn't cause any behavior at all, since
it's not a legal C translation unit. Show us a complete translation
unit, or preferably a complete program, and we can discuss it.

Or just ask yourself this question: what is the value of i?
 
C

Chad

I misworded it. The question stems from the following thread:

http://groups.google.com/group/comp...9b0bb/717dea00f4d301c0?hl=en#717dea00f4d301c0

One of the responses was:

See below.

#include <stdio.h>
void Func1()

Change this to:

void Func1( void )

to match the pointer declarations below.


{
printf("func1\n");
return;
}
void Func2()

Same here.
{
printf("func2\n");
return;
}
void Func3()

Same here.
{
printf("func3\n");
return;
}
void StartFunctions( void (*f)(void) )
{
printf("Starting Function...\n");
f();
return;
}
int main()

This isn't one of the two legitimate declarations of main, use:

int main( void )

{
char i;

Using a character value for array subscripting is at least suspicious;
no, wait, it's actually a constraint violation. Use:

int i;

void *v[] = { (void*)Func1, (void*)Func2, (void*)Func3 };


In order to avoid pointer type mismatches you presumably want:

void (*v[])(void) = { Func1, Func2, Func3 };

Note: cluttering your code with spurious casts is the wrong approach.
Better use correct declarations in the first place.
for ( i = 0; i < 3; i++ ) StartFunctions( v );


Minor quibble: beware of magic numbers (3).
return 0;

That's fine. :eek:)

The question I meant to ask was why does 'char i' cause a constraint
violation, but 'int i' wouldnt?

Chad
 
B

Ben Pfaff

Chad said:
Can someone tell my why:

char i;
int a;

Causes unconstrained behavior.


In C90, this is a constraint violation, because the expression
that indicates the size of the array is not an integral constant
expression.

In C99, at file scope this is a constraint violation for the same
reason. At block scope it yields undefined behavior because `i'
is uninitialized.
 
K

Keith Thompson

Chad said:
I misworded it. The question stems from the following thread:

http://groups.google.com/group/comp...9b0bb/717dea00f4d301c0?hl=en#717dea00f4d301c0

One of the responses was: [snip]
{
char i;

Using a character value for array subscripting is at least suspicious;
no, wait, it's actually a constraint violation. Use:

int i; [...]
The question I meant to ask was why does 'char i' cause a constraint
violation, but 'int i' wouldnt?

Ok, that's completely different from what you posted. In your
original post, you were using i as the dimension in an array
declaration; here, you're just using it as an array index.

Using a char value as an array index does not violate a constraint.
The constraints on an array subscripting expression are (with
underscores denoting italics):

One of the expressions shall have type "pointer to object _type_",
the other expression shall have integer type, and the result has
type "_type_".

Since char is an integer type, it's perfectly legal to use a char as
an array index.

It is suspicious, though. It's implementation-defined whether char is
signed or unsigned, and the value is promoted to int (or conceivably
to unsigned int) anyway. The space saving for a single declared
object is insignificant. There's almost never a good reason to use a
char as an array index.
 
J

Joe Wright

Keith said:
Chad said:
I misworded it. The question stems from the following thread:

http://groups.google.com/group/comp...9b0bb/717dea00f4d301c0?hl=en#717dea00f4d301c0

One of the responses was:
[snip]
{
char i;

Using a character value for array subscripting is at least suspicious;
no, wait, it's actually a constraint violation. Use:

int i;
[...]

The question I meant to ask was why does 'char i' cause a constraint
violation, but 'int i' wouldnt?


Ok, that's completely different from what you posted. In your
original post, you were using i as the dimension in an array
declaration; here, you're just using it as an array index.

Using a char value as an array index does not violate a constraint.
The constraints on an array subscripting expression are (with
underscores denoting italics):

One of the expressions shall have type "pointer to object _type_",
the other expression shall have integer type, and the result has
type "_type_".

Since char is an integer type, it's perfectly legal to use a char as
an array index.

It is suspicious, though. It's implementation-defined whether char is
signed or unsigned, and the value is promoted to int (or conceivably
to unsigned int) anyway. The space saving for a single declared
object is insignificant. There's almost never a good reason to use a
char as an array index.

It's hard to use char at all. Virtually any expression referring to char
is converted immediately to int to fit a register or accumulator. There
are no char-width places to put it.
 

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,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top