不同的编译器,不同的输出? [英] Different compilers, different output?

查看:215
本文介绍了不同的编译器,不同的输出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些C代码,我一直在我的Mac上的Xcode。然后我想在Windows机器上使用它,并用TinyC编译它。当我运行它,输出是不同的。



这可能是由于使用不同的编译器?



谢谢!






编辑1



它会打开一个wav文件,将所有样本转换为数组。

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



void read_wav_header(unsigned int * samp_rate,unsigned int * bits_per_samp,
unsigned int * num_samp);
void read_wav_data(int * data,unsigned int samp_rate,
unsigned int bits_per_samp,unsigned int num_samp);
int conv_bit_size(unsigned int in,int bps);



int main(void)
// int read_wav(void)
{


unsigned int samp_rate,bits_per_samp,num_samp;
read_wav_header(& samp_rate,& bit_per_samp,& num_samp);

printf(samp_rate = [%d] bits_per_samp = [%d] num_samp = [%d] \\\

samp_rate,bits_per_samp,num_samp);

int * data =(int *)malloc(num_samp * sizeof(int));
read_wav_data(data,samp_rate,bits_per_samp,num_samp);

unsigned int i;
// for(i = 0; i
for(i = 0; i <100; ++ i){
printf(%d \\\
,data [i]);
}

return EXIT_SUCCESS;
}

void read_wav_header(unsigned int * samp_rate,unsigned int * bits_per_samp,
unsigned int * num_samp)
{
unsigned char buf [5] ;

// freopen(/Users/ericbrotto/Desktop/iPhoneData/tmp/Hack.wav\",\"r\",stdin);

freopen(C:/ Documents and Settings / Eric.Brotto / Desktop / Eric_Other / Files / Hack.wav,r,stdin)



/ * ChunkID(RIFF为小端,RIFX为大端)* /
fread(buf,1,4,stdin);
buf [4] ='\0';
if(strcmp((char *)buf,RIFF))exit(EXIT_FAILURE);


/ * ChunkSize * /
fread(buf,1,4,stdin);

/ *格式* /
fread(buf,1,4,stdin);
buf [4] ='\0';

printf(IS THIS WAVE? - >%s< - \\\
,(char *)buf);


if(strcmp((char *)buf,WAVE))exit(EXIT_FAILURE);

/ * Subchunk1ID * /
fread(buf,1,4,stdin);
buf [4] ='\0';
printf(IS THIS fmt? - >%s< - \\\
,(char *)buf);

if(strcmp((char *)buf,fmt))exit(EXIT_FAILURE);

/ * Subchunk1Size(PCM为16)* /
fread(buf,1,4,stdin);
if(buf [0]!= 16 || buf [1] || buf [2] || buf [3])exit(EXIT_FAILURE);

/ * AudioFormat(PCM = 1,其他值表示压缩)* /
fread(buf,1,2,stdin);
if(buf [0]!= 1 || buf [1])exit(EXIT_FAILURE);

/ * NumChannels(Mono = 1,Stereo = 2,etc)* /
fread(buf,1,2,stdin);
unsigned int num_ch = buf [0] +(buf [1] <8);
if(num_ch!= 1)exit(EXIT_FAILURE);

/ * SampleRate(8000,44100等)* /
fread(buf,1,4,stdin);
* samp_rate = buf [0] +(buf [1] <8)+
(buf [2] <16)+(buf [3] <24 );

/ * ByteRate(SampleRate * NumChannels * BitsPerSample / 8)* /
fread(buf,1,4,stdin);
const unsigned int byte_rate = buf [0] +(buf [1] <8)+
(buf [2] <16)+(buf [3] ; 24);

/ * BlockAlign(NumChannels * BitsPerSample / 8)* /
fread(buf,1,2,stdin);
const unsigned int block_align = buf [0] +(buf [1] <8);

/ * BitsPerSample * /
fread(buf,1,2,stdin);
* bits_per_samp = buf [0] +(buf [1] <8);

if(byte_rate!=((* samp_rate * num_ch * * bits_per_samp)> 3))
exit(EXIT_FAILURE);

if(block_align!=((num_ch * * bits_per_samp)> 3))
exit(EXIT_FAILURE);



/ * Subchunk2ID * /
// fread逐行读取直到结束。

fread(buf,1,4,stdin);
buf [4] ='\0';

if(strcmp((char *)buf,data))exit(EXIT_FAILURE);



/ * Subchunk2Size(NumSamples * NumChannels * BitsPerSample / 8)* /
fread(buf,1,4,stdin)

const unsigned int subchunk2_size = buf [0] +(buf [1] <8)+
(buf [2] <16)+ ] <24);
* num_samp =(subchunk2_size<<< 3)/(
num_ch * * bits_per_samp);
}


