Different compilers, different output? -
i have bit of c code i've been working on in xcode on mac. wanted work on windows machine , compile tinyc. when run it, output different.
is possible due using different compilers?
thanks!
edit 1
the code simple script opens wav file throw samples 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; // (i = 0; < num_samp; ++i) { (i = 0; < 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 , settings/eric.brotto/desktop/eric_other/files/hack.wav","r",stdin); /* chunkid (riff little-endian, rifx 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 wave? -->%s<--\n",(char*)buf); if (strcmp((char*)buf, "wave")) exit(exit_failure); /* subchunk1id */ fread(buf, 1, 4, stdin); buf[4] = '\0'; printf("is fmt? -->%s<--\n",(char*)buf); if (strcmp((char*)buf, "fmt ")) exit(exit_failure); /* subchunk1size (16 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 line until 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 , settings/eric.brotto/desktop/eric_other/files/hack.wav","r",stdin); unsigned char buf; unsigned int i, j; (i=0; < num_samp; ++i) { unsigned int tmp = 0; (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 not correct: http://ubuntuforums.org/showthread.php?p=10442711 // return in > max ? in - ((max<<1)+2) : in; }
edit 2
on mac outputs samples in array (ints between -32000 , 32000). here output see in image followed few million zeros.
yes. assuming both compilers comply iso standard (which not so), there's still lot of leeway in standard.
for example, if program uses implementation defined or locale-specific behaviour, output can different.
if whoever wrote program used undefined behaviour, that's possibility different output.
your best bet show code proper analysis.
if you're interested in sorts of things can differ, annex j of c99 (you can downlaod draft tc1, 2 , 3 bottom of page - differences between , final product minimal) lists portability issues (unspecified, undefined, implementation-defined , locale-specific behaviour).
one thing may want careful of. may not apply tiny c know microsoft compilers i've used 1 of class "r"
, "rb"
treated differently in fopen/freopen
. if specify "r"
, translation takes place may give wrong data binary file such wav
file.
Comments
Post a Comment