You need a clearer understanding of how structs and char arrays work in C.
When you unpack a record, the struct only contains pointers and the pointers
point into the locked record. When editing a record, you usually point the
pointers to new pieces of memory, e.g. dynamically created memory associated
with fields. Then you pack the new values into the resized record.

You can't write the struct "as-is" in this case, because the struct doesn't
contain your string memory. You can go back to the original problem of
having the struct contain the string memory, but then you have to specify
the size of the string.

Matt


----- Original Message -----
From: "Michel Petrovic" <[EMAIL PROTECTED]>
To: "Palm Developer Forum" <[EMAIL PROTECTED]>
Sent: Wednesday, October 02, 2002 3:44 AM
Subject: Re: How to assign text to a struct?


> Actually I DO have a book: the 2nd edition of the
> O'Reilly book, but as far as I've seen in no place do
> they talk about how to store a string in a struct
> (with the exact size of the string), and later store
> that struct in the DB.
>
> Sorry if I wasn't clear with the "as-is" question.
> With "as-is" I meant storing the the whole struct in
> one go (with only one DmWrite()), instead of writing
> the different fields of the struct separately (using
> DmWrite() for integers and DmStrCopy() for strings as
> you do in your code example).
> So my question was: assuming that all the integers are
> defined first in the struct (and later any existing
> strings), wouldn't the same result be achieved if only
> one DmWrite() is done of the whole struct?
>
> Hopefully that was a bit clearer.
>
> Regards
> Michel.P
>
>
> --- Steve Jerrett <[EMAIL PROTECTED]> wrote:
> > Michel Petrovic wrote:
> > > Thanks for your prompt and exhaustive reply Steven
> > :-)
> > >
> >
> >   My pleasure.
> >
> > > 1) You mention that it's important to clearly
> > divide
> > > the numeric fields from the text fields when data
> > is
> > > stored.
> > > Will it create problems if a struct is stored
> > "as-is",
> > > without this separation? If so, why?
> >
> >   I am unsure what you mean by "as-is". A data
> > structure can be altered
> > from one system (desktop) to Palm. One of the best
> > reasons to keep the
> > numeric values at the beginning of the Palm
> > structure is to keep the
> > byte alignment as the OS expects. I have seen
> > unpredictable results when
> > this does not occur.
> >   If you mean storing the char data in fixed-length
> > fields, there are
> > several issues, not the least of which is wasting
> > space with meaningless
> > garbage in the "unused" text fields. Storing text
> > fields in
> > variable-length null-terminated fields conserves
> > space in the limited
> > available memory and gives you an easy way to assign
> > the appropriate
> > data address to the redefined structure -- the
> > "fields" to display and
> > manipulate.
> >
> > >
> > > 2) Could the code for packing() and unpacking()
> > also
> > > be used when the user modifies a text field that
> > is
> > > already stored in the DB? I'm asking because I see
> > > that the PackedDiv is used for calculating the
> > Offset,
> > > but if it's a modification, isn't there a
> > discrepancy
> > > between the length of the PackedDiv.desc and
> > Div.desc?
> >
> >   You unpack the record before you display each
> > field (as in a record
> > view) or load a table or list (the opening screen of
> > the address
> > application), or edit a record. When changes are
> > made, the record is
> > repacked and the data is stored at a new address.
> >
> >   It might help to go to the Palm Dev site and see
> > what books they
> > recommend. There are some excellent references that
> > give better
> > explanations than I can come up with. It also helps
> > to look at the
> > source code for the on-board applications.
>
> __________________________________________________
> Do you Yahoo!?
> New DSL Internet Access from SBC & Yahoo!
> http://sbc.yahoo.com
>
> --
> For information on using the Palm Developer Forums, or to unsubscribe,
please see http://www.palmos.com/dev/support/forums/
>


-- 
For information on using the Palm Developer Forums, or to unsubscribe, please see 
http://www.palmos.com/dev/support/forums/

Reply via email to