Thanks for your replies, Jeff.

Doh! I had forgotten that since I was declaring (char
**) I had to do MemPtrNew twice, once for each
dimension.

But now I'm getting the error that says I'm basically
running out of space, or I didn't allocate enough
space in my buffer. I'm just trying to allocate n rows
of 3 chars (including the null char). My code is the
same, except I've added the line within the loop:

diffs[d] = (char *) MemPtrNew(3 * sizeof(char));

Then, I tried your 2nd suggestion, simply declaring a
static array 

char diffs[MY_MAX_SIZE][10];

That gets farther than the other method, but crashes
when actually drawing the list. I thought I had found
older posts similar to this problem last week, but now
I can't find them. But there seemed to be a frequently
encountered problem when drawing a dynamically
generated list....


--- In [EMAIL PROTECTED], "Jeff Wheeler"
<[EMAIL PROTECTED]> wrote:
> 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!?
SBC Yahoo! DSL - Now only $29.95 per month!
http://sbc.yahoo.com

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

Reply via email to