 |
BorlandTalk.com Borland discussion newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
DJV Guest
|
Posted: Sat Dec 17, 2005 11:33 am Post subject: structures under c and byte arrays |
|
|
I have beed developing some routines to permit me to "pack" byte arrays with strings and integers on a desktop under VB 5.0. These are then transferred to and read into a structure in C++ on a handheld device. I notice when I download the array generated under C by the handheld device, there is a null that appears to be inserted between the strings and the first of the series of integers. Does C/C++ typically insert a null between a string and an integer in a byte array? This only happens at the transition from the strings to the integers I can't seem to find any basis for this in my C/C++ code (such as string sizes in the structure).
|
|
| Back to top |
|
 |
Alan Bellingham Guest
|
Posted: Sat Dec 17, 2005 2:27 pm Post subject: Re: structures under c and byte arrays |
|
|
"DJV" <ventruella (AT) hotmail (DOT) com> wrote:
| Quote: | I have beed developing some routines to permit me to "pack" byte arrays
|
Does this always occur, or perhaps 50% of the time? It might be an
alignment issue.
Alan Bellingham
--
Team Thai Kingdom
<url:http://www.borland.com/newsgroups/> Borland newsgroup descriptions
<url:http://www.borland.com/newsgroups/netiquette.html> netiquette
|
|
| Back to top |
|
 |
Bob Gonder Guest
|
Posted: Sat Dec 17, 2005 3:19 pm Post subject: Re: structures under c and byte arrays |
|
|
DJV wrote:
| Quote: | Does C/C++ typically insert a null between a string and an integer in a byte array?
|
It typically does what you tell it to do. ;-/
We would need to see your structure, and the code that fills it to
tell whether or not you are telling it to do so.
|
|
| Back to top |
|
 |
Ed Mulroy Guest
|
Posted: Sat Dec 17, 2005 3:27 pm Post subject: Re: structures under c and byte arrays |
|
|
Two items to check for:
A long shot but are you speaking of the string termination character? All
strings end in a null character, a ' '.
For performance reasons the elements in structures in C++ Builder are
aligned on 4 byte boundaries. This would result in unused space in the
structure, space that could have the value zero.
A pragma can be used on the definition of the structure to pack the elements
it contains on single byte boundaries. There are several ways to do this.
This specifies that the elements contained are aligned on single byte
boundaries:
#pragma pack(1)
struct SomeStruct
{
char abc[27];
int def;
----etc----
};
#pragma pack()
See the above in use in winappc.h, wincpic.h, windns.h
Some people prefer the version of that pragma that came later which causes
the state of all options to be saved when entering the structure and then
restored after. I find it difficult to understand the actual benefit of
this and dislike the extra typing but, if you prefer you could encase the
structure like this:
#pragma pack(push, 1)
struct SomeStruct
{
char abc[27];
int def;
----etc----
};
#pragma pack(pop)
See the above in use in winldap.h, stdio.h and stdlib.h
A command line option for adjusting alignment is -an where n is a digit, 1,
2, 4, 8, etc. Some use the pragma for specifying options in that way to
adjust alignment although usually also specify other options as well in the
line.
#pragma option push -a1
struct SomeStruct
{
char abc[27];
int def;
----etc----
};
#pragma option pop
See the above in use in winappc.h, windows.h and winerror.h
.. Ed
| Quote: | DJV wrote in message
news:43a4058d$1 (AT) newsgroups (DOT) borland.com...
I have beed developing some routines to permit me to "pack" byte
arrays with strings and integers on a desktop under VB 5.0. These
are then transferred to and read into a structure in C++ on a handheld
device. I notice when I download the array generated under C by the
handheld device, there is a null that appears to be inserted between
the strings and the first of the series of integers. Does C/C++ typically
insert a null between a string and an integer in a byte array? This
only happens at the transition from the strings to the integers I can't
seem to find any basis for this in my C/C++ code (such as string
sizes in the structure).
|
|
|
| Back to top |
|
 |
