주소의 매핑 된 지역에 대한 잘못된 권한

압델 라만 라갑

현재 CS50x 문제 세트 5, Speller에 대해 작업 중입니다. valgrind를 사용하면 다음 오류가 발생합니다.

==395== Memcheck, a memory error detector
==395== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==395== Using Valgrind-3.13.0 and LibVEX; rerun with -h for copyright info
==395== Command: ./speller texts/cat.txt
==395== 
==395== 
==395== Process terminating with default action of signal 11 (SIGSEGV)
==395==  Bad permissions for mapped region at address 0x601DE8
==395==    at 0x401155: add_Node (dictionary.c:75)
==395==    by 0x401239: load (dictionary.c:107)
==395==    by 0x400944: main (speller.c:40)
==395== 
==395== HEAP SUMMARY:
==395==     in use at exit: 5,328,336 bytes in 95,140 blocks
==395==   total heap usage: 95,141 allocs, 1 frees, 5,332,432 bytes allocated
==395== 
==395== 552 bytes in 1 blocks are still reachable in loss record 1 of 2
==395==    at 0x4C2FB0F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==395==    by 0x5258E49: __fopen_internal (iofopen.c:65)
==395==    by 0x5258E49: fopen@@GLIBC_2.2.5 (iofopen.c:89)
==395==    by 0x4011BE: load (dictionary.c:88)
==395==    by 0x400944: main (speller.c:40)
==395== 
==395== 5,327,784 bytes in 95,139 blocks are still reachable in loss record 2 of 2
==395==    at 0x4C2FB0F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==395==    by 0x401118: add_Node (dictionary.c:69)
==395==    by 0x401239: load (dictionary.c:107)
==395==    by 0x400944: main (speller.c:40)
==395== 
==395== LEAK SUMMARY:
==395==    definitely lost: 0 bytes in 0 blocks
==395==    indirectly lost: 0 bytes in 0 blocks
==395==      possibly lost: 0 bytes in 0 blocks
==395==    still reachable: 5,328,336 bytes in 95,140 blocks
==395==         suppressed: 0 bytes in 0 blocks
==395== 
==395== For counts of detected and suppressed errors, rerun with: -v
==395== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
Segmentation fault

이것은 load 및 add_Node 함수에 대한 내 코드입니다.

// Loads dictionary into memory, returning true if successful else false
bool load(const char *dictionary)
{
    FILE *file = fopen(dictionary, "r");

    if (file == NULL)
    {
        return false;
    }

    // Set pointers in table to NULL
    for (int i = 0; i < N; i++)
    {
        table[i] = NULL;
    }
    
    char word[LENGTH + 1];  //to hold a word from dictionary
    
    while (fgets(word, LENGTH + 1, file) != NULL)
    {
        int row = hash(word);   // to determine where to store the word

        add_Node(row, word);
    }
    
    fclose(file);

    return true;
}

// Adds new node to a list in dictionary
void add_Node(int row, char word[LENGTH + 1])
{
    node *newNode = malloc(sizeof(node));
    strcpy(newNode->word, word);
    
    if (table[row] == NULL)  //if single linked list is empty
    {
        table[row] = newNode;
    }
    else
    {
        node *tmpHead = table[row];   
        table[row] = newNode;
        newNode->next = tmpHead;
    }
}

89 행은 다음에 해당합니다.

add_Node(row, word);

105 행은 "table [row] = newNode;"에 해당합니다. 에:

if (table[row] == NULL)  //if single linked list is empty
        {
            table[row] = newNode;
        }

table은 노드 유형의 포인터 배열 인 전역 변수입니다. 행이 배열, 테이블의 경계 내에 있는지 이미 확인했습니다. 이 문제를 해결하는 방법을 모르겠습니다. 문제가 무엇일까요?

Manav Dubey

먼저 읽지 않기 때문에 변경 while (fgets(word, LENGTH + 1, file) != NULL)사용하십시오 .while(fscanf(file, "%s", word) != EOF)"\n"

귀하의 add_Node기능이 일부 라인을 누락 일부 변경을 필요로한다 :

낡은:

// Adds new node to a list in dictionary
void add_Node(int row, char word[LENGTH + 1])
{
    node *newNode = malloc(sizeof(node));
    strcpy(newNode->word, word);
    
    if (table[row] == NULL)  //if single linked list is empty
    {
        table[row] = newNode;
    }
    else
    {
        node *tmpHead = table[row];   
        table[row] = newNode;
        newNode->next = tmpHead;
    }
}

새로운:

// Adds new node to a list in dictionary

void add_Node(int row, char word[LENGTH + 1])
{
    node *newNode = malloc(sizeof(node));
    //Test if the node is not empty
    if(newNode == NULL){
        return 1;
    }

    strcpy(newNode->word, word);
    newNode->next = NULL;
    
    if (table[row] == NULL)  //if single linked list is empty
    {
        table[row] = newNode;
    }
    else
    {   //Set the newNode to table at row and make table[row]
        //equal to newNode to overwrite the current address at table[row]. 
        //No need for tmp as we are not swapping things.
        newNode -> next = table[row];
        table[row] = newNode;
    }
}

이것은 당신의 valgrind 문제를 해결할 것입니다. 또한 필요하지 않으므로 tmp 노드가 필요하지 않습니다. 당신이 있는지 확실하지 않다 hash잘 여부 작동 기능,하지만 난 그 괜찮 가정합니다. 마지막으로, valgrind에 따라 "도달 할 수있는"메모리 누수가 발생 fclose(file)하므로 load함수 끝에서 확인하십시오 .

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

Rust에 대한 C 구조체의 잘못된 매핑

