Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Gap of two 0x00 between structs.....

Reply
Thread Tools

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

 
 
Don
Guest
Posts: n/a
 
      10-01-2003
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[i] = 0x55;

for(i=0;i<100;i++)
measuredata.struct1.measure2[i] = 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;

}


 
Reply With Quote
 
 
 
 
Thomas Matthews
Guest
Posts: n/a
 
      10-01-2003
Don wrote:
> 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

 
Reply With Quote
 
 
 
 
Jack Klein
Guest
Posts: n/a
 
      10-01-2003
On Wed, 1 Oct 2003 15:43:25 +0200, "Don" <(E-Mail Removed)> wrote in
comp.lang.c:

> 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
 
Reply With Quote
 
Derk Gwen
Guest
Posts: n/a
 
      10-01-2003
# 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.

--
Derk Gwen http://derkgwen.250free.com/html/index.html
Who's leading this mob?
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Calling two or more API with certain time gap Vallabha Java 3 10-07-2008 12:42 PM
Best way to create a string with gap (0x00's) in the middle? Earle Clubb Ruby 2 04-24-2007 04:12 PM
Gap in IE, no gap in FF chlori HTML 1 01-19-2006 09:30 PM
printf("%#04x\n", 0); print 0000 not 0x00 baumann@pan C Programming 8 07-04-2005 11:50 AM
printing 0x00 in ascii Joseph Suprenant C++ 3 11-20-2003 09:26 PM



Advertisments