pointer to array in library

Discuss PocketC for Palm OS (including Desktop Edition)

Postby izzy on Fri Jun 10, 2005 3:30 pm

Hi,
I've written a library function with one of the parameters being a pointer to an array of chars, which the library function then fills. Now here's the funny part. When the function is called with a pointer to a regular array created with 'char c[10]', it works fine. Same for array created with malloc() in conjunction settype(). However, when created with malloct(), and none of the elements are initialized, it doesn't work and the array remains blank. Why, and what to do?

Thanx
izzy
 
Posts: 20
Joined: Fri Jun 11, 2004 1:48 pm

Postby dewey on Tue Jun 14, 2005 3:36 am

Can you provide more information about your PocketC and library code? Source code would be helpful.
dewey
Site Admin
 
Posts: 749
Joined: Sun Oct 21, 2007 5:12 am

Postby izzy on Wed Jun 15, 2005 6:55 pm

Here it is. I've tried to copy the native Palm function as much as possible, the only difference being that prefs is a pointer to an array of chars.

void getapppreferences(PocketCLibGlobalsPtr gP) {
//(UInt32?Ǭ†creator, UInt16?Ǭ†id, void?Ǭ†*prefs, UInt16?Ǭ†*prefsSize, Boolean?Ǭ†saved)

Value creator,id,prefs,*prefsArr,prefsSize,*prefsSizeP,saved;Int16 retV,i;UInt16 s=0;char* p;
Value* prefsP;
gP->pop(saved);
gP->pop(prefsSize);
gP->pop(prefs);
gP->pop(id);
gP->pop(creator);
prefsP=gP->deref(prefs.iVal);
prefsSizeP=gP->deref(prefsSize.iVal);
if(prefsSizeP->iVal){
s=prefsSizeP->iVal;
if(!prefsP->iVal)return;
p=(char*)MemPtrNew(prefsSizeP->iVal);
retV=PrefGetAppPreferences
((UInt32)creator.iVal,
(UInt16)id.iVal,p,&s,
(Boolean)saved.iVal);
prefsSizeP->iVal=s;
prefsArr=prefsP;
for(i=0;i<prefsSizeP->iVal;
i++)
(prefsArr+i)->cVal=*(p+i);
MemPtrFree(p);
}else{
retV=PrefGetAppPreferences
((UInt32)creator.iVal,
(UInt16)id.iVal,NULL,&s,
(Boolean)saved.iVal);
prefsSizeP->iVal=s;
}
gP->retVal->iVal =retV;
}
izzy
 
Posts: 20
Joined: Fri Jun 11, 2004 1:48 pm

Postby dewey on Thu Jun 16, 2005 1:23 am

Your code looks good, and since it works with normal array there shouldn't be any problem with malloct() arrays. Can you share the code that calls malloct()?
dewey
Site Admin
 
Posts: 749
Joined: Sun Oct 21, 2007 5:12 am

Postby izzy on Fri Jun 17, 2005 12:43 am

I was just testing it. Here's the code.

//test plib
library "PrefLib"

//turns creator ID strings & similar palm types into ints
PalmType(string str){
int i=-1,conv;
while(i++<4)conv=conv+((int)str@[i]<<((3-i)*8));
return conv;
}

//function for manipulating showing of private recs. Returns previous state
#define SHOW 0
#define HIDE 1
#define MASK 2
#define QUERRY 3
privaterecs(int mode){
int size,ver,retval;pointer p;
getapppreferences(PalmType("psys"),0,null,&size,1);
p=malloct(size,'c');
p[0]=1;
ver=getapppreferences(PalmType("psys"),0,p,&size,1);
retval=p[12]+p[61];
if(mode!=QUERRY){
p[12]=!(!(mode));
p[61]=mode/2;
setapppreferences(PalmType
("psys"),0,ver,p,size,1);
}
free(p);
return retval;
}




main(){
int val=0xaabbccdd,size=0,i,ver,fs,fg,fu;
char c[14];
//={'t','h','i','s',' ','i','s',' ','a',' ','t','e','s','t'};
pointer p;



/*works on arrays. On malloct, it will work if at least 1 element is initiated. Otherwise it will remain blank. Also works if created with malloct in conjunction with settype. works to get size.*/

getapppreferences (PalmType("AAAA"),0,null,&size,1);
//alert(size);
p=malloct(size,'c');

p[0]=1;//won't work without this line

ver=getapppreferences(PalmType("AAAA"),0,p,&size,1);
free(p);

}
izzy
 
Posts: 20
Joined: Fri Jun 11, 2004 1:48 pm

Postby dewey on Sat Jun 18, 2005 2:28 pm

Your library source contains this line:
if(!prefsP->iVal) return;

which returns if the first value in the supplied array is 0. I suspect this is not what you intended, but explains why you need to initialize the first element.
dewey
Site Admin
 
Posts: 749
Joined: Sun Oct 21, 2007 5:12 am

Postby izzy on Mon Jun 20, 2005 4:25 pm

Bingo! Thank you.
I had meant to write
if(!prefs.iVal)return;
to prevent users from passing null for the prefs parameter when prefs
size was not 0.

The question now is why the function didn't return with a char array, but that is mere curiosity as everything works fine now.
izzy
 
Posts: 20
Joined: Fri Jun 11, 2004 1:48 pm


Return to PocketC for Palm OS

Who is online

Users browsing this forum: No registered users and 5 guests

cron