maping an unsigned char ofer a structure

This is a discussion on maping an unsigned char ofer a structure within the C Programming forums, part of the General Programming Boards category; My problem is the following. I have an unsigned char array packet[] and the following structures: Code: typedef struct set_serv_prop_s ...

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    167

    maping an unsigned char ofer a structure

    My problem is the following. I have an unsigned char array packet[] and the following structures:

    Code:
    typedef struct set_serv_prop_s
    {
    
    	byte serviceType;
    	union
    	{
    		heartbeatCommand_t	*heartbeatCommand;
    		announcement_t		*announcement;
    	} set_serv_propType;
    
    } set_serv_prop_t;
    
    typedef struct announcement_s
    {
    
    	union
    	{
    		announAllocCommand_t	*announAllocCommand;
    		announAllocResponse_t	*announAllocResponse;
    		announDownloadCommand_t	*announDownloadCommand;
    		announRemCommand_t		*announRemCommand;
    		announRemResponse_t		*announRemResponse;
    		announMarkRemCommand_t	*announMarkRemCommand;
    		announDownAbortCommand_t *announDownAbortCommand;
    	} announcementType;
    
    } announcement_t;
    
    typedef struct announAllocCommand_s
    {
    
    	byte	encodingLaw;
    	uint16	reserved;
    	uint32	announcementSize;
    
    } announAllocCommand_t;
    the service type field tells what king of package it is.

    i try to map it:
    Code:
    void read_set_serv_prop_from_pkt(set_serv_prop_t **head,
    								unsigned char* packet)
    {
    	int i;
    
    	*head = (set_serv_prop_t*) packet.payload;
    	
    	for(i=0;i<packet.size;i++)
    		printf("&#37;x ",packet.payload[i]);
    	printf("\n");
    	
    	if((*head)->serviceType==0x05)
    	{
    		announAllocCommand_t *allocCommand = (*head)->set_serv_propType.announcement->announcementType.announAllocCommand;
    			
    			fprintf(OUTPUT, "\t%d\n",allocCommand->encodingLaw);
    			fprintf(OUTPUT, "\t%d\n",allocCommand->reserved);
    			fprintf(OUTPUT, "\t%d\n\n", allocCommand->announcementSize);
    	}
    }
    and i get the output:

    Code:
    5 0 0 0 0 0 fa 0
            49
            12846
            -2147446592
    5 - Service Type
    0 - Encoding law
    0 0 - Reserved
    0 0 fa 0 -Announcement size

    the output is totally wrong. What is the problem here? and how can I solve it ?

    Please help... I'm very pressed by time
    Last edited by spank; 11-08-2007 at 07:04 AM.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
    typedef struct announAllocCommand_s
    {
    
    	byte	encodingLaw;
    	uint16	reserved;
    	uint32	announcementSize;
    
    } announAllocCommand_t;
    This struct is probaboy ending up 8 bytes long. Is that what you expect?
    The reason is:
    Code:
    byte encodingLaw;  // :@0, 1 byte long. 
    uint16 reserved; //: @2, 2 bytes long - 1 byte alignment inserted before this. 
    uint32 announcementSize;  //: @4 - 4 bytes long.
    --
    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
    Dec 2005
    Posts
    167
    but i have the service type byte in the first structure. i want to map that char array on the structure

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You could probably do something like this:
    Code:
    typedef struct announAllocCommand_s
    {
            byte    serviceType;
    	byte	encodingLaw;
    	uint16	reserved;
    	uint32	announcementSize;
    
    } announAllocCommand_t;
    
    typedef struct set_serv_prop_s
    {
    
    	union
    	{
    	        byte serviceType;
    		heartbeatCommand_t	heartbeatCommand;
    		announcement_t		announcement;
    	} set_serv_propType;
    
    } set_serv_prop_t;
    Note that I've removed the star from the "announceMent" and "heartbeadCommand", as I believe this is what you want - I expect that you are NOT sending pointers across the comms channel.

    --
    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
    Dec 2005
    Posts
    167
    this last post makes sense to me. thank you very much. now i understand padding (or at least I think so )

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by spank View Post
    this last post makes sense to me. thank you very much. now i understand padding (or at least I think so )
    Most compilers have a way that you can turn padding off, but unfortunately, every compiler manufacturer have their own idea of HOW you do that.

    Padding is there to improve performance [and in some processors, bigger data items than bytes HAS to be aligned to certain boundaries, or the processor will throw an exception and abort the execution]. So if you have a single byte followed by a bigger type, e.g. a 32-bit integer, the 32-bit integer should be aligned to an even 4 byte (32-bit) address to make it efficent [and/or correct where relevant].

    --
    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
    Dec 2005
    Posts
    167
    I am working on CodeWarrior and I build the target for a SC100 CCS Simulator. I'm forced to do this because I want to avoid endianness problems (the pc i'm working on is a intel P4 and the package is in network order). so in order to work only on big endian I use this.

    Again thank you for helping me!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 12
    Last Post: 08-12-2008, 12:02 AM
  2. How do i un-SHA1 hash something..
    By willc0de4food in forum C Programming
    Replies: 4
    Last Post: 09-14-2005, 06:59 AM
  3. Strings are V important...
    By NANO in forum C++ Programming
    Replies: 15
    Last Post: 04-14-2002, 12:57 PM
  4. ANY BODY WILLING TO HELP ME WITH Microsoft Visual C++
    By BiG pImPiN fOoL in forum C++ Programming
    Replies: 12
    Last Post: 11-04-2001, 06:03 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21