void read_wav_data(int * data,unsigned int samp_rate,
unsigned int bits_per_samp,unsigned int num_samp)
{

// freopen(/Users/ericbrotto/Desktop/iPhoneData/tmp/Hack.wav\",\"r\",stdin);
freopen(C:/ Documents and Settings / Eric.Brotto / Desktop / Eric_Other / Files / Hack.wav,r,stdin);

unsigned char buf;
unsigned int i,j;
for(i = 0; i unsigned int tmp = 0;
for(j = 0; j!= bits_per_samp; j + = 8){
fread(& buf,1,1,stdin);
tmp + = buf<< j;
}
data [i] = conv_bit_size(tmp,bits_per_samp);
}
}


int conv_bit_size(unsigned int in,int bps)
{
const unsigned int max =(1< <(bps-1)) - 1;
return in> max? in - (max<< 1):in; //假设这不正确:http://ubuntuforums.org/showthread.php?p=10442711
//返回> max? in - ((max< 1)+2):in;

}






strong> EDIT 2



在我的mac上,它输出数组中的所有样本(int大约在-32000到32000之间)。这里我得到你在图像中看到的输出,后面是几百万个零。



解决方案

即使假设两个编译器都符合ISO标准(这不一定是这样),那么该标准仍然有很多余地。



例如,如果你的程序使用实现定义或语言环境特定的行为,输出可以不同。



如果编写程序的人使用未定义的行为,那么这也是不同输出的可能。 >

您最好的选择是向我们展示正确分析的代码。



如果您对可能不同的内容, C99 的附录J(您可以使用TC1,2和3下载草稿从页面底部 - 它和最终产品之间的差异是最小的)列出了可移植性问题(未指定,未定义,实现定义和特定于语言环境的行为)。



<你> 可能需要小心。这可能不适用于Tiny C,但我知道我使用的Microsoft编译器是那个类rrb fopen / freopen 中的处理方式不同。如果你只是指定r,翻译就会发生,这可能会给你一个二进制文件错误的数据,如 wav 文件。


I have a bit of c code that I've been working on in Xcode on my mac. I then wanted to work with it on a Windows machine and compile it with TinyC. When I run it, the output is different.

Is it possible that this is due to using different compilers?

Thanks!


EDIT 1

The code is a simple script that opens up a wav file to throw all the samples into an array.

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



void read_wav_header(unsigned int *samp_rate, unsigned int *bits_per_samp,
                 unsigned int *num_samp);
void read_wav_data(int *data, unsigned int samp_rate,
               unsigned int bits_per_samp, unsigned int num_samp);
int conv_bit_size(unsigned int in, int bps);



