2012-12-12 136 views
-1

我遇到了解碼器問題,想知道是否有人可以幫我解決問題嗎?任何人都可以幫助我解決分段錯誤

基本上,我試圖建立我的隱寫編碼解碼器,編碼器放置位從消息上LSB每一個字節的從聲音文件

解碼器的工作就是蒐集那些位了並從他們的創建一個新的消息

代碼是爲了做到以下幾點:

代碼:選擇所有

  • 轉到消息陣列位置。

  • 集到bitIndex處0的增量,直到7 //(8位爲一個字節)

  • 去聲音陣列位置

  • 如果soundbit等於加0至新的字節否則添加一個至 新的字節結束

  • 執行位位移一次離開

  • 增量bitIndex處

通過使用各種printf的我可以告訴你,運行速度流暢約3/4倍崩潰之前。

希望是有道理的實際循環是這樣的:

{ 


int mIndex, sIndex, bitIndex, mask; 
char *message[9]; 

mask = 1; 
mIndex = 0; 


unsigned char *soundFile = LoadWavAudioFile("boomedit.wav"); 

int soundFileSize = GetSizeOfWavFile(); 



bitIndex = 0; 

    for(mIndex = 0; mIndex < 8; mIndex++)//index for message 
    { 
     printf("1\n"); 
     for(sIndex = 0; sIndex < soundFileSize; sIndex++)//index for soundfile 
     { 
      printf("2\n"); 
      for(bitIndex = 0; bitIndex < 8;) 
      { 
       printf("3\n"); 
       int test; 
       if((soundFile[sIndex] & mask) > 0)//is message bit > 0 
       {         
        printf("++++++++++++++++\n"); 
        *message[mIndex] = (soundFile[sIndex] | 1);//adds a one to message byte 
        *message[mIndex] = *message[mIndex] <<1; //shift bits 1 placce left 
        printf("*****************\n"); 

       } 
       else 
       { //no change the LSB to 0 
        printf("------------------\n"); 
        *message[mIndex]= soundFile[sIndex] & 254; //adds a zero at end o 
        *message[mIndex] = *message[mIndex] <<1; //shifts bits 1 place to left 
        printf("******************\n"); 
       } 

       bitIndex++; 
      } 
     } 
    } 

printf("\n hiddden letters:%s\n", *message); //print message  
printf("\nalert 5\n"); 

} 

希望幫助任何事情都會是有益的。

+1

哪條線段錯誤?如何聲明和初始化'message'和'soundFile'? (發佈代碼時不要使用標籤,改用4個空格) – Mike

+1

缺少sIndex for循環的開放括號('{')。這是一個錯字嗎?你也......從來沒有使用bitIndex? –

+0

我認爲丟失括號是一個錯字。當它將位添加到我的新消息數組時,會發生段錯誤。 – Babbleshack

回答

1

的問題是在這裏:

char *message[9]; 

你所做的9個指針數組的字符,你不爲它們分配任何值或分配他們有任何記憶。它們未初始化。

你現在要做的第一件事就是尊重那些未初始化的指針之一:

*message[mIndex] = 

因此你崩潰。


編輯:

您可以通過它來初始化所有NULL S:

char *message[9] = {0}; 

但你仍然不能使用,而不是它只是賽格上deferencing NULL指針故障。你要分配一些內存這些是有用的。例如,你可以這樣做:

message[0] = malloc(100); // I don't know how much you need for your strings 
message[1] = malloc(100); // that's up to you, so I'm just arbitrally picking 100 bytes 
message[2] = malloc(100); // here to illustrate the point. 
message[3] = malloc(100); 
message[4] = malloc(100); 
message[5] = malloc(100); 
message[6] = malloc(100); 
message[7] = malloc(100); 
message[8] = malloc(100); 

那麼你就必須在完成後釋放他們每個人。這是你想要的嗎?一串字符串?

這條線:

printf("\n hiddden letters:%s\n", *message); //print message 

意味着對我說,你只是一個字符串後真的...

+0

2分鐘讓我看到和生病的消息back – Babbleshack

+0

我innitialised由char *消息[9] = {},但它仍然崩潰 – Babbleshack

+0

@ Babbleshack - 這是因爲他們還沒有初始化。看我的編輯。 – Mike

0

在您發佈的代碼,sIndex將等於soundFileSize當你拿到你的第一個if聲明。它從這裏看起來就像你正在讀取一個數組的末尾,soundFile。 (假設數組的大小其實soundFileSize

+0

嗯你是什麼意思,它的讀取過去的soundFile結束。 基本上我找到聲音文件的大小使用我的老師提供給我的功能 – Babbleshack

相關問題