完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
您好,我正在编写一些代码,在函数中我使用了一些本地变量:无效的TOTO(空隙){双A,B;双A1,B1,RES;…}我的代码工作得很好,但是变量A,B在调用函数时保持以前的值。显然这是我的应用程序中需要的,所以当我意识到T时,我感到困惑。尽管这些变量被声明为本地的,它们保留了内存……我查看了我的固件X.debug.map并注意到:TOTO @ A BSSBANK0 0 000 E0 toto @ A1 CSTACKBANK0 0 000 A0 toto1,B BSSBANK0 0 0 0 DDTO to @ B1 CSTACKBANK0 0 000 95。真诚地,
以上来自于百度翻译 以下为原文 Hello, I'm writing some code and on a function I use some local varables: void toto(void){My code works well, but variables a, b keep memory of previous values when the function is called. Obviously it's what I need in my application so I was confused when I realise that altough the fact those variables were declared as local they keep memories... I look into my firmware X.debug.map and noticed this: toto@a bssBANK0 0000E0Maybe somebody can tell about this behavior and those memory storages. Sincerely, |
|
相关推荐
13个回答
|
|
这可能是由于XC8所使用的“编译堆栈”。在任何情况下,单位化变量都具有“随机”值,而“以前”是随机的,如果您希望它们在任何时候被设置在某个给定值上(即输入的函数),那么就给它们写一个值。如果你希望他们在功能结束时保持他们的价值,让他们保持静态。
以上来自于百度翻译 以下为原文 This may due to the "compiled stack" used by XC8. In any case, unitialized variables have "random" values, and "previous" is one of the random If you want them to be set at some given value any time the code is used (i.e. function entered), write a value to them. If you want them to prserve their values when the function ends, make them static. |
|
|
|
我猜你之所以懒惰,是因为全局变量在启动时被归零。永远不要假设一个未初始化的变量在写入之前会有任何特定的值。
以上来自于百度翻译 以下为原文 I guess you've got lazy because global variables are zeroed on startup. Never assume a variable that you have not initialised is going to have any particular value before you write to it. |
|
|
|
显然,“编译堆栈”概念的副作用(我将一些内存地址分配给每个“堆栈变量”)。我不确定XC8是否会尝试在内存耗尽之前重用这些内存位置,因此我建议为本地变量添加“static”,以确保在后续调用之间保持值。
以上来自于百度翻译 以下为原文 Obviously a side-effect of the concept of the "compiled stack" (which i assigning some memory address to each "stack variable"). I'm not sure whether XC8 might try to "reuse" such memory locations prior running out of RAM, thus I suggest to add 'static' for local variables you want to make sure to keep the values between subsequent calls. |
|
|
|
我会的。我也会花时间阅读XC8用户指南。谢谢你的回答。
以上来自于百度翻译 以下为原文 I'll do. I'll also take time to read the XC8 user guide. Thanks for your answers. |
|
|
|
这也可能发生在一个真正的堆栈上。但是这种有趣的副作用可能随着代码的改变而消失。这可能只是时间的一部分。使用它作为一个特性将只是等待它成为一个bug。
以上来自于百度翻译 以下为原文 It is possible that would happen with a real stack too. But this interesting side effect could disappear with a change of code. It may only be true part of the time. Using it as a feature would be just waiting for it to become a bug. |
|
|
|
只要这些RAM位置不与其他函数共享,HIST值就会发生。事实上,这些值被保留了,这让我怀疑您使用的是一个小的测试程序。如果你有一个简单的测试程序,那么本地变量所用的堆栈框架将在函数的每个调用中驻留在同一个地址上。
以上来自于百度翻译 以下为原文 Hi This value retension will happen as long as those RAM locations are not shared with other functions. The fact that the values are retained make me suspect that you have are using a small test program. The same efect can happen also with a real stack. If you have a simple test program chances are that the stack frame used for the local variables will reside in the same address on every call of the function. Just my 2 cents.... Best regards Jorge |
|
|
|
你什么意思?你在1个帖子里说了2件事。全局变量设置为零(好吧,我不知道这一点,保存一些ROM也写了0),你确定这个吗?然后你说:永远不要假设一个未初始化的变量在写入之前会有任何特定的值。如果它设置为零,那么我可以假定它是什么?
以上来自于百度翻译 以下为原文 What do you mean ?, you say 2 things in 1 post. Globals are set to zero ( alright, i did not know this, saves some ROM to write a zero also ), are you sure about this ? Then you say : Never assume a variable that you have not initialised is going to have any particular value before you write to it. If its set to zero, i can assume that then ? |
|
|
|
全局变量被设置为零,而不是局部变量(并且取决于编译器,这种行为也可能受到某些选项的影响)。
以上来自于百度翻译 以下为原文 GLOBALS are set to zero, not locals (and depending upon compiler, that behaviour may be subject to some options too...) |
|
|
|
|
|
|
|
例如,在C18中,您可以使用一个不同的链接器文件,以便只在显式初始化时将它们初始化为全局变量(不必初始化它们)(我想,在最近的编译器中,它们总是被初始化为零)。
以上来自于百度翻译 以下为原文 For example, in C18 you could use a different linker file in order to choose
|
|
|
|
你什么意思?你在1个帖子里说了2件事。全局变量被设置为零(好吧,我不知道这一点,保存一些ROM来写一个0也不可能。)你确定吗?在符合ASNI的编译器中,它是正确的。静力学。然后你说:永远不要假设一个你没有初始化的变量在写入之前会有任何特定的值。这是个好习惯。加Ex int x=0;不使用任何添加内存或代码。它是多余的。但它可以确保你考虑它,后续程序可以看到它。在代码的不同部分做不同的事情会引起错误。如果它设置为零,那么我可以假设?对于全局和静态,是的,但假设是额外的工作。
以上来自于百度翻译 以下为原文 What do you mean ?, you say 2 things in 1 post. Globals are set to zero ( alright, i did not know this, saves some ROM to write a zero also NO It may not.), are you sure about this ? In an ASNI Compliant Compiler it is true. Statics Also. Then you say : Never assume a variable that you have not initialised is going to have any particular value before you write to it. This is good Practice. Plus ex int X = 0; will not use any adding memory or code. it is redundant. But it insure you thought about it, Follow up programs can see it. doing different things in different parts of the code invites mistakes. If its set to zero, i can assume that then ? For Globals and Statics Yes, But assuming is extra work. |
|
|
|
HI,8位PICS没有变量堆栈,所以本地变量存储在RAM中,因为这些设备具有哈佛体系结构和只包含地址的硬件堆栈。根据您的程序,它们可能是与先前调用中的那些相同的位置。变量被标记为静态的,然后编译器确保它们有一个永久RAM位置,并且它们在函数调用之间保持它们的值。即使在没有被标记为静态的情况下,局部变量仍保持其值的事实只是一个“意外事件”,并且可以在发生更改时容易地改变。如果你需要它们来保持它们的价值,只需将它们标记为静态的,否则不要假设它们保留了先前的值。
以上来自于百度翻译 以下为原文 Hi, 8 bit PICs have no stack for variables so local variables are stored in RAM because these devices have an Harvard architecture and an hardware stack which only contains addresses. Depending upon your program it could be that they end up in the same locations as the ones in previous calls. If local variables are tagged static, then the compiler make sure they have a permanent RAM location AND that they retain their value between function calls. The fact that a local variable retain its value even when it is not tagged static is just an "accident" and could easily change as you make changes to you program or when you will optimize it. So, if you need them to retain their value just tag them static, otherwise don't assume that they do retain their previous value Regards |
|
|
|
“Alram”初始化是严格讲的——不是编译器的一个特性或选项:它通常是在一些RAM测试期间由启动代码完成的。因此,它通常不局限于全局变量。如果C18链接器有一些开关,那么这很好,但这与我的声明不矛盾:它是最有可能在启动代码块的不同变种之间切换的。
以上来自于百度翻译 以下为原文 @ all RAM initialization is - strictly spoken - not a feature or option of the compiler: it is usually done by the startup code during some RAM test. Thus it usually is not restricted to global variables. Nice if the C18 linker had some switches but this does not contradict my statement: it is the linker - most likely switching between different variants of the startup code blocks. |
|
|
|
只有小组成员才能发言,加入小组>>
5242 浏览 9 评论
2031 浏览 8 评论
1955 浏览 10 评论
请问是否能把一个ADC值转换成两个字节用来设置PWM占空比?
3207 浏览 3 评论
请问电源和晶体值之间有什么关系吗?PIC在正常条件下运行4MHz需要多少电压?
2256 浏览 5 评论
778浏览 1评论
669浏览 1评论
有偿咨询,关于MPLAB X IPE烧录PIC32MX所遇到的问题
597浏览 1评论
PIC Kit3出现目标设备ID(00000000)与预期的设备ID(02c20000)不匹配。是什么原因
679浏览 0评论
577浏览 0评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-12-24 04:11 , Processed in 1.356879 second(s), Total 105, Slave 88 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号