简体   繁体   English

浮动价值突然变得巨大

[英]Float value suddenly becoming huge

I would rather not dump code, but explain my problem. 我宁愿不转储代码,而是解释我的问题。 After hours of debugging I managed to understand that at some point in my code, a float value that is not explicitly modified turns HUGE (more than 1e15). 经过数小时的调试,我设法理解了代码中的某个点, 未经显式修改的浮点值将变为HUGE(大于1e15)。 I do use a lot of memory in my program (a string array containing 800+ words), other than that though, I have no idea what could cause this. 我确实在程序中使用了很多内存(包含800多个单词的字符串数组),除此之外,我不知道是什么原因引起的。

If anyone has any ideas regarding this, please share. 如果有人对此有任何想法,请分享。 Otherwise, I'll post a pastebin of the code soon. 否则,我将尽快发布该代码的pastebin。

EDIT: Here is the code: http://pastebin.com/vgiZweNq . 编辑:这是代码: http : //pastebin.com/vgiZweNq The problem rests in the next_generation() function, where the sumfit variable goes nuts at random times in the loop. 问题出在next_generation()函数中,该函数的sumfit变量在循环中的随机时间sumfit

Also, I've compiled this on linux using -fno-stack-limit and -fstack-check , to avoid stack overflows. 另外,我在Linux上使用-fno-stack-limit-fstack-check进行了编译,以避免堆栈溢出。

EDIT 2: I've changed the program to use a dynamically allocated linked list, to further avoid stack overflows. 编辑2:我已更改程序以使用动态分配的链表,以进一步避免堆栈溢出。 Still, sumfit gets changed to Floatzilla at random points, usually pretty early on. 尽管如此, sumfit还是在很早的时候就随机更改为Floatzilla。

Cheers! 干杯!

Since the variable is obviously being modified from an unexpected point, you might want to check some possibilities: 由于显然从意外的地方修改了该变量,因此您可能要检查一些可能性:

  • Is it being modified from a different thread or from an interrupt / event handler? 是从其他线程修改还是从中断/事件处理程序修改? If so, is the access properly synchronized to prevent a data race? 如果是这样,访问是否正确同步以防止数据争用?
  • Are you doing pointer arithmetic that might be buggy and cause access outside the intended buffer? 您是否正在执行可能有故障并导致在预期缓冲区之外进行访问的指针算术?
  • Are you casting pointers between types of different sizes? 您是否在不同大小的类型之间转换指针?
  • Especially if you are working on an embedded device: Maybe the memory is full and your stack is overlapping the heap, or the global variables. 尤其是在嵌入式设备上工作时:也许内存已满,并且堆栈与堆或全局变量重叠。

More information about the platform this happens on would be helpful. 有关发生这种情况的平台的更多信息将很有帮助。

You're using strcpy on the chrom array, but i don't see where they ever get null terminated. 您在chrom阵列上使用了strcpy,但是我看不到它们在哪里终止为null。

Maybe I'm just missing it, though. 不过,也许我只是想念它。

You've got a huge string array. 您有一个巨大的字符串数组。 I reckon you're probably going off the end of it. 我认为您可能快要结束了。 Keep track of the size of data going into that array. 跟踪进入该数组的数据大小。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM