0

I get a pointer with 0xCDCDCDCD when I want to print a tree but no problem when I want to print a queue in the almost same code

I write a queue by a c program and revise it to print as a tree , that queue was successful printed but when I tried to print the tree my visual studio told me there was a 0xCDCDCDCD problem.

this is the previous version that ran well.

#include<stdio.h>
#include<stdlib.h>

typedef struct _node
{
   long long value;
    //int value
   struct _node *next;
}Node;

typedef struct _Queue
{
   Node *head;
   Node *tail;
}Queue;

Queue* init_queue()
{
   Queue *queue=(Queue*)malloc(sizeof(Queue));
   queue->head = queue->tail = NULL;
   return queue;
}

void enQueue(Queue *pQueue,Node *pNode)
{
      if(pQueue->head == NULL)
      {//when it's empty
           pQueue->head = pNode;
       pQueue->tail = pNode;
      }
      else
      {
           pQueue->tail->next = pNode;
       pQueue->tail = pNode;
      }
}

int deQueue(Queue *pQueue)
{
    int i;

    if(pQueue->head == NULL)
    {
       return NULL;
    }
   // Node  *deNode;
   //Node *tmp;

    //Node  *deNode;
    //deNode= pQueue->head;
    Node  *deNode= pQueue->head;

    //Node *tmp= pQueue->head;

    i=deNode->value;

    pQueue->head= pQueue->head->next;

    //free(deNode);

        return i;
}

Node* init_node(int value)
{
    Node  *new_node = (Node*)malloc(sizeof(Node));
    new_node->value=value;
    return new_node;
}

//0:empty
int ifEmpty(Queue *pQueue)
{
   if(pQueue->head == NULL)
   {
     printf("empty tree\n");
     return 0;
   }

   printf("queue is not empty\n");
   return 1;
}

int main()
{
  Queue *queue=init_queue();
  int i;

  ifEmpty(queue);
  printf("insert node to queue\n");
  for(i=0; i<7;i++)
  {
   Node *node = init_node(i);
   enQueue(queue,node);
  // free(node);
  }

//  Node *node = init_node(1);
//  printf("node->value = %d\n",node->value);
//  enQueue(queue,node);
  ifEmpty(queue);

  for(i=0;i<7;i++)
  {
      int deNode = deQueue(queue);

      //if(!deNode)
      //{
         //printf("NULL\n");
      //}
      //else
      //{
         printf("deNode->value = %d\n",deNode);

      //}


  }

   //int deNode = deQueue(queue);
  //free(queue);

  // printf("\n after free deNode->value = %d\n",queue->head->value);


    ifEmpty(queue);

  return 0;
}

I want to change this into a tree format but my visual studio told me there was a 0xCDCDCDCD problem. below are my codecs:

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define NUM 6


typedef struct _node
{
   int value;
   struct _node *left;
   struct _node *right;



}TNode,*Tree;

//add a *next in q_node is my purpose
//other wise , we need to add in the Tree node struct
//So, for the sake of doesn't modify the struct of tree
//I design a q_node struct to include it
//we can use define command to make it as a template.
typedef struct _q_node
{
  TNode *t_node;
  int depth;
  int blank;  //0: means correspoinding tree node is not NULL(default)
  struct _q_node *next;
}QNode;

typedef struct _Queue
{
   QNode *head;
   QNode *tail;
}Queue;

Queue* init_queue()
{
   Queue *queue=(Queue*)malloc(sizeof(Queue));
   queue->head = queue->tail = NULL;
   return queue;
}


int enQueue(Queue *pQueue,TNode *pTNode,int pDepth)
{

      QNode *pQNode = (QNode *)malloc(sizeof(QNode));
      pQNode->depth = pDepth;
      pQNode->blank = 0; //default config
      if(pTNode==NULL)
      {
         //change default setting; 1 means it's blank QNode
         pQNode->blank =1;
      }

      pQNode->t_node= pTNode;

      if(pQueue->head == NULL)
      {//when it's empty
           pQueue->head = pQNode;
       pQueue->tail = pQNode;
      }

      else
      {
           pQueue->tail->next = pQNode;
       pQueue->tail = pQNode;
      }
}

