Whoops, one more thing...

Since you're going to give data to the list, if you use the stack method
your declaration will need to be "static" since the data must exist for the
life of the list.

Jeff
 

-----Original Message-----
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED] On Behalf Of Jeff
Wheeler
Sent: Saturday, June 14, 2003 7:35 AM
To: Palm Developer Forum
Subject: RE: bus error building dynamic list

Your error isn't related to the lists at all, but rather to incorrect use of
pointers and memory allocations.  Your memory allocation is wrong or
incomplete.  You are allocating a block of memory and telling the compiler
that it will contain an array of pointers to other characters, but then you
access those pointers without initializing them.  The value of these
pointers is garbage, so you generate an error.

Try something like this:

  #define MY_MAX_SIZE 3

  Char ** diffs = (Char **) MemPtrNew(level * sizeof(Char *));
  for (d=0; d<level; d++)
    diffs[d] = (Char *) MemPtrNew(MY_MAX_SIZE * sizeof(Char));

  ...

  // Free memory appropriately when done...

Or,

  Char ** diffs = (Char **) MemPtrNew(level * sizeof(Char *));
  Char * buffer = (Char *) MemPtrNew(level * sizeof(Char) * MY_MAX_SIZE);
  for (d=0; d<level; d++)
    diffs[d] = &buffer[d * sizeof(Char) * MY_MAX_SIZE];

  ...

  // Free memory appropriately when done.

Or, just declare an array that is big enough for what you want.  If the
scope of the variables is only this function and doesn't need to be global,
then the size is likely small enough to reasonably store it on the stack.

  Char diffs[MY_MAX_SIZE][10];

(Or it may be diffs[10][MY_MAX_SIZE] -- I always have to look it up.)

Make sure you allocate memory for the NULL that will terminate the string.
Code above has not been compiled or debugged, but should serve for an
example.

Jeff
 

-----Original Message-----
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED] On Behalf Of Brian
Preston
Sent: Friday, June 13, 2003 4:45 PM
To: Palm Developer Forum
Subject: bus error building dynamic list

I'm trying to build my first dynamic list. 'Dynamic'
might be an exaggeration, I'm simply trying to build a
list of numbers 1 through x, where x's max value is
10, and x is randomly determined. The error occurs at
the first StrCopy. What am I doing wrong? I'm also not
sure if I need 3 chars for each line instead of two,
to include the null terminator at the end of each
string.

Here's the code:

int level = GetRandom(10)+1;
int d=0;
char SBuf2[5];
        
char **diffs = (char **) MemPtrNew (level * 2 *
sizeof(char));
        
for (d; d < level; d++) {
                
  if ( d<9) {
    StrCopy(diffs[d], " ");
    StrIToA(SBuf2, d+1);
    StrCat(diffs[d], SBuf2);
  } else {
    StrCopy(diffs[d], "10");
  }
                
}
        
LstSetListChoices(GetObjectPtr(StartDifficultyList),
diffs, level);
LstSetSelection(GetObjectPtr(StartDifficultyList), level-1);

__________________________________
Do you Yahoo!?
Yahoo! Calendar - Free online calendar with sync to Outlook(TM).
http://calendar.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/




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

Reply via email to