C语言中如何防止内存覆盖

nihongsheng 2011-09-16 05:18:53
各位大侠.在编程中有如下问题:
1>我在一个char[12]里有一个字符数组,并初始化了,前面都是正确的.调试中发现在后面计算时把一个运算结果如 aa/100(aa是一个整形)赋给一个已声明浮点类型的浮点数时(即在等号的右边加上float类型转换符再给左边的浮点数,如果没有float类型转换则出来的是整除结果,小数点后面是0),出现前的的字符串后面加了一些莫名其妙的字符.
2>后面还有:为了防止发现这种情况,我把上面的同样的字符串拷入另一个备份的字符串.,在后面对另一个字符串去掉右空格时,调试中发现:随着循环的执行,备份的字符串后面也添加了一个不明字符,并随着循环的进行而不断变化.当然最后的结果也是不对的.
在后面加上"\0"好象也不中,查看内存地址,发现后面的内存地址再加上偏移量会覆盖掉前面的字符地址.
另外再说明一下:上述情况并不是每次都有的,象第一种情况,我在调另一个程序时,同样的代码,后面却不会出现乱字符.
请各位大侠指导一二,谢谢
...全文
249 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
AnYidan 2011-09-16
  • 打赏
  • 举报
回复
看看 IEEE745
pandahur 2011-09-16
  • 打赏
  • 举报
回复
Setting a Breakpoint When a Variable Changes Value
To set a breakpoint when a variable changes value

From the Edit menu, click Breakpoints.


Click the Data tab of the Breakpoints dialog box.


In the Expression text box, type the name of the variable.


Click OK to set the breakpoint.

--------------
Setting a Breakpoint When the Value at a Specified Memory Address Changes
To break when the value at a specified memory address changes

From the Edit menu, click Breakpoints.


Click the Data tab of the Breakpoints dialog box.


In the Expression text box, type the memory address for the byte.
For a word or doubleword memory address, enclose the address in parentheses, and precede it with a cast operator. For example, WO(21406036) for the word at memory location 21406036. Use the cast operator BY for a byte (optional), WO for a word, or DW for a doubleword. (The debugger interprets all integer constants as decimal unless they begin with zero (0) for octal or zero and x (0x) for hexadecimal.)

In the Number Of Elements text box, type the number of bytes, words, or doublewords to monitor. If you used the BY operator in the Expression field, specify the number of bytes. If you used WO, specify the number of words. If you used DW, specify the number of doublewords.


Click OK to set the breakpoint.
cfvmario 2011-09-16
  • 打赏
  • 举报
回复
浮点数比整数占内存长
当把整数强制转换为浮点数,实质是把那个整数所占的内存加上了后面内存的一些随机数据解释为一个浮点数,所以会有乱字符是后面的随机数据
AndyZhang 2011-09-16
  • 打赏
  • 举报
回复
强制转换,要么就用double
赵4老师 2011-09-16
  • 打赏
  • 举报
回复
Setting a Breakpoint When a Variable Changes Value
To set a breakpoint when a variable changes value

From the Edit menu, click Breakpoints.


Click the Data tab of the Breakpoints dialog box.


In the Expression text box, type the name of the variable.


Click OK to set the breakpoint.

--------------
Setting a Breakpoint When the Value at a Specified Memory Address Changes
To break when the value at a specified memory address changes

From the Edit menu, click Breakpoints.


Click the Data tab of the Breakpoints dialog box.


In the Expression text box, type the memory address for the byte.
For a word or doubleword memory address, enclose the address in parentheses, and precede it with a cast operator. For example, WO(21406036) for the word at memory location 21406036. Use the cast operator BY for a byte (optional), WO for a word, or DW for a doubleword. (The debugger interprets all integer constants as decimal unless they begin with zero (0) for octal or zero and x (0x) for hexadecimal.)

In the Number Of Elements text box, type the number of bytes, words, or doublewords to monitor. If you used the BY operator in the Expression field, specify the number of bytes. If you used WO, specify the number of words. If you used DW, specify the number of doublewords.


Click OK to set the breakpoint.
nice_cxf 2011-09-16
  • 打赏
  • 举报
回复
估计你字符串用满了12个字节,如果是这样的话声明时应该用char[13],并且加上example[12]=\0;
字符串声明时要为结束符分配空间
龙行天下之Sky 2011-09-16
  • 打赏
  • 举报
回复
aa/100(aa是一个整形)
这样只能得出整型的结果
aa/100.0(aa是一个整形)
这样可以得出浮点数
由于float和double的精度问题,比如123/100.0得出的结果不一定等于1.23可能是1.229999999等等
xwfde 2011-09-16
  • 打赏
  • 举报
回复
内存对齐问题

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