完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
我尝试在PX24F控制器的XC16编译器中使用MalLoad函数分配动态内存,但上面的函数不能在运行时分配内存。
以上来自于百度翻译 以下为原文 I am trying to allocate dynamic memory using malloc function in XC16 complier for PIC24F controller, but above function not able to allocate memory at run time. char *i; i = (char*)malloc(5* sizeof(char)); |
|
相关推荐
16个回答
|
|
不能在MALLC中返回空值吗?你准备堆了吗?
以上来自于百度翻译 以下为原文 Not able to as in malloc returns null? Did you setup a heap? |
|
|
|
我已经将BothHead和堆栈设置为50MaloC而不返回null校验,如果(i=空)
以上来自于百度翻译 以下为原文 I have set both Heap and stack to 50 malloc not returning null I check with if(i == NULL) |
|
|
|
50的声音对于堆栈来说太小了。如果MALLC没有返回NULL,那么它确实起作用了。
以上来自于百度翻译 以下为原文 50 sounds way too small for the stack. If malloc did not return NULL then it did work. |
|
|
|
但我只是在分配
以上来自于百度翻译 以下为原文 but I am allocating only i = (char*)malloc(5* sizeof(char)); 5* sizeof(char) How much size should I should keep? |
|
|
|
|
|
|
|
正如尼尔所说,你确实分配了内存。那么你的问题现在解决了吗?你不需要把默认的“MimIm堆栈大小”从16更改为50。正如它所说的,它只是一个最小的大小,与堆相比,在这里你指定最大的大小。如果你为堆分配50个字节,你应该在你的构建输出中看到类似的东西:当然,50个字节对于你的5字节Maloc足够大。如果你的指针是I!= NULL,那么你可以使用I.指向的内存。我建议不要给你的指针一个类似的名称。概率很高,以后你会正确地使用那个变量。为什么使用动态内存?只是学习?
以上来自于百度翻译 以下为原文 As Neil said, you DID allocate the memory. So your problem is solved now ? There should be no need for you to change the default "minimim stack size" from 16 to 50. As it says, it's just a minimum size, in contrast to the heap, where you specify the maximum size. If you allocate 50 bytes for the heap, you should see something similar like this in your build output: Dynamic Memory Usage region address maximum length (dec) ------ ------- --------------------- heap 0x838 0x32 (50) stack 0x86a 0x3f96 (16278) Maximum dynamic memory (bytes): 0x3fc8 (16328) Of course 50 bytes is large enough for your 5 byte malloc. If your pointer i is != NULL, then you can use the memory pointed to by i. I would recommend not to give your pointer a name like i. Probability is high that you will use that variable not correctly afterwards. Why do you use dynamic memory ? Just learning ? |
|
|
|
使用CaloC(int元素,sisitht size);清除分配后的内存。如果(NULL==(T.Buffer = MeMyCaloC(5,sieZof(ObjutsIt)))返回NULL;如果您打算使用它,例如分配和释放,请尽可能多地给出堆。
以上来自于百度翻译 以下为原文 Use calloc(int Elements, size_t Size); to clear the memory after allocation. if(NULL == (T.Buffer = mem_calloc(5, sizeof(object_t)))) return NULL; Give the heap as much as you can if you intend to use it a lot such as allocating and freeing. |
|
|
|
我正在尝试读取一个连接在我的控制器串口上的设备的确认。确认长度从2字节到20字节不等。在确认的基础上,我必须执行一些任务。我的代码完美地工作在第一的确认之后,在动态内存中,我没有收到任何数据。我有25个命令集,我用固定数组检查,工作正常。
以上来自于百度翻译 以下为原文 I am trying to read acknowledgment of a device connected on serial port of my controlle. Acknowledgment length vary from 2 byte to 20 byte. On bases of acknowledgment I have to perform some task. My code work perfectly for 1st acknowledgment, after that I do't receive any data in dynamic memory. I have 25 command set. I check with fixed array, it's work fine. |
|
|
|
(U2Stest.UrxDa==1){ ACK[TEMP]=U2RXRG;//拷贝char / /(U2StestIt.UrxDa==1);/ /等待缓冲区满UpTEMP++;} ACK[TEMP]=U2RXRG;//拷贝CHAR//IFS1BIT.U2RXIF=0;//RE;设置中断/返回温度;/返回接收字符
以上来自于百度翻译 以下为原文 void uart2getchar() { char temp = 0; char i = 0; while(U2STAbits.URXDA == 0); //wait for the buffer to fill up while(U2STAbits.URXDA == 1) { ACK[temp] = U2RXREG; //copy char // while(U2STAbits.URXDA == 1); //wait till buffer buffer full up temp++; } ACK[temp] = U2RXREG; //copy char // IFS1bits.U2RXIF = 0; //reset interrupt // return temp; //return received char } |
|
|
|
这样的代码片段是没有用的。我们无法看到如何创建或使用ACK[]或TEMP。
以上来自于百度翻译 以下为原文 A code snippet like that is useless. We can't see how you create or use ACK[] or temp. |
|
|
|
Maloc(和朋友)对于嵌入式系统来说可能是非常危险的,除非你非常小心,并且以相反的顺序释放分配的存储来分配它们。内存/堆碎片会很快杀死整个进程。在你的情况下(你将收到2到20个字符),我刚刚分配了一个20(加)缓冲区,并有一个计数器/指针,表示下一个接收到的字符。当您检测到接收到所有字符后,处理缓冲区中的字符并重置计数器/POTIN。如果在前一个字符的处理之间可能存在重叠(例如,当您正在处理第一组时,下一组的第一个到达时),然后使用R。ING缓冲器苏珊
以上来自于百度翻译 以下为原文 malloc (and friends) can be incredibly dangerous for embedded systems unless you are VERY careful and release the allocated storage in the reverse order to allocating them. Memory/heap fragmentation will kill this whole process very quickly. In your case (where you will receive 2 to 20 characters), I'd just allocated a 20 (plus) buffer and have a counter/pointer that says whee the next received character goes. When you detect that you have received all characters then process the characters from the buffer and reset the counter/pointer. If there could be an overlap between the processing of the previous characters (e.g. the first of the next set arrives while you are processing the first set) then use a ring buffer. Susan |
|
|
|
正如苏珊所说,没有必要用Maloc来复杂化你的生活。此外,我看不出,如何合理地使用MALOC来完成任务。事实上,您的确认数据可能是2或20字节,没有理由使用MALOC。根据我的理解,你不知道它是2字节还是N字节,所以你不知道要分配多少内存。如果这是错误的,你应该再次解释为什么你要使用MALOC。正如QHB所说,你的代码摘录没有帮助看到你的问题的原因,就MALOC而言。无论如何,你的函数UART2GETCHAR看起来很奇怪。你期望它做什么?变量TEMP是一个局部变量,在每次调用UAR2GETCHAR时都会初始化为0。这意味着,ACK[]中的所有内容将在下一次调用中被覆盖。你怎么知道你收到了完整的回复?你还期望多个字符?为什么在ACC数组中保存两个(最后一个)字符?如果您的代码没有MALOC工作,那么我建议使用该版本。
以上来自于百度翻译 以下为原文 As Susan said, there is really no need to complicate your life with using malloc. Moreover I do not see, how you could use malloc (reasonably) for your task. The fact that your acknowledgement data may be 2 or 20 byte is no reason to use malloc. From my understanding you do not know whether it will be 2 or N byte, so you don't know how much memory to allocate. If that's wrong you should explain again, why you want to use malloc. As qhb said, your code excerpt does not help to see the cause for your problem as far as malloc is concerned. However your function uart2getchar looks weird anyway. What do you expect it to do ? The variable temp is a local variable and will be initialized to 0 on each call to uar2getchar. This means, everything in ACK[] will be overwritten on the next call. How do you know you have received the complete response ? Also you expect multiple chars ? And why do you save the (last) character twice in your ACC array ? void uart2getchar() { char temp = 0; char i = 0; while(U2STAbits.URXDA == 0); //wait for the buffer to fill up while(U2STAbits.URXDA == 1) { ACK[temp] = U2RXREG; //copy char // while(U2STAbits.URXDA == 1); //wait till buffer buffer full up temp++; } ACK[temp] = U2RXREG; //copy char } If your code works without malloc then I recommend using that version. |
|
|
|
然后是RALC(ReSime),我同意苏珊,分配5字节是毫无意义的。
以上来自于百度翻译 以下为原文 Then there is ralloc (resize) I Agree with Susan, it is pointless allocating 5 bytes. |
|
|
|
我试图在网站上发布我的代码,但是这个网站不允许我这么做。
以上来自于百度翻译 以下为原文 I am trying to post my code on website, but this site is not allowing me to do that. |
|
|
|
请参阅附件文件。我有25个不同的命令集,我知道每个命令集的确认。
以上来自于百度翻译 以下为原文 Please see attached file. I have 25 different command set and I know acknowledge of each command set. Attachment(s) New Text Document (3).txt (1.22 KB) - downloaded 31 times |
|
|
|
再次,你最好使用最大数组siZ.Char x(20);有C/C/m/OLLC和空闲的时间和地点。
以上来自于百度翻译 以下为原文 Again, you are better off using a maximum array size. char x[20]; There is a time and place for c/r/m/alloc and free. i.e. draw big A upside down const char *addr=lcd_getcharaddr('A',&fontw,&fonth); //get addess of A mem=lcd_calcresize_dim(fontw,fonth,addr,5,5); //calculate space for font size 5 * width and 5 * height, suppy dimensions if(NULL==(Buffer=mem_malloc(mem)))return ERROR_ALLOC; lcd_resizebitmap(addr,Buffer,5,5,RESIZE_MIRRORY); //resize bitmap fw * 5, fh * 5, mirror y lcd_draw(0,0,Buffer); mem_free(Buffer); // (5*5+7)/8*7*5+2 = 142 bytes return ERROR_OK; |
|
|
|
只有小组成员才能发言,加入小组>>
5142 浏览 9 评论
1989 浏览 8 评论
1917 浏览 10 评论
请问是否能把一个ADC值转换成两个字节用来设置PWM占空比?
3159 浏览 3 评论
请问电源和晶体值之间有什么关系吗?PIC在正常条件下运行4MHz需要多少电压?
2215 浏览 5 评论
708浏览 1评论
598浏览 1评论
有偿咨询,关于MPLAB X IPE烧录PIC32MX所遇到的问题
481浏览 1评论
PIC Kit3出现目标设备ID(00000000)与预期的设备ID(02c20000)不匹配。是什么原因
609浏览 0评论
508浏览 0评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-11-9 13:44 , Processed in 1.278776 second(s), Total 74, Slave 67 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号