Thread: Puffers, pointers and such

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    58

    Smile Buffers, pointers and such

    Hi

    I am trying to cast a byte buffer (that is contained within a struct) to a different struct variable:
    Code:
    typedef struct {
    	
    	uint8_t protocolVersion;
    	uint8_t messageCount;
    	uint8_t payload[9];
    
    } PACK Data_t;
    
    typedef struct {
    	
    	uint8_t EE_Header;
    	uint16_t DstShortAddress;
    	uint16_t FV_Version;
    	uint16_t ProtocolVersion;
    	uint8_t EE_Start;
    	uint8_t EE_End;
    
    } PACK PowerData_t;
    
    Data_t iData;
    
    PowerData_t* LPD = (PowerData_t*)iData->payload;
    The structs are being used in a comms system to allow the passing of structured packets back and forth between devices. The payload in the iData structure needs to be casted to the PowerData type.

    Now the above works fine and dandy, however after I have the local variable LPD with the correct data how do I save this data to a global variable for use after the function doing the converting (above) has finished and the memory for LPD has been re-allocated?

    Code:
    GlobalPowerData = *LPD; // does work but not as I need:
    What I am getting at the moment is that the data comes in fine, however by the time I come to use the global data variable the contents has been over written or altered as the pointer to the local variable (LPD) created no longer apply.

    Any help anyone can give would be greatly appreciated

    David
    Last edited by DavidDobson; 03-19-2009 at 03:53 AM. Reason: My terrible spelling

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    This sounds strange, your global variable should keep it's value no matter what you do with the pointers that was used to set the global value.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Feb 2008
    Posts
    58
    Hi

    thanks for the reply

    So after I do this:

    Code:
    GlobalPowerData = *LPD;
    and the function in which the LPD variable is defined, created and set finishes (thus removing the local LPD variable from memory) the data I have (in effect) coped to the GlobalPowerData variable should still persist despite the fact the pointer now points to an area in memory where LPD used to be?

    (Just trying to get my head around this)

    many thanks

    David

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You are copying the data that is STORED at LPD into the global variable, so yes, LPD and whatever it points to is no longer needed for the global variable to have a valid content.

    Note however that using global variables should be avoided. Is there no way you could pass a pointer to the struct that you need to fill in into this function?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Feb 2008
    Posts
    58
    Hi

    Sorry, not quite with you? I'm not passing global variables between functions, i'm passing pointers.

    David

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by DavidDobson View Post
    Hi

    Sorry, not quite with you? I'm not passing global variables between functions, i'm passing pointers.

    David
    What I mean is that instead of something like this:
    Code:
    int global;
    
    void func(void)
    {
       global = 42;
    }
    
    int main()
    {
        func();
        printf("global = %d\n", global);
    }
    you'd do:
    Code:
    void func(int *px)
    {
        *px = 42;
    }
    
    int main()
    {
        int local;
        func(&local);
        printf("local = %d\n", local);
    }
    With this concept, it is much easier to follow that "local" is being modified in func - with the global variable, there is no way to look at main and understand where global is being set [if it is being set at all - looking at main, you wouldn't know that either].

    Note that you have to use pointers to be able to modify a passed in variable.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Registered User
    Join Date
    Feb 2008
    Posts
    58
    oh right I see, ill try that, many thanks

    David

Popular pages Recent additions subscribe to a feed