嵌入系统,malloc要自己写,不要去用现成的。<br /><br />有的时候,malloc(0)回答1,还有free什么也不回,你怎么知道是不是free了呢?最好的是,写自己的malloc和free,自己定malloc的范围,自己来回挪指针。这样你就不用担心是malloc不对,还是free不对了。<br /><br />例子的话,网上都有。比如,<br /><br />#define NULL 0<br />#define ALLOCSIZE 100<br /><br />static char allocbuf[ALLOCSIZE];<br />static char *allocptr = allocbuf;<br /><br />char *alloc(n)<br />{<br /> if ( allocptr + n <= allocbuf + ALLOCSIZE)<br /> {<br /> allocptr += n;<br /> return( allocptr - n );<br /> }<br /> else<br /> {<br /> return ( NULL );<br /> }<br />}<br /><br />free( p )<br />{<br /> if ( p >= allocbuf && p < allocbuf + BUFSIZE )<br /> {<br /> allocptr = p;<br /> }<br />}<br />
|