variable changed, but i didn't change is.

J

Jasper Aikema

Hi,

I do have the next code in my program:

int main(void)
{
double a[6],x[5];
int i;

a[0] = 4;

printf("a[0] = %lf\n\n", a[0]); // a[0] = 4.000000

for (i=0;i<=6;i++)
{
x = 6;
}

printf("a[0] = %lf\n\n", a[0]); // a[0] = 6.000000 return 0;

}

If I run it, first is says a[0] = 4 and later a[0] = 6. But I didn't
change a[0]. How is that possible, do I do something wrong or is there a
ghost in my computer switching bits ;).
I compiled it on an other computer, and got the same problem.
 
J

Joona I Palaste

Jasper Aikema said:
I do have the next code in my program:
int main(void)
{
double a[6],x[5];
int i;

a[0] = 4;

printf("a[0] = %lf\n\n", a[0]); // a[0] = 4.000000

for (i=0;i<=6;i++)

Take a close look at this line. Particularly the ending condition.
{
x = 6;
}

printf("a[0] = %lf\n\n", a[0]); // a[0] = 6.000000 return 0;

If I run it, first is says a[0] = 4 and later a[0] = 6. But I didn't
change a[0]. How is that possible, do I do something wrong or is there a
ghost in my computer switching bits ;).
I compiled it on an other computer, and got the same problem.


You are creating undefined behaviour by assigning 6 to x[5] and x[6].
On your computer, these seem to happen to coincide with a[3] and a[4],
or a[4] and a[5]. Or there might be a more complex explanation. In
theory, by accessing x[5] or x[6], you are giving the program
permission to paint your living room purple.
No offense intended, but did you really write this code yourself, or
is it a "find the error" homework exercise?

--
/-- Joona Palaste ([email protected]) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"'So called' means: 'There is a long explanation for this, but I have no
time to explain it here.'"
- JIPsoft
 
J

Jens.Toerring

Jasper Aikema said:
I do have the next code in my program:
int main(void)
{
double a[6],x[5];
int i;

a[0] = 4;

printf("a[0] = %lf\n\n", a[0]); // a[0] = 4.000000

No need for "%lf" in printf - "%f" will also do (you only need it
for scanf()).
for (i=0;i<=6;i++)
{
x = 6;
}


Here you're writing past the end of the array 'x' (it has 5 elements,
so the largest index you can use is 4, not 6), thereby invoking un-
defined behaviour and now everything can happen - including waking
up the ghost that sleeps happily in your computer;-)
printf("a[0] = %lf\n\n", a[0]); // a[0] = 6.000000
return 0;
}
If I run it, first is says a[0] = 4 and later a[0] = 6. But I didn't
change a[0]. How is that possible, do I do something wrong or is there a
ghost in my computer switching bits ;).

Form the look of it it seems as if array 'a' is stored in memory
after array 'x', so when you write past the end of 'x' you start
to overwrite parts of 'a'. But don't rely on that - it can be quite
different on a different machine or with a different compiler.

Regards, Jens
 
A

Arthur J. O'Dwyer

Jasper Aikema <[email protected]> scribbled the following: [...]
double a[6],x[5]; [...]
for (i=0;i<=6;i++)

Take a close look at this line. Particularly the ending condition.
{
x = 6;
}

You are creating undefined behaviour by assigning 6 to x[5] and x[6].
On your computer, these seem to happen to coincide with a[3] and a[4],
or a[4] and a[5].

Much more likely they coincide with a[0] and a[1], the first two
elements of 'a'. I don't see how x[5] could "coincide with" a[3]
except on the DS9000!
Or there might be a more complex explanation. In
theory, by accessing x[5] or x[6], you are giving the program
permission to paint your living room purple.

Indeed.

HTH,
-Arthur
 
B

buda

Jasper Aikema said:
Hi,

I do have the next code in my program:

int main(void)
{
double a[6],x[5];
int i;

a[0] = 4;

printf("a[0] = %lf\n\n", a[0]); // a[0] = 4.000000

for (i=0;i<=6;i++)
{
x = 6;
}
printf("a[0] = %lf\n\n", a[0]); // a[0] = 6.000000
return 0;
}


The for loop is your problem. Array x has elements from x[0] to x[4], and
you assign a 6 to elements x[5] and x[6] thus invoking undefined behavior.
The result you get is probably because your machine (like a lot of machines
I've seen) stores all the arrays in your program in continuous memory
locations. So, when you write x[5]=6, what you actually do is assign 6 to
a[0]. (same for x[6] and a[1])
 
J

Jasper Aikema

Op Thu, 16 Sep 2004 16:23:22 +0000, schreef Jens.Toerring:
Jasper Aikema said:
I do have the next code in my program:
int main(void)
{
double a[6],x[5];
int i;

a[0] = 4;

printf("a[0] = %lf\n\n", a[0]); // a[0] = 4.000000

No need for "%lf" in printf - "%f" will also do (you only need it
for scanf()).
for (i=0;i<=6;i++)
{
x = 6;
}


Here you're writing past the end of the array 'x' (it has 5 elements,
so the largest index you can use is 4, not 6), thereby invoking un-
defined behaviour and now everything can happen - including waking
up the ghost that sleeps happily in your computer;-)
printf("a[0] = %lf\n\n", a[0]); // a[0] = 6.000000
return 0;
}
If I run it, first is says a[0] = 4 and later a[0] = 6. But I didn't
change a[0]. How is that possible, do I do something wrong or is there a
ghost in my computer switching bits ;).