분류에서Dev

json 매핑에 대한 잘못된 클래스 속성 선언

분류에서Dev

phpmyadmin의 구성 파일에 대한 잘못된 권한

분류에서Dev

잘못된 작업 표시 줄에 대한 열린 Windows 매핑의 Windows 10 작업 표시 줄 문제

분류에서Dev

런타임 오류 : 잘못된 메모리 주소 또는 페이지 매김에 대한 포인터 역 참조 없음

분류에서Dev

잘못된 요소 매핑

분류에서Dev

/ etc 폴더에 대한 잘못된 권한

분류에서Dev

segfault SEGV_ACCERR-개체에 대한 잘못된 권한

분류에서Dev

Windows WSL Ubuntu에서 탑재 된 디스크의 파일에 대한 잘못된 권한이 표시됨

분류에서Dev

스파크 콩 인코더는 중첩 된 클래스에 대한 잘못된 값을 매핑

분류에서Dev

역 지오 코딩에 대한 잘못된 자격 증명

분류에서Dev

두 개의 중첩 된 @Embeddable에 대한 JPA 매핑 문제

분류에서Dev

ods로 구분 된 SAS의 열에 대한 매크로 루핑

분류에서Dev

Bitbucket git SSH 키 오류 : "/home/centos/.ssh/config에 대한 잘못된 소유자 또는 권한"

분류에서Dev

mysqli_stmt_bind_result ()에 대한 잘못된 매개 변수 수

분류에서Dev

매개 변수 'directory'에 대한 잘못된 값

분류에서Dev

매개 변수에 대한 잘못된 수정 자

분류에서Dev

인앱 구매에 대한 잘못된 제품 식별자

분류에서Dev

오류 87, SetupDiGetDeviceInterfaceDetail에 대한 잘못된 매개 변수 수신

분류에서Dev

불완전한 유형에 대한 'sizeof'의 잘못된 적용

분류에서Dev

DIRECTORY 유형에 대한 Oracle의 dbms_metadata.get_ddl : 매개 변수 SCHEMA에 대한 잘못된 입력 값

분류에서Dev

Google Drive REST API-한 사용자에 대한 잘못된 권한 값

분류에서Dev

$ ra에 잘못된 주소가있어 내 MIPS asm 코드의 무한 루프

분류에서Dev

matlab의 lyap () 함수에 대한 잘못된 대답

분류에서Dev

Nginx 및 Docker가있는 Webpack devserver : 잘못된 주소에 대한 폴링 정보

분류에서Dev

주어진 입력에 대한 잘못된 출력

분류에서Dev

지정된 매핑 집합에 대한 기능 쌍의 발생 횟수 계산

분류에서Dev

IF에 대한 잘못된 인수 개수

분류에서Dev

nginx 경로에 대한 잘못된 값

Related 관련 기사

  1. 1

    Rust에 대한 C 구조체의 잘못된 매핑

  2. 2

    json 매핑에 대한 잘못된 클래스 속성 선언

  3. 3

    phpmyadmin의 구성 파일에 대한 잘못된 권한

  4. 4

    잘못된 작업 표시 줄에 대한 열린 Windows 매핑의 Windows 10 작업 표시 줄 문제

  5. 5

    런타임 오류 : 잘못된 메모리 주소 또는 페이지 매김에 대한 포인터 역 참조 없음

  6. 6

    잘못된 요소 매핑

  7. 7

    / etc 폴더에 대한 잘못된 권한

  8. 8

    segfault SEGV_ACCERR-개체에 대한 잘못된 권한

  9. 9

    Windows WSL Ubuntu에서 탑재 된 디스크의 파일에 대한 잘못된 권한이 표시됨

  10. 10

    스파크 콩 인코더는 중첩 된 클래스에 대한 잘못된 값을 매핑

  11. 11

    역 지오 코딩에 대한 잘못된 자격 증명

  12. 12

    두 개의 중첩 된 @Embeddable에 대한 JPA 매핑 문제

  13. 13

    ods로 구분 된 SAS의 열에 대한 매크로 루핑

  14. 14

    Bitbucket git SSH 키 오류 : "/home/centos/.ssh/config에 대한 잘못된 소유자 또는 권한"

  15. 15

    mysqli_stmt_bind_result ()에 대한 잘못된 매개 변수 수

  16. 16

    매개 변수 'directory'에 대한 잘못된 값

  17. 17

    매개 변수에 대한 잘못된 수정 자

  18. 18

    인앱 구매에 대한 잘못된 제품 식별자

  19. 19

    오류 87, SetupDiGetDeviceInterfaceDetail에 대한 잘못된 매개 변수 수신

  20. 20

    불완전한 유형에 대한 'sizeof'의 잘못된 적용

  21. 21

    DIRECTORY 유형에 대한 Oracle의 dbms_metadata.get_ddl : 매개 변수 SCHEMA에 대한 잘못된 입력 값

  22. 22

    Google Drive REST API-한 사용자에 대한 잘못된 권한 값

  23. 23

    $ ra에 잘못된 주소가있어 내 MIPS asm 코드의 무한 루프

  24. 24

    matlab의 lyap () 함수에 대한 잘못된 대답

  25. 25

    Nginx 및 Docker가있는 Webpack devserver : 잘못된 주소에 대한 폴링 정보

  26. 26

    주어진 입력에 대한 잘못된 출력

  27. 27

    지정된 매핑 집합에 대한 기능 쌍의 발생 횟수 계산

  28. 28

    IF에 대한 잘못된 인수 개수

  29. 29

    nginx 경로에 대한 잘못된 값

뜨겁다태그

보관