QNode* deQueue(Queue *pQueue)
{
    if(pQueue->head == NULL)
    {
       return NULL;
    }

    QNode *deNode= pQueue->head;

    pQueue->head = pQueue->head->next;
    //pQueue->head = pQueue->head->next;

    //deNode = deNode->next;

        return deNode;
}

TNode* init_TNode(int value)
{
    TNode  *new_node = (TNode*)malloc(sizeof(TNode));
    new_node->value=value;
    new_node->left = new_node->right = NULL;
    return new_node;
}

//0:empty
int ifEmpty(Queue *pQueue)
{
   if(pQueue->head == NULL)
   {
     //printf("empty tree\n");
     return 0;
   }

   //printf("queue is not empty\n");
   return 1;
}


int insert_tree(Tree pTree,int pValue)
{

   //found NULL sub tree, then add to his father->left
   if(!pTree)
   {
      return 0;
   }

   TNode *tNode = init_TNode(pValue);

   if(tNode==NULL)
   {
       printf("create TNode error!\n");
       return 0;
   }



   if(pValue < pTree->value)
        if(insert_tree(pTree->left,pValue)==0)
        {
       //no left child any more,set a new left child to pTree
       pTree->left = tNode;
          printf("insert :%d\n",pValue);
    }
   if(pValue > pTree->value)
        if(insert_tree(pTree->right,pValue)==0)
        {
           pTree->right = tNode;
       printf("insert :%d\n",pValue);
        }
}

Tree creatTree(int num)
{
    srand(time(NULL));
    Tree root = init_TNode(rand()%100);
    printf("root is %d\n",root->value);
    int i ;
    for(i=1;i<num;i++)
    {
        insert_tree(root,rand()%100);
    }
    printf("creat tree succuess!Tree heigh is:%d\n",get_tree_height(root));
    return root ;
}

int get_tree_height(Tree pRoot)
{

  if(!pRoot)
  {
    return 0;
  }

  int lh=0,rh=0;
  lh = get_tree_height(pRoot->left);
  rh = get_tree_height(pRoot->right);
  return (lh<rh)?(rh+1):(lh+1);
}



int breath_travel(Tree pRoot,Queue *pQueue)
{
   int height = get_tree_height(pRoot);
   int pad_num = 3;
   //compare to the node's depth in the "while loop"
   int current_depth = 1;
   if(!pRoot)
   {
      return 0;
   }

   enQueue(pQueue,pRoot,1);

   printf("_______________________\n");
   printf("breath begin,enter root:\n");

   while(ifEmpty(pQueue)!=0)
   {

     QNode  *qNode  = deQueue(pQueue);
     //the sub node's depth is 1 more then the parent's
     int child_depth = qNode->depth + 1 ;

     if(qNode->depth > current_depth)
     {
         current_depth = qNode->depth;
         printf("\n\n");
     }
// ***************0****************  pad_between = 31 ; pad_front = 15  (depth == 1)  
// *******0***************0********  pad_between = 15 ; pad_front = 7   (depth == 2)    
// ***0*******0*******0*******0****  pad_between = 7  ; pad_front = 3   (depth == 3)
// *0***0***0***0***0***0***0***0**  pad_between = 3  ; pad_front = 1   (depth == 4)
// 0*0*0*0*0*0*0*0*0*0*0*0*0*0*0*0*  pad_between = 1  ; pad_front = 0;  (depth == 5)
// Tree height = 5
// pad_num = 1
// padding between node = (1+2*pad_front)*pad_num = (1+ (1<<(height-depth))-1)*pad_num
// (1<< (height - current_depth))-1=2^(height - current_depth)-1


     int pad_front = (1<< (height - current_depth))-1;

     if((qNode->blank == 1))
     {
       //add the parent node's padding:2
        if(pad_front == 0)    printf("%*s%*s",pad_num,"o",pad_num," ");
        else                  printf("%*s%*s%*s",pad_front*pad_num," ",pad_num,"o",(1+pad_front)*pad_num," ");
        //pad_front*pad_num+(1+pad_front)*pad_num=(1+2*pad_front)*pad_num=padding between node
       if(child_depth <= height)
       {
         //enter two NULL sub-tree node.
         //every time you enter NULL TNode,there's corresponding blank QNode.
         enQueue(pQueue,NULL,child_depth);
         enQueue(pQueue,NULL,child_depth);
       }
     }
     else
     {


if(pad_front == 0)   printf("%*d%*s",pad_num,qNode->t_node->value,pad_num," ");
        else                 printf("%*s%*d%*s",pad_front*pad_num," ",pad_num,qNode->t_node->value,(1+pad_front)*pad_num," ");
       if(child_depth <=height)
       {
     enQueue(pQueue,qNode->t_node->left,child_depth);
         enQueue(pQueue,qNode->t_node->right,child_depth);
       }
     }

   } //while end
     printf("\n-----------\nbreath end!\n-----------\n");

   return 1;
}

