Suddenly a variable value changes

Oct 26, 2011 at 7:22pm
Hi all,

I have a strange problem. Inside a loop after a certain number iterations value of a variable changes. Nowhere in the loop I try to change its value but it changes!

For example:

loop
A=b
end loop

Suddenly value of "b" changes!

Any idea?
Oct 26, 2011 at 7:29pm
You will need to post some code.
Oct 26, 2011 at 7:51pm
The program is very long. But let me put some portion of it. Here when n=933 and k=1533 cent[255] changes?! I have checked up to these n and k and saw that cent[255] is not changing until these n and k.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
	
	for (int n=0; n<(n_3D*n_faces_one_cell); n++) 
	{
		for (int k=0; k<(n_3D*n_faces_one_cell); k++) 
		{
			if (n == k) {continue;}

//--Internal Faces-----------------------------------------------

			if ( (dface[n].cID == dface[k].cID) && dface[n].used != 1 ) 
			{
				face[m].boun      = 0;
				dface[n].used     = 1;
				dface[k].used     = 1;
				face[m].cID       = dface[n].cID;					
				face[m].P         = cent[dface[n].P].cID;    
				face[m].N         = cent[dface[k].P].cID;
				face[m].PN        = node[face[m].N] - node[face[m].P];
				face[m].Pf        = node[face[m].cID] - node[face[m].P];
				face[m].Nf        = node[face[m].cID] - node[face[m].N];
				face[m].f         = ( face[m].Pf | norm(face[m].PN) ) / mag(face[m].PN);
				face[m].vertex[0] = dface[n].vertex[0];
				face[m].vertex[1] = dface[n].vertex[1];
				face[m].vertex[2] = dface[n].vertex[2];
				face[m].vertex[3] = dface[n].vertex[3];
				face[m].Af        = dface[n].Af;
				Ad_num            = face[m].Af | face[m].Af;
				Ad_den            = face[m].Af | norm(face[m].PN);
				face[m].Ad        = (Ad_num/Ad_den) * norm(face[m].PN);					
				face[m].At        = face[m].Af - ( mag(face[m].Ad) * norm(face[m].PN) );
					
				++sig;			
					
				break;
			}
		} // k-loop

//--Boundary faces-----------------------------------------------

		if (sig == 0) 
		{
			face[m].boun      = 1;
			face[m].cID       = dface[n].cID;
			face[m].P         = cent[dface[n].P].cID;			
			face[m].N         = 0;				// for simplicity
			face[m].Pf        = node[face[m].cID] - node[face[m].P];
			face[m].Nf.i      = 0.0f;			// for simplicity
			face[m].Nf.j      = 0.0f;			// for simplicity
			face[m].Nf.k      = 0.0f;			// for simplicity
			face[m].PN        = face[m].Pf;
			face[m].vertex[0] = dface[n].vertex[0];
			face[m].vertex[1] = dface[n].vertex[1];
			face[m].vertex[2] = dface[n].vertex[2];
			face[m].vertex[3] = dface[n].vertex[3];
			face[m].Af        = dface[n].Af;
			face[m].Ad        = face[m].Af;
			face[m].At.i      = 0.0f;
			face[m].At.j      = 0.0f;
			face[m].At.k      = 0.0f;

			// Getting physical numbers of boundary faces
			for (int k=0; k<n_2D; ++k) 
			{
				if (face[m].cID == el_2D[k].cID)
				{
					face[m].phys = el_2D[k].phys;
				}
			}
		}

		++m;
			
		sig = 0;
	}	
}


Oct 26, 2011 at 9:08pm
Are there any other threads running?


What I would do is set a breakpoint to trip when n=933 and k=1533, then ste through the code and watch cent[255] to see where it changes.

Some debuggers also allow you to set breakpoints that trip when certain variables have changed -- so you could set a breakpoint on cent[255] to find exactly where the offending line is.
Oct 26, 2011 at 10:11pm
I did it. I gave the memory address of the unexpectedly changing variable. It says that number of hit is zero, that is value is not changing according to it. I also monitored that variable and it is changing :(

I also noticed that when I initialized some pointers as NULL that variable changes its value in another time, say n= 892, k= 147.

So I initialized all pointers as NULL but hopeless, still changing its value.
Oct 26, 2011 at 11:14pm
Well one way or another, I would try to find out exactly which line is changing it, then that will help narrow down where the problem is. If that means you have to step through the code line by line in the debugger until you find the culprit, then that's what I'd do.
Oct 28, 2011 at 4:24pm
SOLVED:

I declared my arrays as,

int var[size]

but it should be,

int *var = new int [size]

I don't know details of differences of them but it seems that if former declaration is used members of array may be "stolen" somehow because no memory allocated for it. Good, but why it is so although [size] is written at the end for the former declaration?
Topic archived. No new replies allowed.