Thread: Why Garbage value is getting printed???

  1. #1
    Registered User
    Join Date
    Aug 2012
    Posts
    1

    Why Garbage value is getting printed???

    I have written a simple RPC program for a simple increment operation on an integer using RPCGEN.

    Code is getting compiled fine without any errors, when i call the client after starting the server, the method which i wrote for server is not getting called even after writing a small print statement to check whether the remote method is being called or not, i have no idea where i am going wrong

    Here are the codes which i wrote

    incapp_clnt.c

    Code:
     
    int main(int argc,char* argv[])
     {
     
         CLIENT *cl;
         int a= 9;
         int *num,*res;
         num = &a;
     
         if (argc != 2) {
             printf("Usage: firstmain hostname\n");
             exit(1);
         }
     
         cl = clnt_create(argv[1], ADDAPP, ADDAPP_V1,"tcp");
     
         if (cl == NULL) 
         {
                 printf("error: could not connect to server.\n");
                 return 1;
             }
     
         printf("The value of num is:%d\n",*num);
         res = add_1(num,cl);
     
         printf("Back from firsttry\n");
         if (res == NULL) {
             clnt_perror(cl,argv[1]);
             exit(1);
         }
     
         printf("After addition=%d\n",*res);
     
         return 0;
     }
    incapp_svr.c

    Code:
       
    
    int* add_1_svc(int* num, struct svc_req* cli)
     {
         printf("The server is getting called"); //For checking whether server is being called or not
         int b;
         int *res;
         b = *num++;
         res = &b;
         return res;
     }

    Please tell me where i am going wrong
    Thanks a lot in advance

    Siddarth777

  2. #2
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    You're returning the address of an local variable from add_1_svc.

    Presumably *num++ is enough to increment num in the caller.

    For a quick test, try changing your final printf to:
    Code:
    printf("After addition=%d\n",*num);
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  3. #3
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Why all the pointless layers here? You could do this I guess.

    Code:
    int add_1_svc(int* num, struct svc_req* cli)
     {
         printf("The server is getting called"); //For checking whether server is being called or not
         return *num += 1;
     }
    What you were doing in this function before is undefined behavior: b was a local variable, a copy of the num pointee. You cannot return a pointer to the local variable. After the function ends the local variable gets destroyed and any pointers to it will dangle. My guess was that showed up as a garbage variable in the program.

  4. #4
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Quote Originally Posted by oogabooga View Post
    You're returning the address of an local variable from add_1_svc.

    Presumably *num++ is enough to increment num in the caller.

    For a quick test, try changing your final printf to:
    Code:
    printf("After addition=%d\n",*num);
    Oops.
    *num++ is NOT going to increment num in the caller. It just increments the POINTER num.
    It should be ++*num.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting a bmp copy of what i printed
    By knutso in forum Windows Programming
    Replies: 6
    Last Post: 04-16-2007, 05:29 AM
  2. int wrong value being printed?
    By Axel in forum C Programming
    Replies: 11
    Last Post: 10-14-2006, 01:37 AM
  3. Funny characters being printed
    By learninC in forum C Programming
    Replies: 8
    Last Post: 03-17-2005, 09:02 AM
  4. Getting number of pages printed
    By Yasir_Malik in forum Linux Programming
    Replies: 2
    Last Post: 09-21-2004, 02:59 PM
  5. Problem when data is printed.
    By Fuzzyman81 in forum C Programming
    Replies: 4
    Last Post: 11-06-2001, 03:24 PM