473,769 Members | 6,697 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

sizeof and structure

Hello,

I already asked about my problem and received several valuable advices which
I have followed to. Anyway I should explain briefly.
In the program I'm working on I receive a data stream, having structure as
follows:

| common_header | I2C_header | data ... |
My purpose is to parse the 'common_header' , and read the data following it.

This is my code snippet:

#define MSG_SIZE 1000

enum ctrl_cmds {
CMD_ECHO = 0x0,
CMD_I2C_WRITE,
CMD_I2C_READ,
CMD_I2C_BURST_W RITE
};

struct __attribute__ ((packed)) USB_iobuf_commo n_header_s {
unsigned char function;
unsigned char size_lo;
unsigned char size_hi;
};

typedef struct USB_iobuf_commo n_header_s USB_iobuf_commo n_header;

struct __attribute__ ((packed)) USB_iobuf_i2c_h eader_s {
unsigned char addr;
unsigned char subaddr_lo;
unsigned char subaddr_hi;
unsigned char datasize_lo;
unsigned char datasize_hi;
};

typedef struct USB_iobuf_i2c_h eader_s USB_iobuf_i2c_h eader;

enum usb_offset {
usb_idx_functio n = 0,
usb_idx_size = usb_idx_functio n + sizeof(unsigned char),
usb_idx_i2c_add r = usb_idx_size + sizeof(unsigned char),
usb_idx_i2c_sub addr = usb_idx_i2c_add r + sizeof(unsigned char),
usb_idx_i2c_dat asize = usb_idx_i2c_sub addr + sizeof(unsigned char),
usb_idx_i2c_dat a = usb_idx_i2c_dat asize + sizeof(unsigned char)
};

char data[MSG_SIZE];
char reg;
....

CDC_Read(..., data, MSG_SIZE);

USB_iobuf_commo n_header *hdr = (USB_iobuf_comm on_header *)data;
switch (hdr->function) {
case CMD_I2C_READ:
AT91F_TWI_ReadS ingleIadr(AT91C _BASE_TWI, (usb_idx_i2c_ad dr << 16),
data[4], AT91C_TWI_IADRS Z_1_BYTE, &reg);
...
break;
...
}

At this point I expect to have 'usb_idx_i2c_ad dr' equal to 'data[3]', but
it's always 0, though the other end is sending 0xC0. If I use 'data[3]' or
'data[4]' - everything works perfectly, but this is ugly hack.
I'm stuck. What's the problem with that?

The part of problem problem was with alignment of structure, I found out how
to disable padding and I also added the offsets (via 'enum') to be more
readable.

Thanks.
With best regards, Roman Mashak. E-mail: mr*@tusur.ru
Jan 9 '08 #1
8 1609

"Roman Mashak" <mr*@tusur.ruwr ote in message
news:fm******** ***@relay.tomsk .ru...
enum usb_offset {
usb_idx_functio n = 0,
usb_idx_size = usb_idx_functio n + sizeof(unsigned char),
usb_idx_i2c_add r = usb_idx_size + sizeof(unsigned char),
usb_idx_i2c_sub addr = usb_idx_i2c_add r + sizeof(unsigned char),
usb_idx_i2c_dat asize = usb_idx_i2c_sub addr + sizeof(unsigned char),
usb_idx_i2c_dat a = usb_idx_i2c_dat asize + sizeof(unsigned char)
};
break;
...
}

At this point I expect to have 'usb_idx_i2c_ad dr' equal to 'data[3]', but
'usb_idx_i2c_ad dr' will be equal to 3 (the index) , not data[3]. enumeration
values _are_ the offsets into
the data[] array.
Jan 9 '08 #2
"Ravishanka r S" <ra***********@ in.bosch.comwro te in message
news:fm******** **@news4.fe.int ernet.bosch.com ...
>
"Roman Mashak" <mr*@tusur.ruwr ote in message
news:fm******** ***@relay.tomsk .ru...
enum usb_offset {
usb_idx_functio n = 0,
usb_idx_size = usb_idx_functio n + sizeof(unsigned char),
usb_idx_i2c_add r = usb_idx_size + sizeof(unsigned char),
usb_idx_i2c_sub addr = usb_idx_i2c_add r + sizeof(unsigned char),
usb_idx_i2c_dat asize = usb_idx_i2c_sub addr + sizeof(unsigned char),
usb_idx_i2c_dat a = usb_idx_i2c_dat asize + sizeof(unsigned char)
};
break;
...
}

At this point I expect to have 'usb_idx_i2c_ad dr' equal to 'data[3]',
but
values _are_ the offsets into
the data[] array.
Infact 'usb_idx_i2c_ad dr' is not 3 , but 2.
usb_idx_functio n = 0;
usb_idx_size = usb_idx_functio n + sizeof(unsigned char) = 0 + 1 = 1
usb_idx_i2c_add r = usb_idx_size + sizeof(unsigned char) = 1 + 1 = 2


