Gap of two 0x00 between structs.....

D

Don

Hi NG. I have the code below my question.
I would like to copy the content of measuredata to the MyBuffer from place
nr 20 (MyBuffer[19]). The problem with this code is that between the two
structs I get two empty bytes :-(. Like measuredata.struct1.measure13 are
placed at MyBuffer[19+786] and when I print this it displays oxBF fine, but
when I print
MyBuffer[19+787],MyBuffer[19+788],MyBuffer[19+789] and MyBuffer[19+790], it
displays 0x00, 0x00, 0x12 and 0x34 :-(. And MyBuffer[19+791] and
MyBuffer[19+792] holds the rest of the integer 0x56 and 0x78. So I get these
two 0x00 and 0x00.....Can I workaround this or is this something to do with
the struct type???

Best Regards
Don



unsigned char MyBuffer[2000];

struct mystruct1{
unsigned char measure1[256];
unsigned short measure2[256];
unsigned char measure3;
unsigned short measure4;
unsigned short measure5;
unsigned short measure6;
unsigned short measure7;
unsigned short measure8;
unsigned short measure9;
unsigned char measure10;
unsigned char measure11;
unsigned char measure12;
unsigned char measure13;
};

struct mystruct2{
unsigned int measure14;
unsigned int measure15;
unsigned short measure16;
unsigned short measure17;
unsigned short measure18;
unsigned short measure19;
unsigned short measure20;
unsigned short measure21;

unsigned int measure22;
unsigned int measure23;
unsigned int measure24;
unsigned int measure25;
unsigned int measure26;
unsigned int measure27;

unsigned int measure28;
unsigned int measure29;
unsigned int measure30;
unsigned int measure31;
};


struct measurement_data{
struct mystruct1 struct1;
struct mystruct2 struct2;
}measuredata;



int main(){
int i= 0;

for(i=0;i<200;i++)
measuredata.struct1.measure1 = 0x55;

for(i=0;i<100;i++)
measuredata.struct1.measure2 = 0xFF11;

measuredata.struct1.measure9 = 0x1133;

measuredata.struct2.measure16 = 0x12345678;
measuredata.struct2.measure23 = 0xFFFFFFFF;
measuredata.struct2.measure26 = 0x08080808;
measuredata.struct2.measure30 = 0x00000001;
measuredata.struct2.measure31 = 0xDEADBEEF;

//The transition between struct1 and struct2
measuredata.struct1.measure13 = 0xBF;
measuredata.struct2.measure14 = 0x12345678;

//Copy measuredata struct with variable values to the MyBuffer
memcpy(&MyBuffer[19], (unsigned char *) &measuredata, sizeof(struct
measurement_data));

return 0;

}
 
T

Thomas Matthews

Don said:
Hi NG. I have the code below my question.
I would like to copy the content of measuredata to the MyBuffer from place
nr 20 (MyBuffer[19]). The problem with this code is that between the two
structs I get two empty bytes :-(. Like measuredata.struct1.measure13 are
placed at MyBuffer[19+786] and when I print this it displays oxBF fine, but
when I print
MyBuffer[19+787],MyBuffer[19+788],MyBuffer[19+789] and MyBuffer[19+790], it
displays 0x00, 0x00, 0x12 and 0x34 :-(. And MyBuffer[19+791] and
MyBuffer[19+792] holds the rest of the integer 0x56 and 0x78. So I get these
two 0x00 and 0x00.....Can I workaround this or is this something to do with
the struct type???

Best Regards
Don



unsigned char MyBuffer[2000];

The compiler is allowed to add padding bytes between fields in a
structure and also at the end of the structure.

Some compilers offer a "packed" option which forces the compiler
remove the padding (I've had problems with this option on
different compilers). This option is NOT standard.

Another option is to use a pointer to an unsigned character
then cast to whatever type. In this manner, you can access
memory at the finest detail without messing with any padding
that _may_ be imposed by the compiler.

Just remember that the size of a structure may not be equal
to the size of its fields.

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book
 
J

Jack Klein

Hi NG. I have the code below my question.
I would like to copy the content of measuredata to the MyBuffer from place
nr 20 (MyBuffer[19]). The problem with this code is that between the two
structs I get two empty bytes :-(. Like measuredata.struct1.measure13 are
placed at MyBuffer[19+786] and when I print this it displays oxBF fine, but
when I print
MyBuffer[19+787],MyBuffer[19+788],MyBuffer[19+789] and MyBuffer[19+790], it
displays 0x00, 0x00, 0x12 and 0x34 :-(. And MyBuffer[19+791] and
MyBuffer[19+792] holds the rest of the integer 0x56 and 0x78. So I get these
two 0x00 and 0x00.....Can I workaround this or is this something to do with
the struct type???

Best Regards
Don

[code snipped]

http://www.eskimo.com/~scs/C-faq/q2.13.html

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq
 
D

Derk Gwen

# MyBuffer[19+787],MyBuffer[19+788],MyBuffer[19+789] and MyBuffer[19+790], it
# displays 0x00, 0x00, 0x12 and 0x34 :-(. And MyBuffer[19+791] and
# MyBuffer[19+792] holds the rest of the integer 0x56 and 0x78. So I get these
# two 0x00 and 0x00.....Can I workaround this or is this something to do with
# the struct type???

C structs cannot be portably used to stencil a structure on a byte stream. To be
portable you need to move bytes to and from a struct yourself. Nonportably your
compiler may have pragmas or other means to force byte alignment; consult your
compiler documentation if you want to go this route.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top