为什么我的C代码每次都不起作用?

Nit*_*shu 1 c arrays sorting merge gcc

我写了这段代码来合并两个排序的数组.所需的输出是:

Merged array:0 1 2 3 4 5 6 7 8 9 10 11 12
Run Code Online (Sandbox Code Playgroud)

我正在使用gcc(Ubuntu 5.4.0-6ubuntu1~16.04.4)5.4.0 20160609来编译我的代码.

问题是,当我执行a.out文件时有时会得到所需的输出,但在其他情况下,光标会一直闪烁并且没有显示结果.为什么会这样?我的代码有问题吗?

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

int main(void){

//change arrays as per your need but it should be sorted
int a[] = {1,2,3,7,8};
int b[] = {0,3,5,6,9,10,11,12};

int m =sizeof(a) / sizeof(int);
int n =sizeof(b) / sizeof(int);

int index=0, j=0, k=0;
int size = m + n;
int array[size];


while(index < size) {

    while(a[j] < b[k] && j<m ){
        array[index] = a[j];
        ++index;
        ++j;
    }
    while(a[j] > b[k] && k<n){
        array[index] = b[k];
        ++index;
        ++k;            
    }
    while(a[j] == b[k]){
        array[index] = a[j];
        j++; index++;            
    }        
}

printf("Merged array: ");
for(int i=0; i<size; i++)
    printf("%d ", array[i]);

printf("\n");

}
Run Code Online (Sandbox Code Playgroud)

Ant*_*ala 6

您有未定义的行为(访问数组越界).使用gcc -fsanitize=undefined创建可以检测各种不良行为的可执行文件.

% gcc -g fffff.c -Wall -Wextra -fsanitize=undefined
% ./a.out
fffff.c:20:12: runtime error: index 5 out of bounds for type 'int [5]'
fffff.c:20:12: runtime error: load of address 0x7ffd0c0c9804 with insufficient space for an object of type 'int'
0x7ffd0c0c9804: note: pointer points here
  08 00 00 00 04 00 00 00  04 00 00 00 04 00 00 00  00 00 00 00 03 00 00 00  05 00 00 00 06 00 00 00
              ^ 
fffff.c:25:12: runtime error: index 5 out of bounds for type 'int [5]'
fffff.c:25:12: runtime error: load of address 0x7ffd0c0c9804 with insufficient space for an object of type 'int'
0x7ffd0c0c9804: note: pointer points here
  08 00 00 00 04 00 00 00  04 00 00 00 04 00 00 00  00 00 00 00 03 00 00 00  05 00 00 00 06 00 00 00
              ^ 
fffff.c:30:12: runtime T: index 5 out of bounds for type 'int [5]'
fffff.c:30:12: runtime error: load of address 0x7ffd0c0c9804 with insufficient space for an object of type 'int'
0x7ffd0c0c9804: note: pointer points here
  08 00 00 00 04 00 00 00
Run Code Online (Sandbox Code Playgroud)

线20,25和30是

20      while(a[j] < b[k] && j<m ){

25      while(a[j] > b[k] && k<n){

30      while(a[j] == b[k]){
Run Code Online (Sandbox Code Playgroud)


gsa*_*ras 5

我的代码有问题吗?

是!

访问时它会超出范围a,例如:

while(a[j] < b[k] && j<m ){
    array[index] = a[j];
    ++index;
    ++j;
}
Run Code Online (Sandbox Code Playgroud)

j最终将得到值4,输入if语句的主体,当它尝试解析while循环的条件时,它将访问a[5],这是超出界限,从而导致未定义的行为(这解释了为什么你的代码运行有时,和其他人挂起).

您可以通过将while循环的条件更改为此来让短路帮助您:

while(j < m && a[j] < b[k]) {
Run Code Online (Sandbox Code Playgroud)

j到达mm时,导致被j<m评估为false,将不会通过a[j] < b[k],因为如果其操作数中的至少一个为假,则逻辑和操作将为假.

在你的下一个while循环中也是如此.所以改成它:

while(k < n && a[j] > b[k]) {
Run Code Online (Sandbox Code Playgroud)

最后但并非最不重要的是,最后一个while循环的条件:

while(a[j] == b[k]){
Run Code Online (Sandbox Code Playgroud)

还将调用Undefined Behavior,因为j它将等于5,并且k等于8.

将其更改为:

while(j < m && k < n && a[j] == b[k]) {
Run Code Online (Sandbox Code Playgroud)

将阻止调用未定义的行为.