DJV Guest
|
Posted: Sat Dec 17, 2005 5:02 pm Post subject: Re: structures under c and byte arrays |
|
|
I have the structure being "automatically" filled with fixed data, so I haven't checked to see if whether the number of odd or even bytes in the sum of the strings has an effect, because the the strings in the structure are fixed length. For my purposes, this means that any string I insert will have to fit within the fixed length of the string.
Wouldn't I have to change the string lengths in the structure to make this appear?
Note: I did change one string length from 4 to 5 characters, and that's when the problem manifested itself in terms of the packing routine I was using no longer working to pack the integers in a manner that would work. Definitely seems to be something to consider. Thanks for the hint.
Thanks.
---------
Alan Bellingham <alanb (AT) episys (DOT) com> wrote:
| Quote: | "DJV" <ventruella (AT) hotmail (DOT) com> wrote:
I have beed developing some routines to permit me to "pack" byte arrays
Does this always occur, or perhaps 50% of the time? It might be an
alignment issue.
Alan Bellingham
--
Team Thai Kingdom
url:http://www.borland.com/newsgroups/> Borland newsgroup descriptions
url:http://www.borland.com/newsgroups/netiquette.html> netiquette
|
|
|
| Back to top |
|
 |
DJV Guest
|
Posted: Sat Dec 17, 2005 5:03 pm Post subject: Re: structures under c and byte arrays |
|
|
I will take this to mean that this is not the standard behavior induced by the C compiler in storing a structure.
Bob Gonder <notbg (AT) notmindspring (DOT) invalid> wrote:
| Quote: | DJV wrote:
Does C/C++ typically insert a null between a string and an integer in a byte array?
It typically does what you tell it to do. ;-/
We would need to see your structure, and the code that fills it to
tell whether or not you are telling it to do so.
|
|
|
| Back to top |
|
 |
DJV Guest
|
Posted: Sat Dec 17, 2005 5:15 pm Post subject: Re: structures under c and byte arrays |
|
|
Once again, I believe the members of this forum have provided just the insight that I need. The original response from Mr. Bellingham seems to concur with your perspective.
I did change the length of one string in the structure (under both VB and C), and that is when this problem manifested itself with the byte array I downloaded to the handheld from VB no longer being properly interpreted in terms of the integers that appear for each record. I must have caused the number of bytes to cease to represent a number evenly divisible by 4 (or whatever the suitable number is for the compiler I'm using for the handheld evice). In the case of this byte array, as downloaded, it doesn't return the null terminators for the strings in the byte array received. Just the bytes in each string available for input use.
It seems that your concept of using #pragma pack(#) might be useful in the C program I've written for the handheld to make sure that my packing algorithm doesn't have to be altered for every array I try to download from the handheld. I think the handheld compiler can take the #pragma statement.
Thank you very much, Mr. Mulroy.
-----------
"Ed Mulroy" <dont_email_me (AT) bitbuc (DOT) ket> wrote:
| Quote: | Two items to check for:
A long shot but are you speaking of the string termination character? All
strings end in a null character, a ' '.
For performance reasons the elements in structures in C++ Builder are
aligned on 4 byte boundaries. This would result in unused space in the
structure, space that could have the value zero.
A pragma can be used on the definition of the structure to pack the elements
it contains on single byte boundaries. There are several ways to do this.
This specifies that the elements contained are aligned on single byte
boundaries:
#pragma pack(1)
struct SomeStruct
{
char abc[27];
int def;
----etc----
};
#pragma pack()
See the above in use in winappc.h, wincpic.h, windns.h
Some people prefer the version of that pragma that came later which causes
the state of all options to be saved when entering the structure and then
restored after. I find it difficult to understand the actual benefit of
this and dislike the extra typing but, if you prefer you could encase the
structure like this:
#pragma pack(push, 1)
struct SomeStruct
{
char abc[27];
int def;
----etc----
};
#pragma pack(pop)
See the above in use in winldap.h, stdio.h and stdlib.h
A command line option for adjusting alignment is -an where n is a digit, 1,
2, 4, 8, etc. Some use the pragma for specifying options in that way to
adjust alignment although usually also specify other options as well in the
line.
#pragma option push -a1
struct SomeStruct
{
char abc[27];
int def;
----etc----
};
#pragma option pop
See the above in use in winappc.h, windows.h and winerror.h
. Ed
DJV wrote in message
news:43a4058d$1 (AT) newsgroups (DOT) borland.com...
I have beed developing some routines to permit me to "pack" byte
arrays with strings and integers on a desktop under VB 5.0. These
are then transferred to and read into a structure in C++ on a handheld
device. I notice when I download the array generated under C by the
handheld device, there is a null that appears to be inserted between
the strings and the first of the series of integers. Does C/C++ typically
insert a null between a string and an integer in a byte array? This
only happens at the transition from the strings to the integers I can't
seem to find any basis for this in my C/C++ code (such as string
sizes in the structure).
|
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|