Why Garbage value is getting printed???

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Siddarth777
    New Member
    • Sep 2010
    • 28

    #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
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    At line 8 of incapp_svr.c you return a pointer to a local variable, that is a variable that is destroyed as soon as the function exits.

    Comment

    • Siddarth777
      New Member
      • Sep 2010
      • 28

      #3
      Dear Banfa, Thanks a lot for replying,

      why is my PRINT STATEMENT not getting printed then, and can you please suggest me the solution for the problem?

      Siddarth777

      Comment

      • Banfa
        Recognized Expert Expert
        • Feb 2006
        • 9067

        #4
        Your title says it is getting printed, which is it? Not appearing for appearing with a garbage value?

        Comment

        • Siddarth777
          New Member
          • Sep 2010
          • 28

          #5
          Sorry for not making this clear, actually i was referring to the code in incapp_svr.c the first print statement in the function, which is not getting printed

          Comment

          • Markus
            Recognized Expert Expert
            • Jun 2007
            • 6092

            #6
            Stdout is buffered: it will flush the buffer when encountering a newline: add a newline "\n".

            Comment

            Working...