2016-10-07 12 views
-2

C言語のプログラムに問題があります。ローカル変数の絶対メモリアドレスが関数mainに必要です。私はgdbでデバッグしています。GDB in Cプログラミング

変数にオーバーフローがあるかどうかを知る方法。

break main 
run 
p &inputFile 
はあなたのアドレスを与える:

あなたは

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

typedef int16_t int16; 

#include "mulaw.h" 


void decodeFile(FILE * fIn, FILE * fOut, uint32_t samples) { 

uint32_t i; 

uint8_t * inSamples = malloc(samples * sizeof(uint8_t)); 
int16_t * outSamples = malloc(samples * sizeof(int16_t)); 

fread(inSamples, sizeof(uint8_t), samples, fIn); 

for (i = 0; i < samples; i++) { 
    outSamples[i] = 4 * muLaw[inSamples[i]]; 
} 

fwrite(outSamples, sizeof(int16_t), samples, fOut); 

free(inSamples); 
free(outSamples); 
} 

#define MAX_FILE_SIZE 256 

int main(int argc, char **argv) 
{ 

char inputFile[MAX_FILE_SIZE]; 
char outputFile[MAX_FILE_SIZE]; 

FILE * fIn = NULL, * fOut = NULL; 

struct header_t { 
    char ChunkID[4]; 
    int32_t ChunkSize; 
    char Format[4]; 
} header; 

char SubchunkID[4]; 
uint32_t SubchunkSize; 

struct subheader_t { 
    int16_t AudioFormat; 
    int16_t NumChannels; 
    int32_t SampleRate; 
    int32_t ByteRate; 
    int16_t BlockAlign; 
    int16_t BitsPerSample; 
    int16_t ExtraParamSize; 
    int16_t Padding; 
} subheader; 



/* Usage */ 
if (argc != 3) { 
    puts("Usage is: mulaw INFILE OUTFILE\n"); 
    exit(EXIT_FAILURE); 
} 

/* Careful here!!!!! */ 
strncpy(inputFile, argv[1], MAX_FILE_SIZE); 
strncpy(outputFile, argv[2], MAX_FILE_SIZE); 

/* Open input file */ 
fIn = fopen (inputFile, "rb"); 

/* Read main header */ 
fread(&header, sizeof(struct header_t), 1, fIn); 

if (memcmp(header.ChunkID, "RIFF", 4) != 0 
    || memcmp(header.Format, "WAVE", 4) != 0) { 

    fprintf(stderr, "Unknown input format\n"); 
    exit(EXIT_FAILURE); 
} 

/* Read sub header */ 

while (fread(SubchunkID, sizeof(SubchunkID), 1, fIn)) { 
    fread(&SubchunkSize, sizeof(SubchunkSize), 1, fIn); 

    printf("Reading chunk of type %c%c%c%c (%d bytes)\n", 
     isprint(SubchunkID[0]) ? SubchunkID[0] : '?', 
     isprint(SubchunkID[1]) ? SubchunkID[1] : '?', 
     isprint(SubchunkID[2]) ? SubchunkID[2] : '?', 
     isprint(SubchunkID[3]) ? SubchunkID[3] : '?', 
     (int) SubchunkSize); 

    if (memcmp(SubchunkID, "fmt ", 4) == 0) { 
     /* read a fmt_ header */ 
     fread(&subheader, SubchunkSize, 1, fIn); 

     /* we are going to adjust this header now to change the audio format */ 
     if (subheader.AudioFormat != 7) { 
      fprintf(stderr, "Only mu-law audio input is supported\n"); 
      exit(EXIT_FAILURE); 
     } 

     /* adjust audio format and bit depth */ 
     subheader.AudioFormat = 1; 
     subheader.BitsPerSample = 16; 

     /* fix derivative fields */ 
     subheader.ByteRate = subheader.SampleRate * subheader.NumChannels * subheader.BitsPerSample/8; 
     subheader.BlockAlign = subheader.NumChannels * subheader.BitsPerSample/8; 

     /* we don't write ExtraParamSize, because for AudioFormat == 1 it is not needed */ 
     SubchunkSize -= 2; 

     /* Open file and write the header for our updated fmt_ chunk */ 
     fOut = fopen (outputFile, "wb"); 
     fwrite(&header, sizeof(struct header_t), 1, fOut); /* Main header */ 

     fwrite(SubchunkID, sizeof(SubchunkID), 1, fOut); /* Subheader */ 
     fwrite(&SubchunkSize, sizeof(SubchunkSize), 1, fOut); 
     fwrite(&subheader, SubchunkSize, 1, fOut); 

    } else if (memcmp(SubchunkID, "data", 4) == 0) { 
     /* here is our mu-law data */ 

     /* write the header for our new chunk (it is twice as large) */ 
     int32_t tSubchunkSize = SubchunkSize * 2; 
     fwrite(SubchunkID, sizeof(SubchunkID), 1, fOut); 
     fwrite(&tSubchunkSize, sizeof(SubchunkSize), 1, fOut); 

     /* process the data */ 

     (fIn, fOut, SubchunkSize); 
    } else { 
     /* unknown chunk, skipping */ 
     fseek(fIn, SubchunkSize, SEEK_CUR); 
    } 
} 

/* Cleanup and exit */ 
fclose(fIn); 
fclose(fOut); 

exit(EXIT_SUCCESS); 

}

} 
+0

あなたの質問を明確にしてください。あなたが何をしているのか何が起きているのかを説明し、あなたのコードに問題があると思う理由を教えてください。私たちはあなたのコンピュータ上で何をやっているのか、何を見ているのか分かりません。 – nos

答えて

0

だけGDBに続いてgdb yourexe

を実行しますありがとう:あなたは、それはあなたの問題を追跡するのに役立ちますかどうかわからないと述べp sizeof(variable)またはp sizeof(type_of_the_variable>) を使用することができ、変数のサイズを取得するために、元$2 = (char (*)[256]) 0x62fd60

+0

変数の大きさはどうやって見ることができますか?たとえば、inputFileを直接表示する場合、p&fInは$ 3 =(FILE **)0x7fffffffe240のように表示されます。 –

+0

'p(sizeof(fin))' –

+0

本当にありがとうJean-François。私には別の問題があります。私はプログラムの31行目の1073741824バイトのメモリを逆にするためにgdbを使ってuint8_t * inSamples = malloc(samples * sizeof(uint8_t))を修正したい。私はデータファイルのオフセット、古い値と新しい値でテーブルを埋める必要があります。 –