Home > Net >  how does dereferencing a pointer return garbage value?
how does dereferencing a pointer return garbage value?

Time:10-22

I couldn't find anything similar , if any similar question is asked before feel free to close this. I have a structure defined as shown below

typedef  struct  linked_list{
       int * data;
       struct linked_list *llink;
       struct linked_list *rlink;
   } List;

and I have a function

List * getitem(List* first){
       int temp_data ;
       int * intPtr;
       List * ptr = (List *) calloc(1, sizeof(List));
       if ( ptr == NULL){
          printf("Memory allocation failed");
          exit(0);
      }
      ptr->data= (int*) calloc (1, sizeof(int));
      if ( ptr->data == NULL) {
          printf("Mem alloc failed");
          exit(0);
      }
  
  
      // for now only allow int type
      printf("Enter the data you need");
      scanf("%d", &temp_data);
      ptr->data = &temp_data;
      printf("%d\n",*(ptr->data) );
  
      if (first == NULL){
         ptr->data= &temp_data;
         ptr->llink = NULL;
         ptr->rlink = NULL;
         printf("pointer valu = %p \n", ptr);
          printf("check data = %p \n", ptr->data);
       printf("%d\n",*(ptr->data) );
  
         return ptr;
      } else{
          first->llink= ptr;
          ptr->rlink=first;
          ptr->llink = NULL;
          ptr->data = &temp_data;
          printf("pointer valu = %p \n", ptr);
          printf("check data = %p \n", ptr->data);
  
          return ptr;
      }
  }

I pass a pointer to function ** getitem(List* first) ** assign some values return the same. But when i dereference it inside main it prints some garbage value. I am unable to pass the same pointer to the other function ** display_items(List* first); ** and use it.I might be missing something simple, any help would be greatly appreciated.

       int choice;
       List * First= NULL;
      for(;;){
          printf("1: to enter");
          printf("2: to display");
          scanf("%d", &choice);
          switch(choice){
              case 1:
                 First =  getitem( First);
                 printf("Checking if first actually has someting\n");
                 printf("%d\n",*(First->data));
                  break;
              case 2:
                  display_items(First);
                  break;
              default:
                  exit(0);
          }
  
      }
 
  }

CodePudding user response:

 ptr->data= (int*) calloc (1, sizeof(int));
 if ( ptr->data == NULL) {
     printf("Mem alloc failed");
     exit(0);
 }


 // for now only allow int type
 printf("Enter the data you need");
 scanf("%d", &temp_data);
 ptr->data = &temp_data;

You allocate memory and store a pointer to it in ptr->data, but then you leak that memory and change ptr->data to point to temp_data. So after the function returns, ptr->data points to an object that no longer exists -- it's a garbage pointer. So it's not surprising it points to a garbage value.

Instead of ptr->data = &temp_data;, you meant, *ptr->data = temp_data;.

Really, you should have just read the data where you wanted it in the first place using,
scanf("%d", ptr->data);.

  • Related