Thread: Rpc linux c, dynamic array

  1. #1
    Registered User ch4's Avatar
    Join Date
    Jan 2007
    Posts
    154

    Rpc linux c, dynamic array

    Hello,

    I'm writing an RPC C program in LInux and i want to pass dynamically allocated arrays, but only the first element of the arrays is reachable at the server side.

    Code:
    struct data {
    	int size;
    	int *X;
    	int *Y;
    	float r;
    };
    
    struct myarray {
        float pin<2>;
    };
    
    struct dianisma {
    	int size;
    	float *X;
    };
    
    program ASKISI_PROG {
    		version ASKISI_VERS {
    			int ESOTERIKO_GINOMENO(data) = 1;
    			myarray MESI_TIMI(data) = 2;
    			dianisma GINOMENO(data) = 3;
    	    } = 1;
    } = 0x23451111;
    Let's focus on ESOTERIKO_GINOMENO function. Two dynamically allocated arrays should be passed to the server, but only the first element of both arrays is passed.

    xdr file:
    Code:
    bool_t
    xdr_data (XDR *xdrs, data *objp)
    {
    	register int32_t *buf;
    
    	 if (!xdr_int (xdrs, &objp->size))
    		 return FALSE;
    	 if (!xdr_pointer (xdrs, (char **)&objp->X, sizeof (int), (xdrproc_t) xdr_int))
    		 return FALSE;
    	 if (!xdr_pointer (xdrs, (char **)&objp->Y, sizeof (int), (xdrproc_t) xdr_int))
    		 return FALSE;
    	 if (!xdr_float (xdrs, &objp->r))
    		 return FALSE;
    	return TRUE;
    }
    
    bool_t
    xdr_myarray (XDR *xdrs, myarray *objp)
    {
    	register int32_t *buf;
    
    	 if (!xdr_array (xdrs, (char **)&objp->pin.pin_val, (u_int *) &objp->pin.pin_len, 2,
    		sizeof (float), (xdrproc_t) xdr_float))
    		 return FALSE;
    	return TRUE;
    }
    
    bool_t
    xdr_dianisma (XDR *xdrs, dianisma *objp)
    {
    	register int32_t *buf;
    
    	 if (!xdr_int (xdrs, &objp->size))
    		 return FALSE;
    	 if (!xdr_pointer (xdrs, (char **)&objp->X, sizeof (float), (xdrproc_t) xdr_float))
    		 return FALSE;
    	return TRUE;
    }
    client usage snippet:
    Code:
        scanf("%d", &size);
        data_1_arg.X= (int *) malloc(size*sizeof(int));
        data_1_arg.Y= (int *) malloc(size*sizeof(int));
    
    
        // .... 
        
        result_1 = esoteriko_ginomeno_1(&data_1_arg, clnt);
    Server Side:
    Code:
    int * esoteriko_ginomeno_1_svc(data *argp, struct svc_req *rqstp)
    {
        int i;
    	static int result;
    
    	result = 0;
    
        for(i=0; i<argp->size; i++) {
            printf("%d ", argp->X[i]);
        }
        puts("");
        for(i=0; i<argp->size; i++) {
            printf("%d ", argp->Y[i]);
        }
        puts("");
    
        for(i=0; i<argp->size; i++) {
            
            result = result + ( argp->X[i] * argp->Y[i] );
        }
    
    	return &result;
    }
    Any idea ?
    Thanks in advanced.

  2. #2
    Registered User
    Join Date
    Dec 2017
    Posts
    1,633
    You are telling it that there is only one element in your arrays by passing sizeof (int).
    Instead you need to pass the number of elements multiplied by the size of the elements.
    For example if your arrays have 10 elements, 10 * sizeof (int).
    A little inaccuracy saves tons of explanation. - H.H. Munro

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Perhaps you want xdr_array.

    xdr_pointer(3) — Arch manual pages
    bool_t xdr_array(XDR *xdrs, char **arrp, unsigned int *sizep,
    unsigned int maxsize, unsigned int elsize,
    xdrproc_t elproc);

    A filter primitive that translates between variable-length arrays and their corresponding external representations. The argument arrp is the address of the pointer to the array, while sizep is the address of the element count of the array; this element count cannot exceed maxsize. The argument elsize is the sizeof each of the array's elements, and elproc is an XDR filter that translates between the array elements' C form, and their external representation. This routine returns one if it succeeds, zero otherwise.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User ch4's Avatar
    Join Date
    Jan 2007
    Posts
    154
    Do you mean i have to declare X Y in a different way like the above ?

    Code:
    struct data {
        int size;
        int X<>;
        int Y<>;
        float r;
    };

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dynamic 2D Array.
    By TheGreekMan2000 in forum C Programming
    Replies: 11
    Last Post: 04-30-2020, 03:58 AM
  2. why can't a static array copy values from a dynamic array
    By c++noob145 in forum C++ Programming
    Replies: 2
    Last Post: 03-13-2013, 10:25 AM
  3. Replies: 10
    Last Post: 12-03-2011, 02:26 PM
  4. Copy array into a Dynamic array
    By pantherman34 in forum C Programming
    Replies: 15
    Last Post: 05-01-2010, 10:58 PM
  5. Dynamic structure with array and array count
    By Nazgulled in forum C Programming
    Replies: 14
    Last Post: 06-08-2007, 10:10 PM

Tags for this Thread