strange effect: if ... else in C influence a previous statement


Login or Register for Dates, Times and to Reply

 
Thread Tools Search this Thread
# 1  
strange effect: if ... else in C influence a previous statement

I write a short code in c

it may work well, but when I add an if ... else ... structure, a computation before the structure can not give a correct result

compilation and run can do well, but the result is wrong. Very strange

(1)
y2 = expression; //y2 = 0 always 0 error
.................

add : if(a->is_root) c = m2 - y2;
else f = m2 - y2;


(2)

y2 = expression; //y2 = 0 always 0 error
.................

add : if(a->is_root) c = m2 - y2;
if(!a->is_root) f = m2 - y2;


(3)
y2 = expression; // y2 = normal value; normal
.................

add : //if(a->is_root) c = m2 - y2;
if(!a->is_root) f = m2 - y2;


(4)

y2 = expression; // y2 = normal value; normal
.................

add : if(a->is_root) c = m2 - y2;
//if(!a->is_root) f = m2 - y2;

(5)
y2 = expression; // y2 = normal value; normal

add : //if(a->is_root) c = m2 - y2;
//if(!a->is_root) f = m2 - y2;

Trouble: can not use if... else ... normally

Question: (1) if... else ... can influence a previous statement, why?
(2) How to solve the problem?

Last edited by cdbug; 11-20-2008 at 06:38 AM..
# 2  
It would be helpful if you could provide an compilable example of your problem.
# 3  
This happens in a large function, which has a relatively complicated algorithm.

Simply speaking, its structure is as such:
___________________________________________________________
some basic calculation
------------------------------

y = expression;

-------------------------------
____________________________________________________________
some operations
--------------------------------

--------------------------------

if {
block 1;
}

else{
if { block 2; }
else { block 3; }
}

The structure of blocks 2 and 3 will change the way in which y is calculated. The result is wrong. I check it through gdb. The five results above are obtained at the same environment except for the changes in the two blocks

In many executions, y is always equal to 0. Otherwise, it will be far larger than a right value.

But when either or each of the two blocks is removed, y will obtain a normal value.

I can not understand
# 4  
I'm afraid your description of the problem give no clue.
Without a compilable example, nobody is going to guess what is wrong with your code.
# 5  
The only possible situation here is that block2 or block3 are overflowing a variable on your function's stack and your y var is being changed.

A function's stack is composed (growing from high memory to low memory) by variables (in reverse order), eip, esp, function name and esp.

Here is a simple example for your possible situation:
Code:
void of()
{
    char a[10];
    char b[10];
    strcpy (a, "aaaaaaaaaa");
    printf ("%s\n", a);
    strcpy(b, "bbbbbbbbbboverflow");
    printf ("%s\n", a);
}

void
main ()
{
    of();
}

As you will be able to see, variable b overflows into variable a.
# 6  
The mistaken value is assigned to y when execute 'y = expression;'

in gdb, 'p y' will give the mistaken value after runing the above statement.

but ' p expression' gives the right value.

block2 and block3 are like this:

if(a->is_root) c = m2 - y2;
else f = m2 - y2;

As said before:
only one statement: if(a->is_root) c = m2 - y2; // y2 normal
only one statement: if(!a->is_root) f = m2 - y2; // y2 normal

but:

if(a->is_root) c = m2 - y2;
else f = m2 - y2;

or

if(a->is_root) c = m2 - y2;
if(!a->is_root) f = m2 - y2;

both make y2 abnormal. 'y2 = expression' is far away from if__ else __ structure

Thanks for your teaching

Last edited by cdbug; 11-23-2008 at 05:28 AM..
# 7  
Hum! What kind of var is y2? What is the expression assigned to it? There are really several things that can cause that so is a bit hard to help you without the full compilable code.
Login or Register for Dates, Times and to Reply

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #682
Difficulty: Easy
Steve Job's was the primary founder and majority shareholder of Pixar
True or False?

7 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

[Solved] Strange Problem in case statement while shift

Hi, Here is my code as below: test.ksh: ======= #!/bin/ksh option="${1}" while do case $1 in -f) FILE="${2}" echo "File name is $FILE" ;; -d) DIR="${2}" echo "Dir name is $DIR" ;; -*) echo "`basename ${0}`:usage: | " (5 Replies)
Discussion started by: zaq1xsw2
5 Replies

2. Shell Programming and Scripting

Alias has no effect in script

It doesn't have effect in script but it works on the terminal root@server:/opt/kvm/usecases/logs# alias echo='echo -e' root@server:/opt/kvm/usecases/logs# echo "xxxx\n" xxxx root@server:/opt/kvm/usecases/logs# cat xx.sh #!/bin/bash alias echo='echo -n' echo "sssf \n" ... (3 Replies)
Discussion started by: yanglei_fage
3 Replies

3. Shell Programming and Scripting

Remove previous line if next & previous lines have same 4th character.

I want to remove commands having no output. In below text file. bash-3.2$ cat abc_do_it.txt grpg10so>show trunk group all status grpg11so>show trunk group all status grpg12so>show trunk group all status GCPKNYAIGT73IMO 1440 1345 0 0 94 0 0 INSERVICE 93% 0%... (4 Replies)
Discussion started by: Raza Ali
4 Replies

4. UNIX for Dummies Questions & Answers

What crontab is in effect after reboot

A basic cron question and I hope I explain it enough. If a person creates a file to use with cron and issues the followin command: crontab I understand this will set the cron entries to whatever is in . However, if we now edit the crontab with: crontab -e and save, how does cron... (3 Replies)
Discussion started by: niswonp
3 Replies

5. Virtualization and Cloud Computing

The Network Effect - Part 1

2008-10-31T22:46:14+01:00 http://geekandpoke.typepad.com/.a/6a00d8341d3df553ef010535ce543e970c-800wi Nicholas Carr (and here) has some problems with Tim O'Reilly's theory about the cloud and the network effect. http://feeds.feedburner.com/~f/GeekAndPoke?i=OFn0M... (0 Replies)
Discussion started by: Linux Bot
0 Replies

6. UNIX for Advanced & Expert Users

Effect of Preemptive Kernel

hi there, i am porting kernel 2.2 driver program to kernel 2.6. for some extent i am successfull but some times the system gets hanged. what might be the problem? i am not able to get any help from log messages as nothing is being printed at that moment. hey does this kernel preemptiveness and... (2 Replies)
Discussion started by: sriram.ec
2 Replies

7. UNIX for Dummies Questions & Answers

recursive effect!!

I run the following command in some of my folders... and ended up with a huge mess!! find . -type f -exec perl -e 's/blabla/zzzxxxx/gi' -p -i.bak {} \; I had to kill the process and later when I checked with one of my folders.. ls vaditerm.dt.bak vaditerm.dt.bak.bak... (2 Replies)
Discussion started by: sskb
2 Replies

Featured Tech Videos