2011-02-18 86 views
1

我有一些C代碼,我一直在我的Mac上的Xcode工作。然後我想在Windows機器上使用它並用TinyC進行編譯。當我運行它時,輸出是不同的。不同的編譯器,不同的輸出?

這可能是由於使用不同的編譯器?

謝謝!


EDIT 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, &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; 

} 

編輯2

我的Mac它輸出陣列中的所有樣品(大約-32000到32000之間整數)。在這裏,我得到你在圖像中看到的輸出,然後是幾百萬個零。

enter image description here

+0

輸出是什麼,以及代碼是什麼生成的? – Joe 2011-02-18 13:45:30

+0

完全有可能,它取決於您正在編寫的代碼,有些代碼被認爲是「依賴於實現」,這意味着結果會因編譯器而異 – vmpstr 2011-02-18 13:45:58

+0

我們還需要輸出 – delnan 2011-02-18 13:56:18

回答

0

是的。即使假定兩個編譯器都符合ISO標準(這不一定如此),但該標準仍有很大的餘地。

例如,如果您的程序使用實現定義的或特定於語言環境的行爲,則輸出可能不同。

如果編寫程序的人使用了未定義的行爲,那麼這也是不同輸出的可能性。

最好的辦法是向我們展示適當分析的代碼。

如果您對各種不同的事情感興趣,請參閱附件J C99(您可以從該頁底部的TC1,2和3下載草稿 - 它與最終產品之間的差異是最小)列出了可移植性問題(未指定,未定義,實現定義和特定於語言環境的行爲)。

有一點你可能想要小心。這可能不適用於Tiny C,但我知道我使用的Microsoft編譯器是其中的"r""rb"fopen/freopen中的區別對待。如果您只指定"r",則會發生翻譯,這可能會從二進制文件(例如wav文件)中給您提供錯誤數據。

0

任何相當簡單的代碼(即的printf(「世界,你好!」)的行爲應該以同樣的方式在這兩個環境。更奇特的表情都不能保證。

Xcode使用GCC,所以你的代碼可以使用GNU - 特定擴展名(不保證是便攜式)

相關問題