Thread: structure members showing different values in different places in the program

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    68

    structure members showing different values in different places in the program

    Hi all,


    I have a structure defined in the following way:

    Code:
    enum F_STATUS
    {
        NOT_DONE = 0,
        DONE
    };
    typedef enum F_STATUS fstatus_t;
    
    enum SLEVEL_T
    {
        L_ROOT = 0,
        L_INTERMEDIATE,
        L_LEAF,
    };
    typedef enum SLEVEL_T slevel_t;
    
    struct SERVER
    {
        uint16_t s_id;                   
        uint16_t s_period;		
        uint16_t s_budget;		
        uint16_t s_capacity;		
        uint8_t s_up_link;		
        uint8_t s_down_link;		
        hsf_bool_t s_overrun;	
        fstatus_t s_done;		
        slevel_t s_lvl;             
    };
    typedef struct SERVER server_t;
    I have a function that instantiates an object of the type server_t, and assigns certain values to the members of the structure.

    Code:
    void prepare_the_fixed_server(server_t** svp)
    {
        // prepare the server
        server_t sv = {0};
        
        int packet_size_us;
        int range_low, range_high;
        signed char ret;
       
        packet_size_us = PACKET_SIZE_US_FULL;
        range_low = 5*packet_size_us;
        range_high = 7*packet_size_us;
    
        sv.s_id = 7; 
        sv.s_period = 8;
        sv.s_budget = APPLICATION_RANDOM(range_low, range_high);
        sv.s_capacity = sv.s_budget;
        sv.s_up_link = 2;
        sv.s_down_link = 5;
        sv.s_overrun = HSF_FALSE;
        sv.s_done = NOT_DONE;
        sv.s_lvl = L_LEAF;
        
        *svp = &sv;
        
        printf("(prepare_the_fixed_server) I am still inside the function .............. \n");
        
        SV_printParam(*svp);
            
    }
    In the above code, the routine SV_printParam(*svp) is responsible for printing the values of the SERVER object pointed to by *svp.

    Inside this function, all the prints are correct. This is an output from SV_printParam(*svp) when inside the function "prepare_the_fixed_server".

    ************************************
    * id: 7 *
    * period: 8 *
    * budget: 498 *
    * capacity: 498 *
    * finish status: NOT_DONE *
    * level: LEAF SERVER *
    * src: 2 *
    * dst: 5 *
    ************************************

    However, the function "prepare_the_fixed_server" has to be called from a different location, in a different file. In that file, I do the following:

    Code:
    server_t* svp = NULL;
    prepare_the_fixed_server(&svp);
     SV_printParam(svp);
    And, this is the output from "SV_printParam(svp)" that I receive.

    ************************************
    * id: 7 *
    * period: 8 *
    * budget: 498 *
    * capacity: 52395 *
    * finish status: *
    * level: *
    * src: 86 *
    * dst: 183 *
    ************************************

    As we can see, except for the first three values, all others are changed/wrong.
    can someone please help, what could the cause of this problem.


    Thanks,

    Zahid

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,184
    Why are you passing a pointer to a pointer of your structure into the prepare_the_fixed_server() function?

    Do you realize that zv a local variable and that you can't "return" a pointer to that local variable because the variable is only valid within that function?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 12
    Last Post: 03-27-2017, 02:06 AM
  2. How to find number of structure members in a given structure?
    By bhaskarReddy in forum C Programming
    Replies: 4
    Last Post: 01-16-2012, 05:37 AM
  3. VC++ IntelliSense (showing class members)
    By _Elixia_ in forum Tech Board
    Replies: 0
    Last Post: 08-02-2003, 01:14 PM
  4. structure not showing any value
    By sainideepak in forum Linux Programming
    Replies: 2
    Last Post: 02-28-2003, 11:25 PM
  5. Replies: 10
    Last Post: 06-12-2002, 03:15 PM

Tags for this Thread