Monday, April 13, 2015

C language from BaseBand Processor Eng perspective Part-3 PACKED AND UNPACKED STRUCTURES

The other thing which we see in modem codes is use of data structures. Now unlike in most C codes which we write in colleges. We do not use char , int , long so much as byte, word, UNIT8 , UNIT 16.

Simply put we don't have luxury of wasting Modem memory by using ints. Most of modem data type is in hexadecimal. So we define bytes, unsigned int , char , short .

Also the data structures are __packed type , so that there is no wastage of bits.

What the __packed data structure do, Its removes the padding and use padded bits for other type.

Example

typedef struct ex{

    byte a;
    short b;
    byte c;
} ;

The size of above structure is 4 bytes on a machine with address size of 1 byte (8 bits in memory) .
For address size of 2 bytes ( 16 bits ). The above data structure will take 6 bytes.  that is wastage of 2 bytes , since each byte will occuoy only 8 bits , the rest 8 bits will be padded.


But if we define the above structure as belows

typedef  __packed stuct ex2{

 byte a;
 short b;
 byte c;
};

It will occupy 4 bytes in 16 bit machine also.

Also after discussing with my colleagues, what we have concluded is for FOR UNPACKED , how much size will be taken depends on machine and compiler implementation.

Only PACKED type gives correct answer.

Address size of maximum modem processor as of now is 32-bit  (ARM9).

Also the best way to typedef a structure is

typedef struct {
int a;
int b;
}struct_type;


This will work in both C and C++ and will avoid error if you compile your code sometime in C and sometime in C++.

The below syntax works in C++ , but not in C

typedef struct struct_type{
int a;
int b;
};



No comments:

Post a Comment