Jan 9 '08 #3
Can I ask a silly question?
Why are you using artificially manufactured structs to parse a byte
array?
Is it to make access to the byte array's sections more readable?
Why not just use macros?

I am a newbie so I am just curious to know.
Jan 9 '08 #4
"fnegroni" <f.*********@go oglemail.comwro te in message
news:9d******** *************** ***********@l6g 2000prm.googleg roups.com...
Can I ask a silly question?
Why are you using artificially manufactured structs to parse a byte
array?
Is it to make access to the byte array's sections more readable?
Why not just use macros?

I am a newbie so I am just curious to know.
I suppose its for readability only. comprehension is better when structs are
used.
Jan 9 '08 #5
"Ravishanka r S" <ra***********@ in.bosch.comwri tes:
"fnegroni" <f.*********@go oglemail.comwro te in message
news:9d******** *************** ***********@l6g 2000prm.googleg roups.com...
>Can I ask a silly question?
Why are you using artificially manufactured structs to parse a byte
array?
Is it to make access to the byte array's sections more readable?
Why not just use macros?

I am a newbie so I am just curious to know.
I suppose its for readability only. comprehension is better when structs are
used.
As someone who suggested using offsets + access functions I have to
say that it depends on how well the access functions are written. The
code can be made very readable but, more importantly, can be made more
portable (note that some byte fields are really parts of larger
types -- size_lo size_hi).

What is odd is to see both approaches "half" used.

--
Ben.
Jan 9 '08 #6
On Jan 9, 9:36*pm, "Roman Mashak" <m...@tusur.ruw rote:
Hello,

I already asked about my problem and received several valuable advices which
I have followed to. Anyway I should explain briefly.
In the program I'm working on I receive a data stream, having structure as
follows:

* * * | *common_header | I2C_header | data ... |

My purpose is to parse the 'common_header' , and read the data following it..

This is my code snippet:

#define MSG_SIZE 1000

enum ctrl_cmds {
* * CMD_ECHO = 0x0,
* * CMD_I2C_WRITE,
* * CMD_I2C_READ,
* * CMD_I2C_BURST_W RITE

};

struct __attribute__ ((packed)) USB_iobuf_commo n_header_s {
* * unsigned char function;
* * unsigned char size_lo;
* * unsigned char size_hi;

};

typedef struct USB_iobuf_commo n_header_s USB_iobuf_commo n_header;

struct __attribute__ ((packed)) USB_iobuf_i2c_h eader_s {
* * unsigned char addr;
* * unsigned char subaddr_lo;
* * unsigned char subaddr_hi;
* * unsigned char datasize_lo;
* * unsigned char datasize_hi;

};

typedef struct USB_iobuf_i2c_h eader_s USB_iobuf_i2c_h eader;

enum usb_offset {
* * *usb_idx_functi on = 0,
* * *usb_idx_size *= usb_idx_functio n *+ sizeof(unsigned char),
* * *usb_idx_i2c_ad dr = usb_idx_size * + sizeof(unsigned char),
* * *usb_idx_i2c_su baddr = usb_idx_i2c_add r *+ sizeof(unsigned char),
* * *usb_idx_i2c_da tasize = usb_idx_i2c_sub addr + sizeof(unsigned char),
* * *usb_idx_i2c_da ta * * = usb_idx_i2c_dat asize + sizeof(unsigned char)

};

char data[MSG_SIZE];
char reg;
...

CDC_Read(..., data, MSG_SIZE);

USB_iobuf_commo n_header *hdr = (USB_iobuf_comm on_header *)data;
switch (hdr->function) {
* * case CMD_I2C_READ:
* * * * AT91F_TWI_ReadS ingleIadr(AT91C _BASE_TWI, (usb_idx_i2c_ad dr<< 16),
data[4], AT91C_TWI_IADRS Z_1_BYTE, &reg);
* * * * *...
* * * * break;
* * * * ...

}

At this point I expect to have 'usb_idx_i2c_ad dr' equal to 'data[3]', but
it's always 0, though the other end is sending 0xC0. If I use 'data[3]' or
'data[4]' - everything works perfectly, but this is ugly hack.
I'm stuck. What's the problem with that?

The part of problem problem was with alignment of structure, I found out how
to disable padding and I also added the offsets (via 'enum') to be more
readable.

Thanks.

With best regards, Roman Mashak. *E-mail: m...@tusur.ru
Roman,

A better approach would be for you to create instances of the
structure and populate them as you read the stream.
Since u have declared the structs are 'packed' they are contigeous and
hence no padding of any sort. So in your case if you want to read the
common header

USB_iobuf_i2c_h eader header;

read(filehandle , &header, sizeof(USB_iobu f_i2c_header));

