完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
您好,我试着编辑一个反网络堆栈的一部分,不使用Maloc,因为它总是失败。代码使用MALOC创建对象并插入到链表中。在下面的代码片段中,我已经注释出了MALOC。我的计划是创建结构体的本地实例并将其插入到列表中。我可以把2个项目插入到我的列表中,当试图添加第三时,列表没有被正确地终止,我输入了无限的while循环。有人知道为什么我的名单没有被正确地终止吗?此代码简单地将元素添加到列表中,整个代码随后将一些变量设置为默认值。
以上来自于百度翻译 以下为原文 Hello, I am trying to edit part of a backnet stack to not use malloc as it always fails. The code uses malloc to create an object and insert into a linked list. In the following code snippet I have commented out the malloc. My plan is to create a local instance of the struct and insert that into my list. I am able to insert 2 items into my list, when trying to add the third, the list is not properly terminated and I enter an infinite while loop. Can anyone see why my list is not properly terminated? CHobjects* newNode(instance, channel, name, description) { CHobjects *node; CHobjects newNode; node=CHobjects; while(node!=NULL) { if(node->instance==instance) return node=node->next; } if(strlen((char *)objectName)>objectNameMax || strlen((char *)description)>descriptionMax) goto cc8; //fail name or description is too long // if((node=(CHobject *)malloc(sizeof(CHobject)))==NULL) //get a block of space for this object's info // goto cc8; //fail if we can't get space for it test.next=CHobjects; //link on to list CHobjects=&test; CHcount++; } This code simply adds the elements to the list, the whole code would afterwards set some variables to default values. |
|
相关推荐
12个回答
|
|
在退出这个函数之后,本地元素将不再存在。这是使用MALOC获取函数完成后将幸存的内存的全部要点。
以上来自于百度翻译 以下为原文 The local element won't exist any more after you exit this function. That's the whole point of using malloc to obtain some memory that will survive after the function finishes. |
|
|
|
对象的本地实例在堆栈上创建,并且在函数退出时无效。
以上来自于百度翻译 以下为原文 A local instance of the object is created on the stack and is not valid when the function exits. /Ruben |
|
|
|
如果你想让MALOC工作,你需要分配一个堆。你可以从中创建一个结构数组和“Malc”。动态分配不被认为是最好的小嵌入式系统。您可能只想重写它而不使用MALOC。
以上来自于百度翻译 以下为原文 If you want malloc to work you need to allocate a heap. You could create an array of structs and "Malloc" from that. dynamic allocating is not considered the best thing for small embedded systems. You may want to just rewrite it to not use malloc. |
|
|
|
我想这就是他们想要做的;)高尚的意图,但是糟糕的实现,你需要用一些实现来代替它来分配每一个项目。可能只是你自己管理的一个固定大小的数组。
以上来自于百度翻译 以下为原文 I think that's what they're trying to do ;) Noble intention, but bad implementation, you need to replace it with SOME implementation to allocate each item. Probably just a fixed size array that you manage yourself. |
|
|
|
列表中的元素必须存储在某个地方。如果你想避免MalCube(),你可以创建一个CHObjt数组,它有足够的元素来满足你的需要。然后,将数组中的所有对象链接到一个列表中,并将其称为EMPTyjLIST(或类似)。然后,当您想使用MalCube()时,您只需从EMPTYLY列表中删除第一个元素并使用它。如果在EMPTYLY列表中没有元素,那么你就不走运(内存不足)。当你想免费()一个元素时,你只要把它插入到EMPTYLY列表的开头,这样你就可以在下次需要一个新元素的时候得到它。
以上来自于百度翻译 以下为原文 The elements of the list must be stored somewhere. If you want to avoid malloc(), you can create an array of CHObject which has enough elements to fit your needs. Then you link all your objects in the array into a list and call it empty_list (or similar). Then, when you want to use malloc() you just remove the first element from the empty_list and use it. If there's no elements in the empty_list you're out of luck (out of memory I mean). When you want to free() an element, you simply insert it into the beginning of the empty_list, so you could get it back next time you need a new element. |
|
|
|
如果他们使用一个数组,那么甚至连链表都不需要。这个问题可能会变成一个缓冲区或一个环形缓冲区。
以上来自于百度翻译 以下为原文 If they use an array then then linked list may not even be needed. The Problem may change to a Buffer, or a Ring Buffer. |
|
|
|
在Project属性中,我分配了64个堆空间,但不起作用。我想用一个数组来改变它,但是我之前的尝试失败了。我尝试创建一个最大对象数的CHObjt数组。由于CHObjt结构相当大,这导致链接错误。同样,我对实施没有100%的把握,因为很多事情必须改变。代码是一个第三方的BACnet栈,非常深入。现在我想做的就是让MALOC工作,这样我就可以继续开发,然后再把内存擦掉。关于MALOC有什么想法吗?
以上来自于百度翻译 以下为原文 In the project properties I have allocated 64 bytes of heap space, but that did not work. I would like to change this use an array but my earlier attempt failed. I attempted to make a CHobject array of some maximum number of items. As the CHobject structs are quite large this resulted in linker errors. As well I was not 100% sure about the implementation as a lot had to change. The code is a 3rd party bacnet stack and is quite in depth. For now what I would like to do it get the malloc working so I can continue to develop, then iron out the memory later. Any thoughts on the malloc? |
|
|
|
CHObjt结构相当大,将堆设为64字节?它必须足够大以容纳N*CHObjt,再加上OlHEAN多一点。我希望你的PIC有足够的RAM用于这个代码,它可能是为一个更大的CPU和更多的RAM而设计的。
以上来自于百度翻译 以下为原文 CHobject structs are quite large and you set your heap to 64 bytes? It must be big enough to hold N * CHobject plus a little more for overhean. I hope you PIC has enough RAM for this code that maybe designed for a Bigger CPU with More RAM |
|
|
|
它是一个烟囱,总是非常大。我想我可能不得不限制一个PIC可以控制的设备的数量。
以上来自于百度翻译 以下为原文 It is a bacnet stack, which is always quite large. I suppose I may have to limit the number of devices one pic can control. |
|
|
|
无论你做什么,你不能分配更多的内存比你的芯片提供(这是16K为您的芯片)。最好的方法是去掉泛型代码,编写自己的轻量级版本。要么就是这个,要么你需要一个更大的芯片。
以上来自于百度翻译 以下为原文 No matter what you do, you cannot allocate more memory than your chip provides (which is 16K for your chip). The best way is to get rid of the generic code and write your own lightweight version. It's either that or you need a bigger chip. |
|
|
|
无论你做什么,你不能分配更多的内存比你的芯片提供(这是16K为您的芯片)。最好的方法是去掉泛型代码,编写自己的轻量级版本。要么是这样,要么你需要一个更大的芯片。是的,我认为这是最终的解决方案。我希望有更容易的事情。
以上来自于百度翻译 以下为原文 No matter what you do, you cannot allocate more memory than your chip provides (which is 16K for your chip). The best way is to get rid of the generic code and write your own lightweight version. It's either that or you need a bigger chip. Yeah I was thinking that would be the eventual solution. I was hoping for something a bit easier though. |
|
|
|
在代码中发现了一个“GOTO”语句,它打破了自顶向下的编码方式。格林先生:我写了我自己的内存分配器,它是一个很好的代码块。释放MEM,它可以释放内存并加入内存备份,所以它总是线性的。检查前后的孔,并且可以通过改变页眉大小来重命名这些。没有内存移位。关键部分是头,它。只使用2字节。从该标题中可以找到PREV、这个、下一个、节大小以及内存是否在使用。比特15是一个标志,表示如果该部分内存是空闲的或在使用中。0到14位表示大小可以是2到65534(2字节对齐),这样就永远不能分配奇数个字节。它也可以检查错误。不是免费的内存,已经是免费的。难以置信的小,大小小于MalC/FL.。可以修改使用外部内存。写在16位芯片上。它是我的对象库的一部分。
以上来自于百度翻译 以下为原文 Spotted a "goto" statement in your code which breaks the top down approach to coding. mr green: I wrote my own memory allocator and it is a brilliant piece of coding. Freeing mem, it can free memory and join memory back up so it is always linear. Checks for holes before and after and can reclame these by changing the header size. No memory shifts involves. The key part is the header, it only uses 2 bytes. From that header the prev, this, next, section size and if the memory is in use can be found. Bit 15 is a flag that says if that part of memory is free or in use. 0-14 bits say the size which can be 2 - 65534 (2 byte alignment) So you can never allocate odd size_t bytes. It also error checks so you can not free memory that is already free etc. Unbelievably small in size, magnitude smaller than malloc/free. Could be modified to use external memory. Written on a 16bit chip. It's part of my object library. |
|
|
|
只有小组成员才能发言,加入小组>>
5234 浏览 9 评论
2026 浏览 8 评论
1950 浏览 10 评论
请问是否能把一个ADC值转换成两个字节用来设置PWM占空比?
3200 浏览 3 评论
请问电源和晶体值之间有什么关系吗?PIC在正常条件下运行4MHz需要多少电压?
2253 浏览 5 评论
771浏览 1评论
659浏览 1评论
有偿咨询,关于MPLAB X IPE烧录PIC32MX所遇到的问题
588浏览 1评论
PIC Kit3出现目标设备ID(00000000)与预期的设备ID(02c20000)不匹配。是什么原因
670浏览 0评论
571浏览 0评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-12-20 03:26 , Processed in 1.542835 second(s), Total 103, Slave 86 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号