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

Offline 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?

Offline BlueHazzard

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

Offline 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: 二叉查找树(Ⅱ)-文本显示
时间限制: 1 Sec  内存限制: 128 MB
提交: 425  解决: 204
[提交][状态][讨论版]
题目描述
本任务是在空二叉查找树的基础上,依次插入一些关键字,然后文本方式显示该树。要求树中不能有重复关键字。


输入
由多组数据 组成。

每组数据由两行组成。第一行是待插入关键字的数目n(1<=n<=100)。第2行是n个空格分开的正整数,值不超过100。


输出
对于每一组数据,显示对应的二叉查找树,相当于常规树形左旋90度。见样例。 注意每一层缩进为4,每一行行尾没有空格符号。


input data
6
4 2 1 5 3 6

output data
        6
    5
4
        3
    2
        1


}*/

Offline 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

Offline BlueHazzard

  • Developer
  • Lives here!
  • *****
  • Posts: 3353
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...

Offline 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?

Offline BlueHazzard

  • Developer
  • Lives here!
  • *****
  • Posts: 3353
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

Offline 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 »
Thanks for your help.
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  ;D

Offline BlueHazzard

  • Developer
  • Lives here!
  • *****
  • Posts: 3353
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?


Offline 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 »

Offline 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. :-[

Offline BlueHazzard

  • Developer
  • Lives here!
  • *****
  • Posts: 3353
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

And please post also the debugger log in code tags

Greetings

Offline 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 »
Thanks for your picture link.
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/
https://postimg.org/image/p31384ky3/dad9b28d/

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

Greets


Offline BlueHazzard

  • Developer
  • Lives here!
  • *****
  • Posts: 3353
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
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...

Offline 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 »