Codeblocks与GCC不同的输出(奇怪的行为)?

时间:2015-01-22 15:29:29

标签: c gcc codeblocks

我试图回答这个question并在写完这段代码之后

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

int M =10;
int N =30;
int K = 10;

struct element {
    int *i;
    int *j;
    int k;
};

struct element *create_structure();
void print_element(struct element *);
int compare (const void *, const void * );
void sort(struct element *); // changed the return value of sort
// to void as the argument will be changed directly because it is a
// pointer


int main()
{
    srand(time(NULL));
    struct element *lista;
    lista=create_structure();
    printf("\n--------- i ---  j  ---------\n\n");
    print_element(lista);
    printf("\n---------------------------\n");

    sort(lista);
    print_element(lista);
    return 0;

}


struct element *create_structure()
{
    int aux1=0,count=0;
    struct element *structure;
    // Changed the allocation of structure pointer
    structure = (struct element *) malloc (sizeof(struct element));
    structure->k=K;
    structure->i= (int *)malloc(K*sizeof(int));
    structure->j=(int *)malloc (K*sizeof(int));
    for (count = 0; count < K; count ++)
    {
        aux1=rand()%N;
        // we kept only the first aux1 and copied it in the two arrays
        (structure->i)[count]=aux1;
        (structure->j)[count]=aux1;
    }
    return (structure);
}

void print_element(struct element *lista)
{
    int count=0;
    for(count = 0; count < K; count++)
    {
        printf("row=%2d :  %2d     %2d\n",count+1,(lista->i)[count],(lista->j)[count]);
    }
}


int compare(const void *a, const void *b)
{
    // compare the values of two case of array pointed by i of type int
    return *(int*)a-*(int*)b;
}


void sort(struct element *list)
{
  // we will sort the array pointed by i which contains K elements
  // of type int and size sizeof(int) by using the compare function
    qsort(list->i, K , sizeof(int), compare);
}

我意识到用终端的gcc命令编译它会得到正确的输出,但是使用相同内部GNU GCC编译器的代码块v13.12会产生错误的输出!!

gcc命令的输出:gcc -g -Wall main.c -o exec

gcc版本4.8.2(Ubuntu 4.8.2-19ubuntu1)

--------- i ---  j  ---------

row= 1 :   8      8
row= 2 :  29     29
row= 3 :   9      9
row= 4 :  11     11
row= 5 :  20     20
row= 6 :  21     21
row= 7 :   4      4
row= 8 :  16     16
row= 9 :   9      9
row=10 :   2      2

---------------------------
row= 1 :   2      8
row= 2 :   4     29
row= 3 :   8      9
row= 4 :   9     11
row= 5 :   9     20
row= 6 :  11     21
row= 7 :  16      4
row= 8 :  20     16
row= 9 :  21      9
row=10 :  29      2

代码块的输出(请注意起始行3的编号

row= 3 :  20     20
row= 4 :   4      4
row= 5 :  13     13
row= 6 :   8      8
row= 7 :   4      4
row= 8 :   7      7
row= 9 :  21     21
row=10 :  12     12

---------------------------
row= 1 :   4     24
row= 2 :   4     22
row= 3 :   7     20
row= 4 :   8      4
row= 5 :  12     13
row= 6 :  13      8
row= 7 :  20      4
row= 8 :  21      7
row= 9 :  22     21
row=10 :  24     12

enter image description here

所以我想知道是什么导致这种行为?

1 个答案:

答案 0 :(得分:0)

我已经尝试了我的Code :: Blocks(Windows 8,Code :: Blocks 13.12,GCC 4.7.1)中的代码,它正确地写出了输出!可能是,您的终端仿真器正在裁剪程序输出(或沿着这些行的某些东西)。我会将输出记录到文件中以确保。

发布截图后,我现在肯定了。