gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 17:22:08 25 4
gpt4 key购买 nike

我试图回答这个问题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

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

最佳答案

我已经尝试了 Code::Blocks 中的代码(Windows 8、Code::Blocks 13.12、GCC 4.7.1),它正确写入输出!您的终端模拟器可能正在裁剪程序输出(或类似的内容)。我会将输出记录到一个文件中以确保。

在您发布屏幕截图后,我现在确信这一点。

关于Codeblocks vs GCC 不同的输出(奇怪的行为)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28092565/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com