Sorry i haven't read the notes before. So i am sending it again.When I debugging my program, the following function is not working proper. Actually this function runs properly on C, but not in Keil. There are 2 different problems occured. 1) strcat cannot add 2 strings. after strcat no any results. 2) once i merge this function into main function, the second memmmove is also working inproper. The pointer of T and CngStr are becoming the same. How can I solve it?
void main() { SerialInit(); printf("After Function %s\n",Change("0912cf4f3c")); return; } char *Change(char *Str){ char *CngStr; char *T; char *Change(char *Str){ char *CngStr; char *T; CngStr = (char *) malloc (strlen(Str)+2); T = (char *) malloc (strlen(Str)); strcpy(CngStr,""); strcpy(T,""); memmove(CngStr,Str+2,8); CngStr[8] ='\0'; memmove(T,Str,2); T[2] ='\0'; strcat(CngStr,T); free(T); return(CngStr); }
"Sorry i haven't read the notes before." Before posting to any forum, you should always make the time to read the instructions, etc. "So i am sending it again." It would've been more helpful if you'd continued in your original thread, rather than start a new one on exactly the same subject. http://www.keil.com/forum/docs/thread7262.asp "this function runs properly on C, but not in Keil" That still makes no sense! What do you mean? 'C' is a programmaing language - you can't "run" anything "on 'C'" Your program uses malloc. Apart from this generally being not a good thing in embedded systems, have you initialised the heap?
I'm assuming you are using Keil C51. If you intend to use malloc with Keil tools, you have to take special steps (allocate a buffer for heap and call init_mem_pool or something like that), Keil doesn't follow the ANSI C standard completely in this regard, and rightly so. Don't forget that the use of malloc in a microcontroller-based application is strongly discouraged. Why? Search this forum, this topic has been discussed more than once. - mike
as so often before The '51 ain't no PC
http://www.keil.com/support/man/docs/c51/c51_malloc.htm http://www.keil.com/support/man/docs/c51/c51_init_mempool.htm