2013-03-12 84 views
1

我正在嘗試製作一個打印連續內存位置數量的小應用程序。作爲程序在內存中的位置的指示,我正在打印主函數和虛擬變量的內存位置。從內存中打印值列表時出現分段錯誤

在第一列中,我想打印地址。在第二列中,我想要這個地址的內容和它後面9個地址的內容。在第三列中,我想打印字節值作爲字符,如果它是可打印的。如果它不可打印,我想打印一個點。在第一行下面的行中,我完全一樣。

在啓動時,可以輸入多個要打印的值。如果輸入正值,則地址將遞增,如果輸入負值,地址將遞減。

要快速查看我想要到達的位置,可以運行代碼並輸入例如20個字節來轉儲並使用虛擬地址作爲起始地址。

到目前爲止,我的代碼只適用於正值。當我輸入一個負數時,我會遇到分段錯誤,但我無法弄清楚原因。我試圖在Valgrind中找到錯誤,但沒有成功。

一些幫助將不勝感激!

這裏是我的代碼:

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

#define NUMBER_OF_BYTES 10 

void showAddresses(void); 
void printMemory(void); 
void getDumpSize(void); 

char* dummy;    // dummy is een pointer naar een int 
signed int dumpSize;   // how many bytes have to be dumped? 
signed int upDown;   // do I need to go up or down? 


int main(void) 
{ 
    dummy = (char *) malloc (sizeof(unsigned char));// memory allocation 
    showAddresses(); // prints the main function address and a variable address 
    getDumpSize(); //  
    printMemory(); // 
    free(dummy);  // free memory 
    return 0;  // end the main function 
} 

void showAddresses(void) 
{ 
    printf("Main function address is %p \n", main); 
    printf("Dummy variable address is %p \n",(void*)dummy); 
} 

void getDumpSize(void) 
{ 
    printf("Enter number of bytes to dump <negative or positive>:"); 
    scanf("%d",&dumpSize); 
    if(dumpSize<0) 
    { 
     upDown = -1;  // count down 
     printf("upDown was set to -1\n"); 
    } 
    else 
    { 
     upDown = 1;   // count up 
     printf("upDown was set to +1\n"); 
    } 
} 

void printMemory(void) 
{ 
    int input; 
    printf("Enter the address:"); 
    scanf("%x", &input);    // enter the input 
    printf("Address \tBytes \t\t\t\tChars \n"); // print the table header 
    printf("--------- \t----------------------------- \t---------- "); 
    int i; 
    unsigned char* address;   // 
    for(i=0;i<abs(dumpSize);i++) 
    { 
     address = (unsigned char*) (input+(i*upDown)); // make the address to print 
     if((i%NUMBER_OF_BYTES) == 0) // show the address every 'i*NUMBER_OF_BYTES' times 
     { 
      printf("\n%p \t", (void*) address); 
     } 

     printf("%02x ", *address); // print as a 2 number hex and use zero padding if needed 


     if((i%NUMBER_OF_BYTES) == (NUMBER_OF_BYTES-1))// print the char list for every value (if printable) 
     { 
      printf("\t"); 
      int j; 
      for(j=(NUMBER_OF_BYTES-1);j>=0;j--) 
      { 
       address = (unsigned char*) (input+(i*upDown)-j); 
       if(isprint(*address)==0)// print a dot if the byte value is not printable 
       { 
        printf("."); 
       } 
       else 
       { 
        printf("%c",*address); // print the byte value as a char, if printable 
       } 
      } 
     } 
    } 
} 

回答

1

你分割故障可能是由試圖程序的範圍之外訪問內存來了。 「dummy」中的地址是程序中的第一個malloc,因此它可能代表程序可用內存的第一個「區域」。從那裏上去可能會讓你進入程序空間(因此沒有seg故障),但是往後退可能會將你帶入有限的內存區域。出於好奇:什麼是你的「虛擬」malloc返回的內存地址?這個數字是否足夠大而不會消極? (我想知道你的程序是否能夠看到真正的系統內存映射或已經爲程序打包的分頁映射。)

+0

謝謝你的回覆!我認爲這確實是錯誤的。今天有人告訴我,因爲虛擬變量是一個全局變量,它接近於受限制的內存。這似乎只是分段故障的原因之一。我也在另一個系統上試過這個代碼,並注意到它沒有工作。我認爲我應該使用'long int input'而不是'int input',因爲在某些系統中,int的大小對於某些內存地址是不夠的。 – Joe 2013-03-13 18:54:52