变量任意变化值

时间:2017-05-10 18:51:59

标签: c linux gcc

所以,今天我在C中实现了一个插入排序算法,我发现自己遇到了THIS (video)奇怪的bug。

整数变量arraySize只是在循环中改变它自己的值,它甚至不会每次都发生,它完全是随机的,而且我改变arraySize值的唯一时间是在当我使用值10初始化时,代码的开始。

代码:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>


int main () {
  int y = 0, arraySize = 10, array[1000];
  srand(time(NULL));
  for (int i = 0; i < arraySize; i++) {
    array[i] = rand() % 1000;
  }
  for (int i = 1; i < arraySize; i++) {
    for (int j = i; array[j] <= array[j - 1]; j--) {
      y = array[j - 1];
      array[j - 1] = array[j];
      array[j] = y;
    }
    printf("for externo i = %d, arraySize = %d \n", i, arraySize);
  }
  for (int i = 0; i < arraySize; i++) {
    printf("%d. i = %d ", array[i], i);
  }
  printf("\n");
  return 0;
}

使用简单的&#34; gcc insertionSort.c&#34;

编译后,这个确切程序给出的输出示例
for externo i = 1, arraySize = 10 
for externo i = 2, arraySize = 10 
for externo i = 3, arraySize = 10 
for externo i = 4, arraySize = 5 
10. i = 0 22. i = 1 233. i = 2 343. i = 3 592. i = 4

for externo i = 1, arraySize = 10 
for externo i = 2, arraySize = 10 
for externo i = 3, arraySize = 10 
for externo i = 4, arraySize = 10 
for externo i = 5, arraySize = 10 
for externo i = 6, arraySize = 10 
for externo i = 7, arraySize = 10 
for externo i = 8, arraySize = 10 
for externo i = 9, arraySize = 10 
54. i = 0 239. i = 1 312. i = 2 313. i = 3 438. i = 4 465. i = 5 827. i = 6 839. i = 7 874. i = 8 935. i = 9

for externo i = 1, arraySize = 10 
for externo i = 2, arraySize = 10 
for externo i = 3, arraySize = 10 
for externo i = 4, arraySize = 10 
for externo i = 5, arraySize = 10 
for externo i = 6, arraySize = 10 
for externo i = 7, arraySize = 10 
for externo i = 8, arraySize = 10 
for externo i = 9, arraySize = 6 
10. i = 0 58. i = 1 135. i = 2 316. i = 3 411. i = 4 442. i = 5

我使用的是xubuntu 16.04,这些是我的gcc配置:

使用内置规格。 COLLECT_GCC = GCC COLLECT_LTO_WRAPPER = / usr / lib中/ GCC / x86_64的-Linux的GNU / 5 / LTO-包装 目标:x86_64-linux-gnu 配置为:../ src / configure -v --with-pkgversion =&#39; Ubuntu 5.4.0-6ubuntu1~16.04.4&#39; --with-bugurl = file:///usr/share/doc/gcc-5/README.Bugs --enable-languages = c,ada,c ++,java,go,d,fortran,objc,obj-c ++ - -prefix = / usr --program-suffix = -5 --enable-shared --enable-linker-build-id --libexecdir = / usr / lib --without-included-gettext --enable-threads = posix - -libdir = / usr / lib --enable-nls --with-sysroot = / --enable -clocale = gnu --enable-libstdcxx-debug --enable-libstdcxx-time = yes --with-default-libstdcxx- abi = new --enable-gnu-unique-object --disable-vtable-verify --enable-libmpx --enable-plugin --with-system-zlib --disable-browser-plugin --enable-java-awt = gtk --enable-gtk-cairo --with-java-home = / usr / lib / jvm / java-1.5.0-gcj-5-amd64 / jre --enable-java-home --with-jvm- root-dir = / usr / lib / jvm / java-1.5.0-gcj-5-amd64 --with-jvm-jar-dir = / usr / lib / jvm-exports / java-1.5.0-gcj-5 -amd64 --with-arch-directory = amd64 --with-ecj-jar = / usr / share / java / eclipse-ecj.jar --enable-objc-gc --enable-multiarch --disable-werror - with-arch-32 = i686 --with-abi = m64 --with-multilib-list = m32,m64,mx32 --enable-multilib --with -tune = generic --enable-checking = release --build = x86_64-linux-gnu --host = x86_64-linux-gnu --target = x86_64-linux-gnu 线程模型:posix gcc版本5.4.0 20160609(Ubuntu 5.4.0-6ubuntu1~16.04.4)

2 个答案:

答案 0 :(得分:1)

在这一行 -

for (int j = i; array[j] <= array[j - 1]; j--)  

j可能会小于0,这会导致超出限制的访问导致未定义的行为。

我不想从UB中理解,但是你最终会改变堆栈上的其他值,例如arraySize

一个简单的解决方法是 -

for (int j = i; j > 0 && array[j] <= array[j - 1] ; j--) //Short circuiting prevents UB here. 

而不是上面提到的那一行。

现在这个条件是否适合你的逻辑,那是你要弄明白的。

答案 1 :(得分:0)

这个循环:

for (int j = i; array[j] <= array[j - 1]; j--) {

可以运行数组的开头(j变得小于0),导致未定义的行为。即使j正好为零,评估循环终止条件也会导致UB全部出现,但是更改另一个变量值的最可能的罪魁祸首是对数组执行的越界修改循环体。

您需要确保排序停在数组的开头。