2016-03-13 113 views
0

我想通過這在我的功能,但是在進入功能型前事情錯傳遞一個字符串與指針/一個函數C

FILE *f = fopen("out.bmp", "rb"); 
    int countBit = 0; 
    int size = 0; 
    char* type; 

    for (int i = 0; i < 54; i++) 
     fgetc(f); 
    printf("/* count bit to pixel */\n"); 
    scanf("%d", &countBit); 
    size=DecodeInformationSize(f, countBit); 
    type=DecodeInformationType(f, countBit); 
    DecodeMessage(f,countBit,size,type); 

是TXT enter image description here

但經過:

void DecodeMessage(FILE *f, int countBit, int size, char* type) 
{ 
    char messageSize[8]; 
    char nameOut[15] = "outMessage."; 
    strcat(nameOut, type); 
    char* message = (char*)malloc(size * 8); 

enter image description here

請說明問題

回答

1

要絕對相信我們需要知道什麼呢DecodeInformationType(f, countBit);

但是,似乎它使用堆棧上的一些數據。一旦它返回,這個信息可能只有幾條指令可用。所以,你的調試器顯示調用DecodeMessagetype指向一個有效的字符串,但一旦你進入DecodeMessage,堆棧改寫的DecodeMessage變量,特別是與char nameOut[15] = "outMessage.";

爲了解決這個問題,確保DecodeInformationType回報一個指針,指向存儲器不是的DecodeInformationType堆棧(未自動變量)上。這可能是內存malloc或常量字符串分配。

相關問題