Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Language IDEs » C / C++ IDE (CDT) » How can the declaration of an unused variable affect behavior?(question about variable declaration in c)
How can the declaration of an unused variable affect behavior? [message #1006792] Fri, 01 February 2013 22:55 Go to next message
dan lior is currently offline dan liorFriend
Messages: 4
Registered: October 2012
Junior Member
I am using eclipse juno with gcc (from xcode) on MacOS.

The behaviour of the following C program changes radically when the declaration

int x;

is omitted. The variable x does not appear anywhere else in the code. How can this happen?

Here, data.txt is a simple text file with two digit numbers stored as characters. Something like 08 90 88 77 34 etc...

With the declaration of x included, the program opens a file, displays the first two numbers (as integers) and closed the file. Without the declaration, the program opens the file and displays the first number. It doesn't properly close the file.

How can this be?



#include <stdio.h>

int main (void)
{

FILE *fp;

// if I remove this declaration, the program does not work properly!!!
// this variable is not even used in the program

int x;

char str[2];



fp = fopen("data.txt", "r");
if (fp == NULL){
printf("\n unsuccessfull file open \n");
return 1;
}else{
printf("\n successfull file open \n");
}



fscanf(fp,"%2s",str);
printf("%s ",str);

fscanf(fp,"%2s",str);
printf("%s ",str);




if (fclose(fp)) {
printf("\n unsuccessfull file close \n");
}else{
printf("\n successfull file close \n");
}

return 0;
}

Re: How can the declaration of an unused variable affect behavior? [message #1006797 is a reply to message #1006792] Sat, 02 February 2013 01:05 Go to previous messageGo to next message
David Wegener is currently offline David WegenerFriend
Messages: 1445
Registered: July 2009
Senior Member
On 02/01/2013 04:55 PM, dan lior wrote:
> I am using eclipse juno with gcc (from xcode) on MacOS.
> The behaviour of the following C program changes radically when the
> declaration
> int x;
>
> is omitted. The variable x does not appear anywhere else in the code.
> How can this happen?
>
> Here, data.txt is a simple text file with two digit numbers stored as
> characters. Something like 08 90 88 77 34 etc...
>
> With the declaration of x included, the program opens a file, displays
> the first two numbers (as integers) and closed the file. Without the
> declaration, the program opens the file and displays the first number.
> It doesn't properly close the file.
> How can this be?
>
>
> #include <stdio.h>
>
> int main (void)
> {
>
> FILE *fp;
>
> // if I remove this declaration, the program does not work properly!!!
> // this variable is not even used in the program
>
> int x;
>
> char str[2];
>
>
>
> fp = fopen("data.txt", "r");
> if (fp == NULL){
> printf("\n unsuccessfull file open \n");
> return 1;
> }else{
> printf("\n successfull file open \n");
> }
>
>
>
> fscanf(fp,"%2s",str);
> printf("%s ",str);
>
> fscanf(fp,"%2s",str);
> printf("%s ",str);
>
>
>
>
> if (fclose(fp)) {
> printf("\n unsuccessfull file close \n");
> }else{
> printf("\n successfull file close \n");
> }
>
> return 0;
> }
>
>
Strings get terminated with a null character. Your array is only 2
bytes long.
Re: How can the declaration of an unused variable affect behavior? [message #1006798 is a reply to message #1006797] Sat, 02 February 2013 02:39 Go to previous messageGo to next message
dan lior is currently offline dan liorFriend
Messages: 4
Registered: October 2012
Junior Member
David,

Thank you so much for your quick, clear and correct answer. I really appreciate it.

While I'm at it, let me just ask you to clarify something for me:

I was accessing memory beyond the reserved array space. That's the reason that it resulted in wacky (and probably unpredictable) results.

Is that right?

What really threw me when I was trying to debug this was that the error seemingly disappeared when I declared a variable at that spot. I guess I was causing the compiler to make some room just after the space allocated to the array, thus hiding the mistake.

Is that right? (or at least possible)

WOW. What an insidious bug!

dan
Re: How can the declaration of an unused variable affect behavior? [message #1006808 is a reply to message #1006798] Sat, 02 February 2013 05:40 Go to previous message
David Wegener is currently offline David WegenerFriend
Messages: 1445
Registered: July 2009
Senior Member
On 02/01/2013 08:39 PM, dan lior wrote:

> What really threw me when I was trying to debug this was that the error
> seemingly disappeared when I declared a variable at that spot. I guess I
> was causing the compiler to make some room just after the space
> allocated to the array, thus hiding the mistake.
> Is that right? (or at least possible)

Yes, this is what was happening.

Note that your question really wasn't an Eclipse CDT question, it was a
basic programming question. That really isn't the purpose for these
forums. This forum is for questions specifically relating to using the CDT
Previous Topic:How do I stop Builder Settings adding a -k to the Makefile command
Next Topic:Issues on Eclipse Semantic Checker
Goto Forum:
  


Current Time: Thu Mar 28 14:20:03 GMT 2024

Powered by FUDForum. Page generated in 0.03385 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top