是什么导致我的代码具有不确定性?
我可能已经疯了,但我不断重新编译完全相同相同的代码,并得到不同的答案。我根本没有使用任何随机值。我严格保留浮点数和一维数组(我想最终将其移植到 CUDA)。
在编译器方面是否有可能以某种方式重做我的相同代码,使其根本无法工作?
我只需单击 .exe 即可运行它,它运行良好,但是当我单击“编译并运行”(Dev C++ 4.9.9.2) 时,我的图像都没有正确显示。 ...尽管有时他们会这样做。
...关于如何解决这个问题有什么见解吗?如果我可以提供更多帮助,请告诉我。
非常感谢。
编辑: 这是代码块,如果我将其注释掉,一切都会正常运行。 (如果我评论这个块,它是完全确定性的) -这是一个电磁模拟器,如果这有帮助的话:
//***********************************************************************
// Update HZ in PML regions (hzx,hzy)
//***********************************************************************
boundaryIndex = 0;
for (regionIndex = 1; regionIndex < NUMBEROFREGIONS; regionIndex++) {
xStart = regionData[regionIndex].xStart;
xStop = regionData[regionIndex].xStop ;
yStart = regionData[regionIndex].yStart;
yStop = regionData[regionIndex].yStop ;
for (i = xStart; i < xStop; i++) {
for (j = yStart; j < yStop; j++) {
hzx = hz[i*xSize+j] - hzy[boundaryIndex]; // extract hzx
hzx = dahz[i*xSize+j] * hzx + dbhz[i*xSize+j] * ( ey[i*(xSize+1)+j] - ey[(i+1)*(xSize+1)+j] ); // dahz,dbhz holds dahzx,dbhzx
hzy[boundaryIndex] = dahzy[boundaryIndex] * hzy[boundaryIndex] + dbhzy[boundaryIndex] * ( ex[i*ySize+j+1] - ex[i*ySize+j] );
hz[i*xSize+j] = hzx + hzy[boundaryIndex]; // update hz
boundaryIndex++;
} //jForLoop /
} //iForLoop /
} //
其中,NUMBEROFREGIONS 是常量 (8),Xsize 在编译时定义(此处为 128)。
I might've gone crazy here, but I keep recompiling the exact same code, and get different answers. I'm not using any random values at all. I am strictly staying to floats and 1D arrays (I want to port this to CUDA eventually).
Is it possible on the compiler side that my same code is being redone in a way that makes it not work at all?
I run the .exe by just clicking on it and it runs fine, but when I click "compile and run" (Dev C++ 4.9.9.2) none of my images come out right. ...although sometimes they do.
...any insight on how I fix this? If I can provide any more help please tell me.
Much Appreciated.
Edit:
Here's the block of code that if I comment it out, everything runs sort of right. (Its completely deterministic if I comment this block out)
-this is a electromagnetic simulator, if that helps at all:
//***********************************************************************
// Update HZ in PML regions (hzx,hzy)
//***********************************************************************
boundaryIndex = 0;
for (regionIndex = 1; regionIndex < NUMBEROFREGIONS; regionIndex++) {
xStart = regionData[regionIndex].xStart;
xStop = regionData[regionIndex].xStop ;
yStart = regionData[regionIndex].yStart;
yStop = regionData[regionIndex].yStop ;
for (i = xStart; i < xStop; i++) {
for (j = yStart; j < yStop; j++) {
hzx = hz[i*xSize+j] - hzy[boundaryIndex]; // extract hzx
hzx = dahz[i*xSize+j] * hzx + dbhz[i*xSize+j] * ( ey[i*(xSize+1)+j] - ey[(i+1)*(xSize+1)+j] ); // dahz,dbhz holds dahzx,dbhzx
hzy[boundaryIndex] = dahzy[boundaryIndex] * hzy[boundaryIndex] + dbhzy[boundaryIndex] * ( ex[i*ySize+j+1] - ex[i*ySize+j] );
hz[i*xSize+j] = hzx + hzy[boundaryIndex]; // update hz
boundaryIndex++;
} //jForLoop /
} //iForLoop /
} //
where, NUMBEROFREGIONS is constant (8), Xsize is defined at compile time (128 here).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
正如每个人所说,如果没有一些错误代码,我们无法为您提供很多帮助。
从您刚刚解释的内容中我得到的最好的结果是您在未分配的内存上创建指针。
像这样的东西
这里
p
是一个指向aFunction中局部变量的指针,并且一旦离开函数就被销毁,有时纯粹的运气仍然指向没有'的正确数据没有被重写,但是这个内存空间最终会被改变,你的指针将完全随机地读取不同的东西。As everybody said without some code of what is wrong we can't help you a lot.
My best gest from what you just explained is that your creating pointers on non allocated memory.
something like this
Here
p
is a pointer to something that was a local varaible in aFunction and got destroyed as soon as it left the function, this will sometime by PURE LUCK still point to the right data that hasn't been written over, but this memory space will eventual be changed and your pointer will be reading something different completly random.一些代码示例会有所帮助!但这是未初始化变量的典型症状。
您没有设置一些重要的变量(索引为 0、切换为 True 等),因此您的程序会在每次运行时选取内存中悬挂的值。
由于这些实际上是随机值,因此每次都会得到不同的结果。
Well some code examples would help! But this is a classic symptom of un-initialized variables.
You are not setting some important variables (indexes to 0, switches to True etc.) so your program picks up whichever values are hanging around in memory each time you run.
As these are effectively random values you get different results each time.
您的模拟二维数组是否存在索引错误?
ey
应该是 xSize 还是 xSize+1 宽?您的索引将 2D 数组
ey
视为 xSize+1 宽。数组ex
的代码将其视为 ySize 宽。Is there an indexing error with your simulated two-dimensional array? Is
ey
supposed to be xSize or xSize+1 wide?Your index treats 2D array
ey
as being xSize+1 wide. The code for arrayex
treats it as being ySize wide.您可能会调用未定义的行为。 C 语言标准未定义许多内容。其中一些情况可以被编译器捕获,并且可能会向您发出诊断信息,而其他情况则编译器很难捕获。以下是一些具有未定义行为的事情:
尝试使用未初始化变量的值:
在序列点之间多次修改对象:
读取或写入超过已分配内存块的末尾:
使用
printf
等。但提供了错误的格式说明符:将值转换为有符号整数类型,但该类型无法表示该值(有人说这是实现定义的,而 C 语言规范似乎不明确):
编辑:
在 OP 提供代码之前回答。
You are potentially invoking undefined behaviour. There are a number of things that are undefined by the C language standard. Some of these cases can be caught by the compiler and you may be issued a diagnostic, others are harder for the compiler to catch. Here is just a few things that have undefined behaviour:
Trying to use the value of an uninitialised variable:
An object is modified more than once between sequence points:
Reading or writing past the end of an allocated memory block:
Using
printf
et. al but providing the wrong format specifiers:Converting a value to a signed integer type but the type cannot represent the value (some say this is implementation defined and the C language specification seems ambiguous):
Edit:
Answered before OP provided code.
你是在调试模式还是发布模式下编译它?其中每一种都有不同的初始化堆和内存的方式。
Are you compiling it in debug mode or release mode? Each one of these have different way how they initialize the heap and memory.