int main(int argc,char **argv)
{
  Queue *queue=init_queue();
  int i;

  ifEmpty(queue);
  printf("insert node to queue\n");

  int num = NUM; //default

  if(argc == 2)
  {
    num = atoi(argv[1]);
  }

  Tree root = creatTree(num);
  if(!root)
  {
    printf("create Tree failed!\n");
    return 0;
  }

  breath_travel(root,queue);
  return 0;
}

the problem is in the deQueue function when I want to print the tree my visual studio told me " pQueue->head= pQueue->head->next;" this line had a 0xCDCDCDCD problem in the deQueue function I don't know why I even tried to put all this funtion in the main function but the problem was still there:

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define NUM 6


typedef struct _node
{
    int value;
    struct _node *left;
    struct _node *right;



}TNode, *Tree;

//add a *next in q_node is my purpose
//other wise , we need to add in the Tree node struct
//So, for the sake of doesn't modify the struct of tree
//I design a q_node struct to include it
//we can use define command to make it as a template.
typedef struct _q_node
{
    TNode *t_node;
    int depth;
    int blank;  //0: means correspoinding tree node is not NULL(default)
    struct _q_node *next;
}QNode;

typedef struct _Queue
{
    QNode *head;
    QNode *tail;
}Queue;

Queue* init_queue()
{
    Queue *queue = (Queue*)malloc(sizeof(Queue));
    queue->head = queue->tail = NULL;
    return queue;
}


int enQueue(Queue *pQueue, TNode *pTNode, int pDepth)
{

    QNode *pQNode = (QNode *)malloc(sizeof(QNode));
    pQNode->depth = pDepth;
    pQNode->blank = 0; //default config
    if (pTNode == NULL)
    {
        //change default setting; 1 means it's blank QNode
        pQNode->blank = 1;
    }

    pQNode->t_node = pTNode;

    if (pQueue->head == NULL)
    {//when it's empty
        pQueue->head = pQNode;
        pQueue->tail = pQNode;
    }

    else
    {
        pQueue->tail->next = pQNode;
        pQueue->tail = pQNode;
    }
}



/*QNode* deQueue(Queue *pQueue)
{
    if (pQueue->head == NULL)
    {
        return NULL;
    }

    QNode *deNode = pQueue->head;

    pQueue->head = pQueue->head->next;
    //pQueue->head = pQueue->head->next;

    //deNode = deNode->next;

    return deNode;
}
*/
TNode* init_TNode(int value)
{
    TNode  *new_node = (TNode*)malloc(sizeof(TNode));
    new_node->value = value;
    new_node->left = new_node->right = NULL;
    return new_node;
}

//0:empty
int ifEmpty(Queue *pQueue)
{
    if (pQueue->head == NULL)
    {
        //printf("empty tree\n");
        return 0;
    }

    //printf("queue is not empty\n");
    return 1;
}


int insert_tree(Tree pTree, int pValue)
{

    //found NULL sub tree, then add to his father->left
    if (!pTree)
    {
        return 0;
    }

    TNode *tNode = init_TNode(pValue);

    if (tNode == NULL)
    {
        printf("create TNode error!\n");
        return 0;
    }



    if (pValue < pTree->value)
        if (insert_tree(pTree->left, pValue) == 0)
        {
            //no left child any more,set a new left child to pTree
            pTree->left = tNode;
            printf("insert :%d\n", pValue);
        }
    if (pValue > pTree->value)
        if (insert_tree(pTree->right, pValue) == 0)
        {
            pTree->right = tNode;
            printf("insert :%d\n", pValue);
        }
}

Tree creatTree(int num)
{
    srand((unsigned)time(NULL));
    Tree root = init_TNode(rand() % 100);
    printf("root is %d\n", root->value);
    int i;
    for (i = 1; i < num; i++)
    {
        insert_tree(root, rand() % 100);
    }
    printf("creat tree succuess!Tree heigh is:%d\n", get_tree_height(root));
    return root;
}


int get_tree_height(Tree pRoot)
{

    if (!pRoot)
    {
        return 0;
    }

    int lh = 0, rh = 0;
    lh = get_tree_height(pRoot->left);
    rh = get_tree_height(pRoot->right);
    return (lh < rh) ? (rh + 1) : (lh + 1);
}


/*
int breath_travel(Tree pRoot, Queue *pQueue)
{
    int height = get_tree_height(pRoot);
    int pad_num = 3;
    //compare to the node's depth in the "while loop"
    int current_depth = 1;
    if (!pRoot)
    {
        return 0;
    }

    enQueue(pQueue, pRoot, 1);

    printf("_______________________\n");
    printf("breath begin,enter root:\n");

    while (ifEmpty(pQueue) != 0)
    {

        //QNode  *qNode = deQueue(pQueue);
        //the sub node's depth is 1 more then the parent's

    if (pQueue->head == NULL)
        {
            return NULL;
        }


        QNode *qNode = pQueue->head;

        pQueue->head = pQueue->head->next;


        int child_depth = qNode->depth + 1;


        if (qNode->depth > current_depth)
        {
            current_depth = qNode->depth;
            printf("\n\n");
        }
        // ***************0****************  pad_between = 31 ; pad_front = 15  (depth == 1)  一共31个*
        // *******0***************0********  pad_between = 15 ; pad_front = 7   (depth == 2)    
        // ***0*******0*******0*******0****  pad_between = 7  ; pad_front = 3   (depth == 3)
        // *0***0***0***0***0***0***0***0**  pad_between = 3  ; pad_front = 1   (depth == 4)
        // 0*0*0*0*0*0*0*0*0*0*0*0*0*0*0*0*  pad_between = 1  ; pad_front = 0;  (depth == 5)
        // Tree height = 5
        // pad_num = 1
        // padding between node = (1+2*pad_front)*pad_num = (1+ (1<<(height-depth))-1)*pad_num
        // (1<< (height - current_depth))-1=2^(height - current_depth)-1


        int pad_front = (1 << (height - current_depth)) - 1;

        if ((qNode->blank == 1))
        {
            //add the parent node's padding:2
            if (pad_front == 0)    printf("%*s%*s", pad_num, "o", pad_num, " ");
            else                  printf("%*s%*s%*s", pad_front*pad_num, " ", pad_num, "o", (1 + pad_front)*pad_num, " ");
            //pad_front*pad_num+(1+pad_front)*pad_num=(1+2*pad_front)*pad_num=padding between node
            if (child_depth <= height)
            {
                //enter two NULL sub-tree node.
                //every time you enter NULL TNode,there's corresponding blank QNode.
                enQueue(pQueue, NULL, child_depth);
                enQueue(pQueue, NULL, child_depth);
            }
        }
        else
        {
            if (pad_front == 0)   printf("%*d%*s", pad_num, qNode->t_node->value, pad_num, " ");
            else                 printf("%*s%*d%*s", pad_front*pad_num, " ", pad_num, qNode->t_node->value, (1 + pad_front)*pad_num, " ");
            if (child_depth <= height)
            {
                enQueue(pQueue, qNode->t_node->left, child_depth);
                enQueue(pQueue, qNode->t_node->right, child_depth);
            }
        }

    } //while end
    printf("\n-----------\nbreath end!\n-----------\n");

    return 1;
}
*/
int main(int argc, char **argv)
{
    Queue *pQueue = init_queue();
    int i;

    ifEmpty(pQueue);
    printf("insert node to queue\n");

    int num = NUM; //default

    if (argc == 2)
    {
        num = atoi(argv[1]);
    }

    Tree pRoot = creatTree(num);
    if (!pRoot)
    {
        printf("create Tree failed!\n");
        return 0;
    }

    //breath_travel(root, queue);

    int height = get_tree_height(pRoot);
    int pad_num = 3;
    //compare to the node's depth in the "while loop"
    int current_depth = 1;
    if (!pRoot)
    {
        return 0;
    }

    enQueue(pQueue, pRoot, 1);

    printf("_______________________\n");
    printf("breath begin,enter root:\n");

    while (ifEmpty(pQueue) != 0)
    {

        //QNode  *qNode = deQueue(pQueue);
        //the sub node's depth is 1 more then the parent's

        if (pQueue->head == NULL)
        {
            return NULL;
        }


        QNode *qNode = pQueue->head;

        pQueue->head = pQueue->head->next;


        int child_depth = qNode->depth + 1;



        if (qNode->depth > current_depth)
        {
            current_depth = qNode->depth;
            printf("\n\n");
        }
        // ***************0****************  pad_between = 31 ; pad_front = 15  (depth == 1)
        // *******0***************0********  pad_between = 15 ; pad_front = 7   (depth == 2)    
        // ***0*******0*******0*******0****  pad_between = 7  ; pad_front = 3   (depth == 3)
        // *0***0***0***0***0***0***0***0**  pad_between = 3  ; pad_front = 1   (depth == 4)
        // 0*0*0*0*0*0*0*0*0*0*0*0*0*0*0*0*  pad_between = 1  ; pad_front = 0;  (depth == 5)
        // Tree height = 5
        // pad_num = 1
        // padding between node = (1+2*pad_front)*pad_num = (1+ (1<<(height-depth))-1)*pad_num
        // (1<< (height - current_depth))-1=2^(height - current_depth)-1


        int pad_front = (1 << (height - current_depth)) - 1;

        if ((qNode->blank == 1))
        {
            //add the parent node's padding:2
            if (pad_front == 0)    printf("%*s%*s", pad_num, "o", pad_num, " ");
            else                  printf("%*s%*s%*s", pad_front*pad_num, " ", pad_num, "o", (1 + pad_front)*pad_num, " ");
            //pad_front*pad_num+(1+pad_front)*pad_num=(1+2*pad_front)*pad_num=padding between node
            if (child_depth <= height)
            {
                //enter two NULL sub-tree node.
                //every time you enter NULL TNode,there's corresponding blank QNode.
                enQueue(pQueue, NULL, child_depth);
                enQueue(pQueue, NULL, child_depth);
            }
        }
        else
        {
            if (pad_front == 0)   printf("%*d%*s", pad_num, qNode->t_node->value, pad_num, " ");
            else                 printf("%*s%*d%*s", pad_front*pad_num, " ", pad_num, qNode->t_node->value, (1 + pad_front)*pad_num, " ");
            if (child_depth <= height)
            {
                enQueue(pQueue, qNode->t_node->left, child_depth);
                enQueue(pQueue, qNode->t_node->right, child_depth);
            }
        }

    } //while end
    printf("\n-----------\nbreath end!\n-----------\n");
    return 0;
}
yuan sun
  • 13
  • 2
  • 2
    Welcome to Stack Overflow! It sounds like you may need to learn how to use a [debugger](https://en.wikipedia.org/wiki/Debugger) to step through your code. With a good debugger, you can execute your program line by line and see where it is deviating from what you expect. This is an essential tool if you are going to do any programming. Further reading: [How to debug small programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/). – Paul R Dec 24 '18 at 11:10
  • That value (`0xcdcdcdcd`) denotes uninitialized heap content. Since your nodes are allocated on the heap, it simply means you *probably* didn't initialize a member pointer of one of your structures after allocation, then later tried to dereference through said-same. – WhozCraig Dec 24 '18 at 11:19
  • Compile with all warnings and debug info: `gcc -Wall -Wextra -g` with [GCC](http://gcc.gnu.org/). Improve your code to get no warnings. Also use [valgrind](http://valgrind.org/) and perhaps [clang-analyzer](http://clang-analyzer.llvm.org/) – Basile Starynkevitch Dec 24 '18 at 12:02
  • 0xCD is memory you've allocated but never written to. [When and why will an OS initialise memory to 0xCD, 0xDD, etc. on malloc/free/new/delete?](https://stackoverflow.com/q/370195/995714) – phuclv Dec 24 '18 at 13:09

0 Answers0