Form the look of it it seems as if array 'a' is stored in memory
after array 'x', so when you write past the end of 'x' you start
to overwrite parts of 'a'. But don't rely on that - it can be quite
different on a different machine or with a different compiler.

Regards, Jens


Thanx, you are right, i changed it in

for (i=0;i<6;i++)

and it did work.

Greetings, Jasper
 
J

Joona I Palaste

Jasper Aikema said:
Op Thu, 16 Sep 2004 16:23:22 +0000, schreef Jens.Toerring:
Jasper Aikema said:
I do have the next code in my program:
int main(void)
{
double a[6],x[5];
int i;

a[0] = 4;

printf("a[0] = %lf\n\n", a[0]); // a[0] = 4.000000

No need for "%lf" in printf - "%f" will also do (you only need it
for scanf()).
for (i=0;i<=6;i++)
{
x = 6;
}


Here you're writing past the end of the array 'x' (it has 5 elements,
so the largest index you can use is 4, not 6), thereby invoking un-
defined behaviour and now everything can happen - including waking
up the ghost that sleeps happily in your computer;-)
printf("a[0] = %lf\n\n", a[0]); // a[0] = 6.000000
return 0;
}
If I run it, first is says a[0] = 4 and later a[0] = 6. But I didn't
change a[0]. How is that possible, do I do something wrong or is there a
ghost in my computer switching bits ;).

Form the look of it it seems as if array 'a' is stored in memory
after array 'x', so when you write past the end of 'x' you start
to overwrite parts of 'a'. But don't rely on that - it can be quite
different on a different machine or with a different compiler.

Thanx, you are right, i changed it in
for (i=0;i<6;i++)
and it did work.

For loose values of "work". You're still creating undefined behaviour by
accessing x[5]. Have you read a C textbook?
 
J

Jasper Aikema

Op Thu, 16 Sep 2004 17:01:50 +0000, schreef Jens.Toerring:
Jasper Aikema said:
Op Thu, 16 Sep 2004 16:23:22 +0000, schreef Jens.Toerring:
I do have the next code in my program:
Thanx, you are right, i changed it in
double a[6],x[5];
for (i=0;i<6;i++)
and it did work.

Just by bad luck;-) Since 'x' has only 5 elements you still have a
bug, it must be

for ( i = 0; i < 5; i++ )

or you're still writing past the end of the array.

Regards, Jens

I also changed double a[6],x[5]; in double a[6],x[6];
 
R

Randy Howard

I also changed double a[6],x[5]; in double a[6],x[6];

I think you're still not quite "getting it". :) You
are aware of 0-based arrays, since your loops start there,
so why is this so problematic?
 
M

Martin Ambuhl

Jasper said:
Hi,

I do have the next code in my program:

int main(void)
{
double a[6],x[5];
int i;
a[0] = 4;
printf("a[0] = %lf\n\n", a[0]); // a[0] = 4.000000
for (i=0;i<=6;i++)
{
x = 6;
}
printf("a[0] = %lf\n\n", a[0]); // a[0] = 6.000000 return 0;
}

If I run it, first is says a[0] = 4 and later a[0] = 6. But I didn't
change a[0]. How is that possible, do I do something wrong or is there a
ghost in my computer switching bits ;).
I compiled it on an other computer, and got the same problem.



#include <stdio.h> /* mha: added. There _must_ be a
prototype for the variadic function
printf() */

int main(void)
{
double a[6] = { 4 }, x[5];
size_t i;

printf("a[0] = %f\n\n", a[0]);
#if 0
/* mha: the following tries to illegally write to the non-existent
array elements x[5] and x[6]. Fixed below. */
for (i = 0; i <= 6; i++) {
x = 6;
}
#endif
for (i = 0; i < sizeof x / sizeof *x; i++)
x = 6;
printf("a[0] = %f\n\n", a[0]);
}
 
D

Dan Pop

In said:
Jasper Aikema said:
I do have the next code in my program:
int main(void)
{
double a[6],x[5];
int i;

a[0] = 4;

printf("a[0] = %lf\n\n", a[0]); // a[0] = 4.000000

No need for "%lf" in printf - "%f" will also do (you only need it
for scanf()).

In C89 it's worse than "no need", "%lf" in printf invokes undefined
behaviour. It's a new feature of C99, for the sake of improving the
symmetry between scanf and printf conversion specifiers.

Dan
 

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,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top