> From: Matt Andreko [mailto:[EMAIL PROTECTED]
> Sent: Friday, June 06, 2003 12:32 PM
> To: Palm Developer Forum
> Subject: problem writing to database (beginner)
> 
> I have some code which just needs to write to a single table database.
> however, it's dying on a line, that i'm not sure how to fix.  
> (i'm new to palm and c++)

Sounds like an open invitation... :-)

BTW, while I'll pick out some problems, it wouild help if you would describe what you 
meant by "dying".

>       MemHandle h = DmNewRecord(gDatabase, &index, 5);
>...
>       Char * s = (Char *) MemHandleLock(h);
> 
>       Char * pChar = (Char*)MemPtrNew(5 * sizeof(Char*));
>       StrCopy(pChar, "Orders\0");
>       // program is dying on next line
> 
>       DmStrCopy(s, 0, pChar);
>       MemPtrFree( pChar);
>       MemHandleUnlock(h);

* In the line where you call MemPtrNew, note that you are specifying a size of "5 * 
sizeof(Char*)" bytes.  The first problem here is that you need a buffer that holds 7 
characters, not 5.  However, that's made up for by the fact that you are multiplying 
by "sizeof(Char*)", which is 4 bytes long instead of just 1.  That means you are 
allocating 20 bytes, when you thought you were allocating 5 bytes, and you actually 
needed 7.

* There's no need to specifically terminate your string literal with a NUL.  C does 
that for you.

* There's no need to allocate the buffer at all.  Just say:

>       Char * s = (Char *) MemHandleLock(h);
>       DmStrCopy(s, 0, "Orders");
>       MemHandleUnlock(h);

* Finally, the problem is probably because in your DmNewRecord call, you allocate only 
5 bytes when, as we noted, you need 7.

-- Keith

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

Reply via email to