int main(void)
// int read_wav(void)
{


unsigned int samp_rate, bits_per_samp, num_samp;
read_wav_header(&samp_rate, &bits_per_samp, &num_samp);

printf("samp_rate=[%d] bits_per_samp=[%d] num_samp=[%d]\n",
       samp_rate, bits_per_samp, num_samp);

int *data = (int *) malloc(num_samp * sizeof(int));
read_wav_data(data, samp_rate, bits_per_samp, num_samp);

unsigned int i;
// for (i = 0; i < num_samp; ++i) {

for (i = 0; i < 100; ++i) {
    printf("%d\n", data[i]);
}

return EXIT_SUCCESS;
    }

   void read_wav_header(unsigned int *samp_rate, unsigned int *bits_per_samp,
                 unsigned int *num_samp)
{
unsigned char buf[5];

// freopen ("/Users/ericbrotto/Desktop/iPhoneData/tmp/Hack.wav","r",stdin);

 freopen ("C:/Documents and Settings/Eric.Brotto/Desktop/Eric_Other/Files/Hack.wav","r",stdin);



/* ChunkID (RIFF for little-endian, RIFX for big-endian) */
fread(buf, 1, 4, stdin);
buf[4] = '\0';
if (strcmp((char*)buf, "RIFF")) exit(EXIT_FAILURE);


/* ChunkSize */
fread(buf, 1, 4, stdin);

/* Format */
fread(buf, 1, 4, stdin);
buf[4] = '\0';

printf("IS THIS WAVE?  -->%s<--\n",(char*)buf);


if (strcmp((char*)buf, "WAVE")) exit(EXIT_FAILURE);

/* Subchunk1ID */
fread(buf, 1, 4, stdin);
buf[4] = '\0';
printf("IS THIS fmt?  -->%s<--\n",(char*)buf);

if (strcmp((char*)buf, "fmt ")) exit(EXIT_FAILURE);

/* Subchunk1Size (16 for PCM) */
fread(buf, 1, 4, stdin);
if (buf[0] != 16 || buf[1] || buf[2] || buf[3]) exit(EXIT_FAILURE);

/* AudioFormat (PCM = 1, other values indicate compression) */
fread(buf, 1, 2, stdin);
if (buf[0] != 1 || buf[1]) exit(EXIT_FAILURE);

/* NumChannels (Mono = 1, Stereo = 2, etc) */
fread(buf, 1, 2, stdin);
unsigned int num_ch = buf[0] + (buf[1] << 8);
if (num_ch != 1) exit(EXIT_FAILURE);

/* SampleRate (8000, 44100, etc) */
fread(buf, 1, 4, stdin);
*samp_rate = buf[0] + (buf[1] << 8) +
(buf[2] << 16) + (buf[3] << 24);

/* ByteRate (SampleRate * NumChannels * BitsPerSample / 8) */
fread(buf, 1, 4, stdin);
const unsigned int byte_rate = buf[0] + (buf[1] << 8) +
(buf[2] << 16) + (buf[3] << 24);

/* BlockAlign (NumChannels * BitsPerSample / 8) */
fread(buf, 1, 2, stdin);
const unsigned int block_align = buf[0] + (buf[1] << 8);

/* BitsPerSample */
fread(buf, 1, 2, stdin);
*bits_per_samp = buf[0] + (buf[1] << 8);

if (byte_rate != ((*samp_rate * num_ch * *bits_per_samp) >> 3))
    exit(EXIT_FAILURE);

if (block_align != ((num_ch * *bits_per_samp) >> 3))
    exit(EXIT_FAILURE);



/* Subchunk2ID */
// fread reads line by line until the end. 

fread(buf, 1, 4, stdin);
  buf[4] = '\0';

if (strcmp((char*)buf, "data")) exit(EXIT_FAILURE);



/* Subchunk2Size (NumSamples * NumChannels * BitsPerSample / 8) */
fread(buf, 1, 4, stdin);

const unsigned int subchunk2_size = buf[0] + (buf[1] << 8) +
(buf[2] << 16) + (buf[3] << 24);
*num_samp = (subchunk2_size << 3) / (
                                     num_ch * *bits_per_samp);
 }


 void read_wav_data(int *data, unsigned int samp_rate,
               unsigned int bits_per_samp, unsigned int num_samp)
 {

// freopen ("/Users/ericbrotto/Desktop/iPhoneData/tmp/Hack.wav","r",stdin);
 freopen ("C:/Documents and Settings/Eric.Brotto/Desktop/Eric_Other/Files/Hack.wav","r",stdin);

  unsigned char buf;
unsigned int i, j;
for (i=0; i < num_samp; ++i) {
    unsigned int tmp = 0;
    for (j=0; j != bits_per_samp; j+=8) {
        fread(&buf, 1, 1, stdin);           
        tmp += buf << j;
    }
    data[i] = conv_bit_size(tmp, bits_per_samp);
}
 }


 int conv_bit_size(unsigned int in, int bps)
 {
const unsigned int max = (1 << (bps-1)) - 1;
 return in > max ? in - (max<<1) : in; // Supposedly this is not correct: http://ubuntuforums.org/showthread.php?p=10442711
//  return in > max ? in - ((max<<1)+2) : in;

 }


EDIT 2

On my mac it outputs all the samples in the array (ints roughly between -32000 and 32000). Here I get the output you see in the image followed by a few million zeros.

解决方案

Yes. Even assuming that both compilers comply with the ISO standard (which is not necessarily so), there's still a lot of leeway in that standard.

For example, if your program uses implementation defined or locale-specific behaviour, the output can be different.

If whoever wrote the program used undefined behaviour, then that's also a possibility for different output.

Your best bet would be to show us the code for proper analysis.

If you're interested in the sorts of things that can differ, Annex J of C99 (you can downlaod the draft with TC1, 2 and 3 from the bottom of that page - the differences between that and the final product are minimal) lists the portability issues (unspecified, undefined, implementation-defined and locale-specific behaviour).

One thing you may want to be careful of. This may not apply to Tiny C but I know that the Microsoft compilers I've used are one of that class where "r" and "rb" are treated differently in fopen/freopen. If you just specify "r", translation takes place which may give you the wrong data from a binary file such as a wav file.

这篇关于不同的编译器,不同的输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