把LibTom加入到工程后,可以顺利编译通过.通过USB下载到板子上面,开心地调用RSA算法,发现有去无回.
.net micro framework调试c/cpp代码是非常不方便的,还好牛人已经给我探索出了方法.
MDK结合JLINK在调试C/CPP Native工程
用这个调试,发现,libTom使用calloc或者malloc动态分配空间时候,竟然把BUFFER分配到Flash的地址上.....Oh My God....
在群上问了一些人,但是没有任何回复.在Netduion的论坛上面,搜索到一些相关信息.表示不建议使用动态内存.
原文章:http://forums.netduino.com/index.php?/topic/11718-garbage-collector-sd-card-stream-writer-ram/?hl=malloc#entry62045
(To pre-allocate memory for a managed function, just allocate it at the class level. We do this a lot in our driver code. Then use that pre-allocated memory in your function. No dynamic memory allocation or GC required.)
好吧,竟然不行,就重新搞内存管理吧.但只能说理想很美好,现实很骨感.
让我们打一架吧!!!还是失败了.
找呀找呀,怎么在底层使用动态内存呢~~!!竟然发现有tinyhal.h文件中有如下代码:
extern "C" {void* private_malloc ( size_t len );void private_free ( void* ptr );void* private_realloc( void* ptr, size_t len );}
好吧,用他来重定义这两个函数试试.新建一个MEN.C
贴上我的代码
#include <string.h>typedef unsigned char BYTE;typedef unsigned char* PBYTE;typedef unsigned char UINT8;typedef signed char INT8;typedef unsigned short int UINT16;typedef signed short int INT16;typedef unsigned int UINT32;typedef signed int INT32;typedef unsigned __int64 UINT64;typedef signed __int64 INT64;typedef unsigned int size_t;extern void* private_malloc ( size_t len );extern void private_free ( void* ptr );extern void* private_realloc( void* ptr, size_t len );void *memset(void *s, int c, size_t count){ char *xs = s; while (count--) *xs++ = c; return s;}void free(void *ptr){ private_free(ptr);}void * malloc(UINT32 size){ return private_malloc(size);}void *calloc(size_t n, size_t size){ PBYTE lp = private_malloc(n*size); if(lp) { memset(lp,0,n*size); } return lp;}void *realloc(void *ptr,UINT32 size){ return private_realloc(ptr,size); }
见证奇迹的时候......
好吧,你已经成功了....
来自:http://blog.csdn.net/lan120576664