Hello Nathan,

Depends on how you access them. Most RISC CPU's can't do unaligned access
to multi-byte values like int's and long, they'll segfault. Intel
CPU's don't have this problem. If you memcpy the values into place,
this is a non-issue. You see it alot with embedded CPU's.

Without knowing his environment, there's no way to guess how this
would shake out. It'll work as long as the structs don't have pointers
in them. That means anything other than naked structs won't work with
the blob technique (no strings or C++ classes). You know, there's a
whole process that I believe Microsoft calls marshalling which is used
to take structs of all sorts and send them over a wire and
re-constitute them on the other side. It's fairly complicated.

My guess is he wants to store two ints and we're getting way too
complicated but, that's just a guess.

C

Thursday, December 8, 2005, 12:52:45 AM, you wrote:

NK> On Wed, Dec 07, 2005 at 08:34:46PM -0800, Dan Kennedy wrote:
>> > To make it work in more than a superficial manner, you probably will
>> > need a good understanding of how structures are internally represented
>> > in C++ or C.  You pass sqlite a pointer to the struct and tell it how
>> > long it is (using sizeof()).  When you get the blob back, you then
>> > treat the data as an instance of your structure by casting it.
>> 
>> I'm not sure this is always the best way to go. Any databases produced like
>> this will not be portable between architectures. In theory, they may not
>> be portable between compilers, but in practice you're probably Ok there.

NK> A good caveat.  If any of the members of your structure are more than
NK> one byte long (ie, an int) this approach will fail terribly if you
NK> create the database on a little-endian machine (Intel/PC) and then try
NK> to use it on a little-endian one (Motorola/Mac).  But I decided to
NK> take the question as 'How do I', rather than 'Should I'.

>> Also, SQLite makes no guarantees as to the alignment of returned blobs.
>> This could cause a random crash somewhere down the line. You could get
>> around this by making a copy of the blob to memory obtained from malloc().

NK> Can you offer more information about this?  In what I'm working on, I
NK> am storing arrays of complex structures as blobs and then reconstituting
NK> them.  I was concerned about alignment at one point, but it seems to
NK> be working without problems.  Are there 'gotchas' that are likely to
NK> 'get me' in the future?  This is server-side, and only for temporary
NK> data, so I'm not concerned about the endian-ness, only the alignment.

NK> Thanks!

NK> --nate





-- 
Best regards,
 Teg                            mailto:[EMAIL PROTECTED]

Reply via email to