Author Topic: Is the result of "printf" and "debug watch" different?  (Read 5109 times)

hnust_zhangyehua

• Multiple posting newcomer
• Posts: 10
Is the result of "printf" and "debug watch" different?
« on: June 30, 2016, 10:08:20 am »
I have to ask for a help, for I meet this problem twice. The problem is that the result , which is printed by the "printf" and which is showed by the "debug watches" is different. For example, the value of integer a printed by the "printf" is 0, while the other is  1.
Can anyone help me?

BlueHazzard

• Developer
• Lives here!
• Posts: 2716
Re: Is the result of "printf" and "debug watch" different?
« Reply #1 on: June 30, 2016, 10:57:24 am »
not without example code...

hnust_zhangyehua

• Multiple posting newcomer
• Posts: 10
Re: Is the result of "printf" and "debug watch" different?
« Reply #2 on: June 30, 2016, 11:21:31 am »
#include <iostream>
#include<cstdio>
using namespace std;
int m=0,k=0,n,c[100]={0},f=0,g;
typedef struct LNode
{
int data;
struct LNode *lchild,*rchild;
}LNode,*List;
void bl(List &T)//遍历后应该输出的数按顺序存入数组c中。
{
if(T)
{
bl(T->rchild);
c[f]=T->data;
printf("c[%d]=%d\n",f,c[f]);/* ********这条语句在VC和CB中跑的结果不一样******** */
f++;
bl(T->lchild);
}
}
void creat(List &T,int e)//创建树
{
if(!T)
{
LNode *S;
S=new LNode;
S->data=e;
S->lchild=S->rchild=NULL;
T=S;
}
else if(e>T->data)creat(T->rchild,e);
else if(e<T->data)creat(T->lchild,e);
}
List search1(List &T,int e)//查找树的节点，用m来记载节点的层数；即深度。
{
if((!T)||e==T->data)return T;
else if(T)
{
m++;
if(e>T->data)return search1(T->rchild,e);
if(e<T->data)return search1(T->lchild,e);
}
//return m;
}
int main()
{
int a[100],b[100],z[100],i,s,F,w,x;
LNode *Q;
while((scanf("%d",&n)!=EOF))
{
m=w=x=k=f=g=0,F=1;
Q=NULL;
Q=new LNode;
Q->lchild=Q->rchild=NULL;
//cin>>n;//cout<<'123'<<'\n';
if(n<0||n>100)break;
for(i=0;i<n;i++)
{
cin>>a;
}

for(w=1; w<n; w++)
{
for(x=0; x<w; x++)
{
if(a
• ==a[w])

break;
//z[w]=a[w];
}
if(x==w)
{
z[F]=a[w];
F++;
}
}
z[0]=a[0];

for(i=0;i<F;i++)
{
creat(Q,z);
}
bl(Q);
for(i=0;i<F;i++)
{
search1(Q,c);//用数组b存储树的层数；即深度
b[k]=m;k++;
m=0;
}
for(i=0;i<F;i++)
{
for(g=0;g<4*(b-1);g++)
{
cout<<" ";
}
cout<<c<<endl;
g=0;
}
}
return 0;
}
/*
1725: 二叉查找树(Ⅱ)-文本显示

[提交][状态][讨论版]

input data
6
4 2 1 5 3 6

output data
6
5
4
3
2
1

}*/

hnust_zhangyehua

• Multiple posting newcomer
• Posts: 10
Re: Is the result of "printf" and "debug watch" different?
« Reply #3 on: June 30, 2016, 11:22:57 am »
The input data is at the bottom

BlueHazzard

