Problem with adl_memGet


#1

hi i have a problem with adl_memGet function. It confuses the string. Here is my code

void fo1()
{
char * myVar = adl_memGet(10);
int i;
for(i=0;i<sizeof(myvar);i++) // initialize my var
myVar[i]=’\0’;
strcpy(myVar,“123456”);
adl_SendResponse(myVar); // write myVar on the screen . it writes 123456 thats correct
adl_memRelease();
}

void fo2()
{
char * myVar = adl_memGet(10);
int i;
for(i=0;i<sizeof(myvar);i++) // initialize my var
myVar[i]=’\0’;
strcpy(myVar,“ABC”);
adl_SendResponse(myVar); // write myVar on the screen . it writes ABC456 thats incorrect. what the hell is this
adl_memRelease();
}

it also confuses the strcat oparations. How can i reduce this error? if i use static allocation i need more and more space in ram.


#2

Hiya,

The sizeof(myVar) call will only return the size of the variable (i.e. the size of a char pointer), not the size of the memory referenced by that variable. You’re lucky that you’re not scribbling over other variables when you do your for() loop.

Try something along the lines of:

void fn0( void )
{
    ascii *myVar;
    s16 varSize = 10;

    myVar = adl_memGet((sizeof(ascii) * varSize));         // allocate memory
    wm_memset(myVar, '\0', varSize);                        // set memory to '\0'
    wm_strcpy(myVar, "ABC123");

    // etc

    adl_memRelease(myVar);
    return;
}

You should also look at using the Wavecom defined data types (e.g. s16, ascii, etc) as the code is being cross-compiled to an ARM processor, and the size of an int/double/long etc may not be what you are used to. Same goes with using the wavecom versions of the string (strcpy etc) functions. I know that these functions are simply re-definitions of the existing functions, but this may not always be the case…

Good Luck.

ciao, Dave


#3

See davidc’s reply.

Not if you keep them local to the function…


#4

thank you very much for your fast reply . also i tried fast and got the result correctly.
the problem was with me as you said. i thought that sizeof(char)=1 so no need to say adl_memGet(sizeof(char)*varSize) .
i wrote only adl_memGet(varSize)
and the second mistake was with for loop. i tought that sizeof(myVar) returns the varSize. Now i changed sizeof(myVar) to varSize.
Now no problems can be seen now thank you very much again