2015-12-21 81 views
0

我目前正在編程我的學士項目什麼是RFB客戶端和共享內存。 RFB客戶端的初始化完成後,將創建共享內存。我的老師讓我解耦代碼,我寫了幾個函數,併爲共享內存使用了一個全局變量。在幾個函數中使用全局變量後出現分段錯誤

但是現在嘗試讀取全局變量的內容時發生了段錯誤。我調試了代碼,發現:全局變量「my_shm」的內容始終爲「0x00」: -/ 您能幫我嗎?

這些代碼的部分發生問題: (我知道,這是一個漫長的代碼,但只發送的這部分將是無用的...)

char *my_shm; --> //global variable 

int SHM_init (int shmid, char* shm, key_t key, long int size) { 


    /* Create a new (System V) shared memory segment of the specified size */ 
    shmid = shmget(key, SHM_SIZE, IPC_CREAT|0777); 
    /* Check if SHM creation was successful */ 
    if (shmid < 0) { 
     /* DBG: Debug message to show which point of the program has been passed */ 
     DBG_PRINT("C\n"); 

     /* Check if creation failed because of already existing SHM */ 
     if (EEXIST == errno) { 
      /* DBG: Debug message to show which point of the program has been passed */ 
      DBG_PRINT("CC\n"); 
      /* Delete already existing SHM with shmctl */ 
      shmctl(shmid, IPC_RMID, NULL); 
     } else { 
      /* DBG: Debug message to show which point of the program has been passed */ 
      DBG_PRINT("CCC\n"); 
     } 

     /* Creation and initialization of SHM failed */ 
     return -1; 
    } 
    /* Attach the SHM data pointer to the previously created SHM segment */ 
    shm = shmat(shmid, NULL, 0); 

    if(shm == (char *) -1) { 
     /* Attaching failed */ 
     return -1; 
    } 
    DBG_PRINT("Shared Memory Initialization successful\n"); 
    /* Creation and initialization of shared memory was successful */ 
    return 0; 
} 

void RFB_update(rfbClient* client) {  

    DBG_PRINT("RFB_update called\n"); 
    int i,j; 

    rfbPixelFormat* pformat=&client->format; 

    DBG_PRINT("A\n"); 

    /*bytesPerPix: variable which stores Bytes per Pixel*/ 
    int bytesPerPix = pformat->bitsPerPixel/8; 

    DBG_PRINT("B\n"); 

    /*row= width of frame*bytes per Pixel*/ 
    int row=client->width*bytesPerPix; 

     DBG_PRINT("C\n"); 

    char byte_to_write; 

    //as long as j is smaller than 128*(width*bytesPerPix) 
    for(j=0;j<client->height*row;j+=row) { 
     //as long as i is smaller than 128 * bytesPerPix 
     for(i=0;i<client->width*bytesPerPix;i+=bytesPerPix) {  
      /*frameBuff: Pointer on FrameBuffer*/   
     unsigned char* frameBuff = client->frameBuffer+j+i; 
      unsigned int v;   

      if(bytesPerPix==4) 
       v=(unsigned int*)frameBuff; 

      byte_to_write = ((v>>pformat->redShift)*256/(pformat->redMax+1)); 
      SHM_write_byte(my_shm,byte_to_write); 

      byte_to_write = ((v>>pformat->greenShift)*256/(pformat->greenMax+1)); 
      SHM_write_byte(my_shm,byte_to_write); 

      byte_to_write = ((v>>pformat->blueShift)*256/(pformat->blueMax+1)); 
      SHM_write_byte(my_shm,byte_to_write); 


     } 
    } 

    DBG_PRINT("RFB_update successful, Shared Memory is filled\n"); 
} 

int SHM_write_byte (char** shm, char byte) { 


    /*Check if pointer to SHM is valid */ 
    if (shm == (char **) -1) { 
     /* Pointer is invalid */ 
     return -1; 
    } 
    shm = byte; 
    shm++; 

    return 0; 
} 

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

    if (SHM_init(shmid, my_shm, SHM_KEY, SHM_SIZE) != 0) { 
     DBG_PRINT("Shared Memory initialized\n"); 
     /* Couldn't initialize SHM,initializing failed */ 
     return -1; 
    } 
    /* Initialize RFB Client   */ 
    if (RFB_client_init(rfb_client, (FinishedFrameBufferUpdateProc)RFB_update) != 0) { 
     DBG_PRINT("Couldn't initialize client\n"); 
     /* Couldn't initialize Client,initializing failed */ 
     return -1; 

} 

- >無處不在使用變量「my_shm」:內容是:0x00 ...

回答

1

這似乎是今天在stackoverflow.com上的一個非常常見的問題,問題是您通過值將參數傳遞給函數和不是,通過參考

這意味着當你傳遞一個參數給一個函數時,它的值被複制,並且該函數只能在函數內的本地副本上工作。正如你應該知道的那樣,修改副本當然不會修改原文。

C沒有通過引用傳遞,但它可以通過使用指針來模擬。在你的情況,因爲你有一個指針,你需要使用地址運算符將指針傳遞到指針,就像

SHM_init(shmid, &my_shm, SHM_KEY, SHM_SIZE) 
//   ^
//    | 
// Note ampersand (address-of operator) here 

當然,你需要修改的功能,居然接受一個指針的指針:

int SHM_init (int shmid, char** shm, key_t key, long int size) 

,當然還有使用變量時,使用引用操作*

*shm = shmat(shmid, NULL, 0); 
+0

太感謝你了!這幫了很多... –