• Developer
• Lives here!
• Posts: 2716
Re: Is the result of "printf" and "debug watch" different?
« Reply #4 on: June 30, 2016, 04:44:20 pm »
pls use code tags (the # symbol in the forum editor) to post code, it is more easy to read the code...

i can't find any discrepancy between debugger and printf... But i had to correct your code in a lot places, because the formatting is wrong
, better you repaste the code in code tags...

hnust_zhangyehua

• Multiple posting newcomer
• Posts: 10
Re: Is the result of "printf" and "debug watch" different?
« Reply #5 on: July 01, 2016, 11:13:13 am »
Would you like to leave your Facebook or other contact information, which can make our  communication more convenient for i really want to
solve this problem?

BlueHazzard

• Developer
• Lives here!
• Posts: 2716
Re: Is the result of "printf" and "debug watch" different?
« Reply #6 on: July 01, 2016, 05:04:46 pm »
To solve the problem you can try the following steps:
1) post the code in code tags, so the formatting wont mess up
Quote
for(x=0; x<w; x++)
{
if(a

==a[w])

break;
//z[w]=a[w];
}
here is clearly a error, because 'a' is a array, and you can't compare a integer 'a[w]' to a array (pointer)
2) Is your problem only in codeblocks or also in gdb:
a) What does the debugger log says if you type "p a" ? It can be possible that codeblocks parses the debug output wrong...
b) for this it would be nice if you can enable full debug logging (Settings->Debugger->Common->Full (Debug) log ) and then try to debug your application. If you detect the error please copy and paste the log  from the "Debugger" tab here. (Please use also code tags (# symbol or [ code ][ \code ] for the log)

greetings

hnust_zhangyehua

• Multiple posting newcomer
• Posts: 10
Re: Is the result of "printf" and "debug watch" different?
« Reply #7 on: July 02, 2016, 03:54:54 pm »
I paste the code on the Ubuntu Pastebin

http://paste.ubuntu.com/18302527/

You can open the link which shows my code.
The line 16 on the code is my question.You can copy the code to the codeblocks,and then,use the input date and watch the output of the array C. Subsequently, you use "shift + F7" to debug, and use
"F4" to jump the line 84, and use "F7". In this monment, you can watch the "printf window" or the "debug watch" to check the array C , ans you will find the difference from array c.

I really thanks for your help

BlueHazzard

• Developer
• Lives here!
• Posts: 2716
Re: Is the result of "printf" and "debug watch" different?
« Reply #8 on: July 02, 2016, 06:20:24 pm »
sry, but i can't find any discrepancy...
can you post a screenshot?

hnust_zhangyehua

• Multiple posting newcomer
• Posts: 10
Re: Is the result of "printf" and "debug watch" different?
« Reply #9 on: July 04, 2016, 04:52:16 am »
the first photo is the "printf" after execute

the second photo is the "print" in the process of debug
« Last Edit: July 04, 2016, 04:55:09 am by hnust_zhangyehua »

hnust_zhangyehua

• Multiple posting newcomer
• Posts: 10
Re: Is the result of "printf" and "debug watch" different?
« Reply #10 on: July 04, 2016, 04:57:08 am »
How can i post the picture?   I press the "insert images" that has no reaction.

BlueHazzard

• Developer
• Lives here!
• Posts: 2716
Re: Is the result of "printf" and "debug watch" different?
« Reply #11 on: July 04, 2016, 08:44:09 am »
You can add the image as attachement add the bottom, or better you use a imige hoster like https://postimage.org/ and post the link here

Greetings

hnust_zhangyehua

• Multiple posting newcomer
• Posts: 10
Re: Is the result of "printf" and "debug watch" different?
« Reply #12 on: July 04, 2016, 10:50:34 am »
the first photo which stands for "printf window" after execute

https://postimg.org/image/h7qjtbbbf/

the second photo which stands for "debug watch" and "printf window" in the debugging, the third photo is the position of debugging.

https://postimg.org/image/7oguzunt7/6cf18042/

Apparently, the first photo is differren from the second photo.

Greets

BlueHazzard

• Developer
• Lives here!
• Posts: 2716
Re: Is the result of "printf" and "debug watch" different?
« Reply #13 on: July 04, 2016, 11:50:20 am »
Now i begin to understand:
You start your program with the debugger and it works as expected, but if you start your program without debugger it fails?

I can reproduce this. It is not a fault of the debugger, but this happens if you don't initialize a variable, like here:
Code: [Select]
`int a[100],b[100],z[100],i,s,F,w,x;`all this variables have random values. It is possible that the debugger runs on a zeroed ram segment, and all your variables are initialized with zero, but in release mode your program runs on a random filled ram segment, so your variables are filled with random values. This leads to errors like you are receiving.

This has nothing to do with codeblocks or the debugger, but with clean programming...
The solution would be to clean up your code...

hnust_zhangyehua

• Multiple posting newcomer
• Posts: 10
Re: Is the result of "printf" and "debug watch" different?
« Reply #14 on: July 04, 2016, 12:42:57 pm »
A very important problem that I forget to tell you
This code runs at Visual Stdio is good, in other words, when the code run at the Visual Stdio, its result, on the "printf windows" whether  after execute or in the debugging, is same. Moreover, the result of array C in the  Visual Stdio is same as the other result when the code is debugging in the codeblocks.

Therefore, I can get a conclusion that codeblocks goes wrong
« Last Edit: July 04, 2016, 12:56:19 pm by hnust_zhangyehua »

hnust_zhangyehua

• Multiple posting newcomer
• Posts: 10
Re: Is the result of "printf" and "debug watch" different?
« Reply #15 on: July 04, 2016, 12:55:52 pm »
A very important problem that I forget to tell you
This code runs at Visual Stdio is good, in other words, when the code run at the Visual Stdio, its result, on the "printf windows" whether  after execute or in the debugging, is same. Moreover, the result of array C in the  Visual Stdio is same as the other result when the code is debugging in the codeblocks.

Therefore, I can get a conclusion that codeblocks goes wrong

BlueHazzard

• Developer
• Lives here!
• Posts: 2716
Re: Is the result of "printf" and "debug watch" different?
« Reply #16 on: July 04, 2016, 03:38:08 pm »
codeblocks per se does not go wrong, because codeblocks is not a compiler and for this it has nothing to do with your code / your compiled program...
I don't think that in so simple code a compiler bug triggers so i think your code is wrong on one point... I made a quick look, but i didn't find anything critical (beside that you are not deleting Q after a new call)... One thing i can think of is, that cin reads some garbage from the input buffer...
you can try cpp check to analyze your code: http://cppcheck.sourceforge.net/

Code: [Select]
`void creat(List &T,int e) //build the tree{    if(!T)    {        LNode *S;        S=new LNode;        S->data=e;        S->lchild=S->rchild=NULL;        T=S;    }    else if(e>T->data)creat(T->rchild,e);    else if(e<T->data)creat(T->lchild,e);}`You assign T a new allocated List, but what happens with the old List??? This is for example a memory leak you should avoid. And i think the whole code is full with this... If your code works, it is luck or tolerance of your OS or compiler, but at one point it will blow up... The consequence is (or better can be) that your program works under debug mode but not in release...

sry i can't help more, but this is now out of the scope of this forum, as you don't have a problem with codeblocks, but with the underlying compiler...

« Last Edit: July 04, 2016, 03:40:18 pm by BlueHazzard »

raynebc

• Almost regular
• Posts: 216
Re: Is the result of "printf" and "debug watch" different?
« Reply #17 on: July 04, 2016, 08:50:41 pm »
That linked list logic looks wrong to me.  When you add a link, it correctly sets either the lchild or rchild pointer of the list's current tail, but the other pointer is left at its initialized value of NULL.  Unless you ensure both pointers are correctly adjusted every time you append or prefix a link, it will not correctly function as a doubly linked list.