At this point the file pointer will be pointing to I2C_header and you
could use the same approach to populate the appropriate buffers.

This may help you alleviate some issues.

Suresh Shenoy
Jan 9 '08 #7
"Roman Mashak" <mr*@tusur.ruwr ites:
Hello, Ben!
You wrote on Wed, 09 Jan 2008 11:35:25 +0000:

??|> common_header | I2C_header | data ... |

BBJust to be clear... you call it a "common header". Do you sometimes
BBneed to process messages like this:

??| common_header | data ... |
BBor
??| common_header | some_other_head er | data ... |
BB?

Probably never. The format of message is defined and fixed.
Then you could consider putting both headers in one struct and you
will (most likely) not have to do any system-specif packing magic --
the header will consist of 8 chars.

If, for some reason you all need indexes into a char array, you can
use the offsetof macro to get them.

<snip>
Making indexes readable is suffice, then the offsets really should look like
this:

enum usb_offset {
usb_idx_functio n = 0,
usb_idx_size = usb_idx_functio n + sizeof(unsigned char),
usb_idx_i2c_add r = usb_idx_size + sizeof(unsigned short),
usb_idx_i2c_sub addr = usb_idx_i2c_add r + sizeof(unsigned char),
usb_idx_i2c_dat asize = usb_idx_i2c_sub addr + sizeof(unsigned short),
usb_idx_i2c_dat a = usb_idx_i2c_dat asize + sizeof(unsigned short)
};

Am I correct?
Yes, but you might have to check short is the size you want. That is
why I suggested uint16_t.

Having indexes without access functions seems pointless to me. If you
want to have both the struct and the index enum, why not use offsetof?
It would be more readable than my method.

--
Ben.
Jan 10 '08 #8
Hello, Ben!
You wrote on Thu, 10 Jan 2008 11:42:10 +0000:

[skip]
BBThen you could consider putting both headers in one struct and you will
BB(most likely) not have to do any system-specif packing magic -- the
BBheader will consist of 8 chars.

BBYes, but you might have to check short is the size you want. That is
BBwhy I suggested uint16_t.

BBHaving indexes without access functions seems pointless to me. If you
BBwant to have both the struct and the index enum, why not use offsetof?
BBIt would be more readable than my method.

Oh, great idea. Why didn't I consider 'offsetof' macro.
Thanks!

With best regards, Roman Mashak. E-mail: mr*@tusur.ru
Jan 11 '08 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
2206
by: Christof Warlich | last post by:
Hi, I'm trying to build an _efficient_, _purely_ _abstract_ API for inter process(or) communication, _completely_ hiding any implementation details. The core components are "Buffer", "Address" and "Process". They may be instantiated by a "Factory". Here is a simplified, but compilable version: // Abstract Interface enum OsSelector {
19
9237
by: Martin Pohlack | last post by:
Hi, I have a funtion which shall compute the amount for a later malloc. In this function I need the sizes of some struct members without having an instance or pointer of the struct. As "sizeof(int)" is legal I assumed "sizeof(struct x.y)" to be legal too. But is is not: #include <dirent.h>
7
7202
by: Zero | last post by:
If we have a structure like: struct something{ int *a; int b; }; We allocate mempry for a using malloc or calloc. The question is when we want to know the size of the structure, sizeof(struct something), it will not give us the correct number (i.e. the size of the structure
10
2433
by: Sean | last post by:
I have a struct that I wrote to test a protocol. The idea I had was to just declare the elements of the struct in the order in which they are sent and received as defined by the protocol. However, writing this struct to a file produces unexpected results. Here is a test struct I wrote: struct Tester { unsigned short first; unsigned int second;
18
12962
by: Mockey Chen | last post by:
My friend ask me a question as following: give a union SU define as: typedef union _SU { short x; struct y{ char a; short b; char c;
17
2191
by: Jack | last post by:
For the structure below: struct A{ int a; double b; char c; }; struct A sa; printf("sizeof(A): %d\n", sizeof(sa));
9
6083
by: CptDondo | last post by:
I am missing something about structure declarations.... I am trying to get the size of a structure member using sizeof. my xml.h file (beware of line wrap): struct fieldSchedule_t { uint8_t action; uint16_t fromBearing, toBearing; };
8
4247
by: Chameleon | last post by:
I have a TGA image header struct. TGA has 18 bytes header, so the C struct too. why this return 20? sizeof(TGAHeader) I saw this in many structs. I believe compiler round up the size to 4 multiple.
4
2161
by: Sebastian Fahr | last post by:
Hey there I have a problem with my own Objects. I have to write a programm in school that read objects from a binary file. This Object has the following structure: #pragma once class Sandbox
0
9589
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10222
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10050
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9866
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8876
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7413
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6675
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5448
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3570
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.