re: Basic C Question (pointer hell)
I am converting from C to C++ and I did not know about reinterpret_cast. Thank you Greg -- For information on using the PalmSource Developer Forums, or to unsubscribe, please see http://www.palmos.com/dev/support/forums/
Basic C Question (pointer hell)
I have a MemPtr that is as a pointer to a custom structure (pdbLayout) PDBData = static_castpdbLayout*(MemPtrNew(size)); This is also the start of my data. I have a second pointer (Char* pOffset) that locates the end of the data. How can I find the size of the data? This is not the right way --- size = static_castInt32(pOffset) - static_castInt32(PDBData); So I can write it to a record: MemMove(pNewData, PDBData, size); Thanks Greg -- For information on using the PalmSource Developer Forums, or to unsubscribe, please see http://www.palmos.com/dev/support/forums/
Re: Basic C Question (pointer hell)
If you are asking a basic 'C' question, then the following will work just fine: unsigned long size; size = (unsigned long)((char *)pOffset - (char *)PDBData) + 1; Otherwise, you are asking a basic 'C++' question, so might I recommend the following: unsigned long size; size = static_castunsigned long(reinterpret_castchar *(pOffset) - reinterpret_castchar *(PDBData)) + 1; On 11/8/06, Greg [EMAIL PROTECTED] wrote: I have a MemPtr that is as a pointer to a custom structure (pdbLayout) PDBData = static_castpdbLayout*(MemPtrNew(size)); This is also the start of my data. I have a second pointer (Char* pOffset) that locates the end of the data. How can I find the size of the data? This is not the right way --- size = static_castInt32(pOffset) - static_castInt32(PDBData); So I can write it to a record: MemMove(pNewData, PDBData, size); Thanks Greg -- For information on using the PalmSource Developer Forums, or to unsubscribe, please see http://www.palmos.com/dev/support/forums/ -- yisdersomenimororsisasisdenderisorsis? Jeff Loucks -- For information on using the PalmSource Developer Forums, or to unsubscribe, please see http://www.palmos.com/dev/support/forums/
re: Basic C Question (pointer hell)
Greg wrote: I have a MemPtr that is as a pointer to a custom structure (pdbLayout) PDBData = static_castpdbLayout*(MemPtrNew(size)); This is also the start of my data. I have a second pointer (Char* pOffset) that locates the end of the data. How can I find the size of the data? As Jeff said, cast both pointers to Char* and subtract. If your end pointer points to just beyond the end of your memory block (i.e., it's an exclusive endpoint), you don't need the +1. If it's inclusive, you do. On Palm OS, you also simply could call MemPtrSize to get the size of a block allocated with MemPtrNew. -- For information on using the PalmSource Developer Forums, or to unsubscribe, please see http://www.palmos.com/dev/support/forums/
Re: Basic C question
Try replacing Char *encDate = 34857384573498573498; with Char encDate[] = 34857384573498573498; Also, have you try the following before? StrCopy(record.Date, 34857384573498573498); On 1 Apr 2002 at 19:02, Ed wrote: With the code below, I get the following error: just read from memory location 0x003CD32, which is in the unused portion of the stack. The stack range is ... - ... and the stack pointer is ... This does not happen everytime. Any ideas? Thanks, Ed. Borislav Kolev wrote: The I can only assume what you have left out of your example, but with these assumptions it is correct it should be typedef struct RecData { Char Date[256]; } RecData; void AddToDateDatabase(RecData* v); void foo() { RecData record; Char *encDate = 34857384573498573498; StrCopy(record.Date, encdate);// Generally better use StrNCopy() and clear out the last Char (not byte!) error = AddToDateDatabase(record); } And this seems to be fine. - bobby -- 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/ John Leung [EMAIL PROTECTED] http://persweb.direct.ca/jleung -- For information on using the Palm Developer Forums, or to unsubscribe, please see http://www.palmos.com/dev/support/forums/
Re: Basic C question
This works StrCopy(record.Date, 34857384573498573498); This fails = StrCopy(record.Date, encDate); It says that Application just read from memory location X which is the unused portion of the stack. The stack is the area of RAM used to contain function parameters and local variables. Applications may access the area of the stack above the stack pointer, but not below it. All I want to to is get the value of encDate into record.Date. I cant see why this is failing unless I am missing something. Any ideas? Thanks, Ed. John Leung wrote: Try replace the following line Char *encDate = 34857384573498573498; with Char encDate[] = 34857384573498573498; Also, have you ever tried the following before? StrCopy(record.Date, 34857384573498573498); The I can only assume what you have left out of your example, but with these assumptions it is correct it should be typedef struct RecData { Char Date[256]; } RecData; void AddToDateDatabase(RecData* v); void foo() { RecData record; Char *encDate = 34857384573498573498; StrCopy(record.Date, encdate);// Generally better use StrNCopy() and clear out the last Char (not byte!) error = AddToDateDatabase(record); } -- For information on using the Palm Developer Forums, or to unsubscribe, please see http://www.palmos.com/dev/support/forums/
Re: Basic C question
Ed == Ed [EMAIL PROTECTED] scribbles: Ed This works StrCopy(record.Date, Ed 34857384573498573498); This fails = Ed StrCopy(record.Date, encDate); It says that Application just Ed read from memory location X which is the unused portion of the Ed stack. The stack is the area of RAM used to contain function Ed parameters and local variables. Applications may access the Ed area of the stack above the stack pointer, but not below it. What is the real declaration of Record? Sounds like you are writing off the end of the array, and hence the stack. Jake -- For information on using the Palm Developer Forums, or to unsubscribe, please see http://www.palmos.com/dev/support/forums/
Re: Basic C question
Looks like you are passing the variable instead of its address for the second parameter. StrCopy expects a src and dst string address. Try passing (Char *)encDate. Regards, Charu -Original Message- From: Ed [EMAIL PROTECTED] To: Palm Developer Forum [EMAIL PROTECTED] Date: Tuesday, April 02, 2002 4:00 PM Subject: Re: Basic C question This works StrCopy(record.Date, 34857384573498573498); This fails = StrCopy(record.Date, encDate); It says that Application just read from memory location X which is the unused portion of the stack. The stack is the area of RAM used to contain function parameters and local variables. Applications may access the area of the stack above the stack pointer, but not below it. All I want to to is get the value of encDate into record.Date. I cant see why this is failing unless I am missing something. Any ideas? Thanks, Ed. John Leung wrote: Try replace the following line Char *encDate = 34857384573498573498; with Char encDate[] = 34857384573498573498; Also, have you ever tried the following before? StrCopy(record.Date, 34857384573498573498); The I can only assume what you have left out of your example, but with these assumptions it is correct it should be typedef struct RecData { Char Date[256]; } RecData; void AddToDateDatabase(RecData* v); void foo() { RecData record; Char *encDate = 34857384573498573498; StrCopy(record.Date, encdate);// Generally better use StrNCopy() and clear out the last Char (not byte!) error = AddToDateDatabase(record); } -- 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/
Re: Basic C question
I can see why Char *encDate = 34857384573498573498; might fail. Because you're assigning a pointer to some temp variable. It depends on when that temp variable decides to go out of scope, that pointer may no longer be valid. (Although I could be wrong. I'm not a C expert.) However, I'm not sure why Char encDate[] = 34857384573498573498; fails. Because this time you're actually creating a variable on the stack and initialize the content with the indicated string. I'm sorry I couldn't help you further. At 01:54 PM 02/04/2002 -0800, Ed wrote: This works StrCopy(record.Date, 34857384573498573498); This fails = StrCopy(record.Date, encDate); It says that Application just read from memory location X which is the unused portion of the stack. The stack is the area of RAM used to contain function parameters and local variables. Applications may access the area of the stack above the stack pointer, but not below it. All I want to to is get the value of encDate into record.Date. I cant see why this is failing unless I am missing something. Any ideas? Thanks, Ed. John Leung wrote: Try replace the following line Char *encDate = 34857384573498573498; with Char encDate[] = 34857384573498573498; Also, have you ever tried the following before? StrCopy(record.Date, 34857384573498573498); The I can only assume what you have left out of your example, but with these assumptions it is correct it should be typedef struct RecData { Char Date[256]; } RecData; void AddToDateDatabase(RecData* v); void foo() { RecData record; Char *encDate = 34857384573498573498; StrCopy(record.Date, encdate);// Generally better use StrNCopy() and clear out the last Char (not byte!) error = AddToDateDatabase(record); } -- For information on using the Palm Developer Forums, or to unsubscribe, please see http://www.palmos.com/dev/support/forums/ John Leung EMail: [EMAIL PROTECTED] Web: http://persweb.direct.ca/jleung/ -- For information on using the Palm Developer Forums, or to unsubscribe, please see http://www.palmos.com/dev/support/forums/
Re: Basic C question
Jake == Jake Donham [EMAIL PROTECTED] asserts: Jake What is the real declaration of Record? Sounds like you are Jake writing off the end of the array, and hence the stack. Um, retract :). Jake -- For information on using the Palm Developer Forums, or to unsubscribe, please see http://www.palmos.com/dev/support/forums/
Re: Basic C question
From: Ed [EMAIL PROTECTED] This works StrCopy(record.Date, 34857384573498573498); This fails = StrCopy(record.Date, encDate); It says that Application just read from memory location X which is the unused portion of the stack. The stack is the area of RAM Need to know a little more to comment usefully. Are you using encDate in the function it's declared in or returning it from another function? Generally stack-based problems are caused by returning a pointer to a local variable which will then cause any access to that pointer to fail because the stack area that it was defined at has been unstacked. Chris Tutty -- For information on using the Palm Developer Forums, or to unsubscribe, please see http://www.palmos.com/dev/support/forums/
Basic C question
Below is a sample of what I am trying to do. I have a database with the following datatype: Char Date[256]; In my function, I have: Char *encDate encDate = 34857384573498573498; StrCopy(record.Date, encDate); error = AddToDateDatabase(record); What I want to do is copy encDate into the record.Date but this gives me errors on the StrCopy line. Obviously this is not the right way to go. I am looking for a suggestion to do what I want to do. Any ideas? Thanks, Ed. -- For information on using the Palm Developer Forums, or to unsubscribe, please see http://www.palmos.com/dev/support/forums/
RE: Basic C question
Actually, this is a PalmOS specific programming issue, if I understand your problem correctly. You want to copy a string into a database record. To write to a database record, you have to use specific APIs that check to make sure you don't write outside the record you're intending to modify. That helps make the database more secure against data loss from a bug in an application. Normally, you used DmWrite to copy a specific number of bytes into a database record, but to copy a null terminated string, you use DmStrCopy. -- Peter Epstein -- For information on using the Palm Developer Forums, or to unsubscribe, please see http://www.palmos.com/dev/support/forums/
RE: Basic C question
The I can only assume what you have left out of your example, but with these assumptions it is correct it should be typedef struct RecData { Char Date[256]; } RecData; void AddToDateDatabase(RecData* v); void foo() { RecData record; Char *encDate = 34857384573498573498; StrCopy(record.Date, encdate);// Generally better use StrNCopy() and clear out the last Char (not byte!) error = AddToDateDatabase(record); } And this seems to be fine. - bobby -- For information on using the Palm Developer Forums, or to unsubscribe, please see http://www.palmos.com/dev/support/forums/
Re: Basic C question
With the code below, I get the following error: just read from memory location 0x003CD32, which is in the unused portion of the stack. The stack range is ... - ... and the stack pointer is ... This does not happen everytime. Any ideas? Thanks, Ed. Borislav Kolev wrote: The I can only assume what you have left out of your example, but with these assumptions it is correct it should be typedef struct RecData { Char Date[256]; } RecData; void AddToDateDatabase(RecData* v); void foo() { RecData record; Char *encDate = 34857384573498573498; StrCopy(record.Date, encdate);// Generally better use StrNCopy() and clear out the last Char (not byte!) error = AddToDateDatabase(record); } And this seems to be fine. - bobby -- 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/
Re: Basic C question
I don't know if it will make a bit of difference, but when all else fails, start grasping at straws... I'd start by adding const to the char * declaration: const Char *encDate = 34857384573498573498; Beyond that.. bobby's code below looks good to me, too. -- Bradly J. Barton - [EMAIL PROTECTED] Jenies Technologies Incorporated (972) 602-1835 http://www.JTI.net http://HandAble.com typedef struct RecData { Char Date[256]; } RecData; void AddToDateDatabase(RecData* v); void foo() { RecData record; Char *encDate = 34857384573498573498; StrCopy(record.Date, encdate);// Generally better use StrNCopy() and clear out the last Char (not byte!) error = AddToDateDatabase(record); } And this seems to be fine. - bobby -- For information on using the Palm Developer Forums, or to unsubscribe, please see http://www.palmos.com/dev/support/forums/
Re: Basic C question
Try replace the following line Char *encDate = 34857384573498573498; with Char encDate[] = 34857384573498573498; Also, have you ever tried the following before? StrCopy(record.Date, 34857384573498573498); On Mon, 01 Apr 2002 19:02:31 -0800, Ed [EMAIL PROTECTED] wrote: With the code below, I get the following error: just read from memory location 0x003CD32, which is in the unused portion of the stack. The stack range is ... - ... and the stack pointer is ... This does not happen everytime. Any ideas? Thanks, Ed. Borislav Kolev wrote: The I can only assume what you have left out of your example, but with these assumptions it is correct it should be typedef struct RecData { Char Date[256]; } RecData; void AddToDateDatabase(RecData* v); void foo() { RecData record; Char *encDate = 34857384573498573498; StrCopy(record.Date, encdate);// Generally better use StrNCopy() and clear out the last Char (not byte!) error = AddToDateDatabase(record); } And this seems to be fine. - bobby -- 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/
Re: Basic C question
Todd, Here are two solutions for PocketStudio. One if you like VB and one if you like DELPHI g -- Douglas Olson - President Co-founder Pocket Technologies, Inc. - [EMAIL PROTECTED] - http://www.pocket-technologies.com Develop PalmOS applications with your Delphi skills Today! //** // // Much like the Visual Basic command MID$ (but different) // procedure Mid(dst, src: PChar; Index, Count: Integer); begin if Index StrLen(Src) then // Bad index check begin Dst[0] := #0; // Return empty string Exit; end; if (Count + Index - 1) StrLen(Src) then // Bad count check Count := StrLen(Src) - Index + 1; StrNCopy(Dst, @Src[Index - 1], Count); Dst[Count] := #0; end; // // Much like the DELPHI Copy command (but different) // procedure Copy(Src, Dst: PChar; Index, Count: Integer); begin StrNCopy(Dst, @Src[Index - 1], Count); Dst[Count] := #0; end; -- For information on using the Palm Developer Forums, or to unsubscribe, please see http://www.palmos.com/dev/tech/support/forums/
Re: Basic C question
On Sun, 30 Sep 2001, Todd Cary wrote: var Dst: PChar; Src: PChar; begin StrCopy(Src, 'Demo string'); // So far so good // Now, how do I copy string to Dst? // I need a substring // This does not work: StrCopy(Dst, Src + 5); end; kinda helps if you allocate the memory for the strings :) // az [EMAIL PROTECTED] http://www.ardiri.com/ -- For information on using the Palm Developer Forums, or to unsubscribe, please see http://www.palmos.com/dev/tech/support/forums/
Re: Basic C question
On Sun, 30 Sep 2001, Bradly J. Barton wrote: StrCopy(Src, Demo string); StrCopy(Dst, Src+5); Personally, I like to use: StrCopy(Dst, Src[5]); But that's just a personal preference... nej :) pointers.. pointers... pointers :) one of the best things about C // az [EMAIL PROTECTED] http://www.ardiri.com/ -- For information on using the Palm Developer Forums, or to unsubscribe, please see http://www.palmos.com/dev/tech/support/forums/
Re: Basic C question
Bradley - Many thanks! I forgot to use the @str[2] representation in Pascal (PocketStudio for the Palm). In my code, I have a field that contains a Time (e.g. 12:32) and I need to access the 12 and 32. It now worksagain thanks Todd -- Todd Cary Ariste Software [EMAIL PROTECTED] -- For information on using the Palm Developer Forums, or to unsubscribe, please see http://www.palmos.com/dev/tech/support/forums/
Re: Basic C question
I think only 'D' will be copied to protected space. Single quotes, and all. -Nullife Bradly J. Barton [EMAIL PROTECTED] wrote in message news:63712@palm-dev-forum... --- Todd Cary wrote: var Dst: PChar; Src: PChar; begin StrCopy(Src, 'Demo string'); // So far so good // Now, how do I copy string to Dst? // I need a substring // This does not work: StrCopy(Dst, Src + 5); end; In C, you could do this: Char Src[12]; Char Dst[7]; StrCopy(Src, Demo string); StrCopy(Dst, Src+5); Personally, I like to use: StrCopy(Dst, Src[5]); But that's just a personal preference... Quite likely, Joe's assessment that you are not allocating space for the strings before copying into them, so you are copying Demo string and string into random (and probably protected) memory spaces. -- For information on using the Palm Developer Forums, or to unsubscribe, please see http://www.palmos.com/dev/tech/support/forums/
Re: Basic C question
--- Nullife [EMAIL PROTECTED] wrote: I think only 'D' will be copied to protected space. Single quotes, and all. No, he's using Pascal, which uses single quotes for strings. If he was using CodeWarrior C, 'Demo string' would be an illegal character constant. I don't know what gcc would think about it, but I doubt it likes it, either. __ Do You Yahoo!? Listen to your Yahoo! Mail messages from any phone. http://phone.yahoo.com -- For information on using the Palm Developer Forums, or to unsubscribe, please see http://www.palmos.com/dev/tech/support/forums/
Basic C question
var Dst: PChar; Src: PChar; begin StrCopy(Src, 'Demo string'); // So far so good // Now, how do I copy string to Dst? // I need a substring // This does not work: StrCopy(Dst, Src + 5); end; Todd -- Todd Cary Ariste Software [EMAIL PROTECTED] -- For information on using the Palm Developer Forums, or to unsubscribe, please see http://www.palmos.com/dev/tech/support/forums/
Re: Basic C question
--- Todd Cary wrote: var Dst: PChar; Src: PChar; begin StrCopy(Src, 'Demo string'); // So far so good // Now, how do I copy string to Dst? // I need a substring // This does not work: StrCopy(Dst, Src + 5); end; This looks more like a Pascal question than a C question, since your code is obviously Pascalese. In C, you could do this: Char Src[12]; Char Dst[7]; StrCopy(Src, Demo string); StrCopy(Dst, Src+5); Note, however, that the following code would fail in C because it doesn't allocate any space for the strings: Char *Src; Char *Dst; StrCopy(Src, Demo string); // error StrCopy(Dst, Src+5); // another error Your code is probably failing for the same reason. __ Do You Yahoo!? Listen to your Yahoo! Mail messages from any phone. http://phone.yahoo.com -- For information on using the Palm Developer Forums, or to unsubscribe, please see http://www.palmos.com/dev/tech/support/forums/
Re: Basic C question
--- Todd Cary wrote: var Dst: PChar; Src: PChar; begin StrCopy(Src, 'Demo string'); // So far so good // Now, how do I copy string to Dst? // I need a substring // This does not work: StrCopy(Dst, Src + 5); end; In C, you could do this: Char Src[12]; Char Dst[7]; StrCopy(Src, Demo string); StrCopy(Dst, Src+5); Personally, I like to use: StrCopy(Dst, Src[5]); But that's just a personal preference... Quite likely, Joe's assessment that you are not allocating space for the strings before copying into them, so you are copying Demo string and string into random (and probably protected) memory spaces. -- For information on using the Palm Developer Forums, or to unsubscribe, please see http://www.palmos.com/dev/tech/support/forums/
Re: Basic C question
It is actually from PocketStudio (http://www.pocket-technologies.com). It is a new development tool for the PalmOS. It does create optimized, native PalmOS applications (fast and small like C) and NO RUNTIME. Just thought you would want to know! -- Douglas Olson - President Co-founder Pocket Technologies, Inc. - [EMAIL PROTECTED] - http://www.pocket-technologies.com Develop PalmOS applications with your Delphi skills Today! Joe Programmer [EMAIL PROTECTED] wrote in message news:63710@palm-dev-forum... --- Todd Cary wrote: var Dst: PChar; Src: PChar; begin StrCopy(Src, 'Demo string'); // So far so good // Now, how do I copy string to Dst? // I need a substring // This does not work: StrCopy(Dst, Src + 5); end; This looks more like a Pascal question than a C question, since your code is obviously Pascalese. In C, you could do this: Char Src[12]; Char Dst[7]; StrCopy(Src, Demo string); StrCopy(Dst, Src+5); Note, however, that the following code would fail in C because it doesn't allocate any space for the strings: Char *Src; Char *Dst; StrCopy(Src, Demo string); // error StrCopy(Dst, Src+5); // another error Your code is probably failing for the same reason. __ Do You Yahoo!? Listen to your Yahoo! Mail messages from any phone. http://phone.yahoo.com -- For information on using the Palm Developer Forums, or to unsubscribe, please see http://www.palmos.com/dev/tech/support/forums/