2016-10-01 2 views
-3

이 프로그램을 실행하면 C에서 간단한 프로그램을 만들고 세그먼트 오류 11이 발생합니다. 내 코드언어 C, 세분화 오류

int main() 
{ 


    char** tab = NULL, i; 

    tab = malloc(HAUTEUR * sizeof(char*)); 
    for(i = 0; i < HAUTEUR; i++) 
     tab[i] = malloc(LARGEUR * sizeof(char)); 

    initialiseGrille(tab); 

    for(i = 0; i < HAUTEUR; i++) 
     free(tab[i]); 
    free(tab); 
} 



void initialiseGrille(char** aGrid) 
{ 
    for(int x=1; x <= 15; x++) 
    { 
     for(int y=1;y <= 10; y++) 
     { 
      aGrid[x][y] = ' '; 
     } 
    } 
} 

저는 구글에서 일부 검색을 할 수 있고 해결책이 내 오류를 감지 Valgrind의를 사용하는 것입니다, 그래서 난이 수익을 얻을 :

==3008== Memcheck, a memory error detector 
==3008== Copyright (C) 2002-2015, and GNU GPL'd, by Julian Seward et al. 
==3008== Using Valgrind-3.11.0 and LibVEX; rerun with -h for copyright info 
==3008== Command: ./a.out 
==3008== 
==3008== Invalid read of size 8 
==3008== at 0x100000B7C: initialiseGrille (in ./a.out) 
==3008== by 0x100000AEE: main (in ./a.out) 
==3008== Address 0x100011fd0 is 0 bytes after a block of size 80 alloc'd 
==3008== at 0x640B: malloc (in /usr/local/Cellar/valgrind/3.11.0_1/lib/valgrind/vgpreload_memcheck-amd64-darwin.so) 
==3008== by 0x100000AA5: main (in ./a.out) 
==3008== 
==3008== Invalid write of size 1 
==3008== at 0x100000B80: initialiseGrille (in ./a.out) 
==3008== by 0x100000AEE: main (in ./a.out) 
==3008== Address 0x1 is not stack'd, malloc'd or (recently) free'd 
==3008== 
==3008== 
==3008== Process terminating with default action of signal 11 (SIGSEGV) 
==3008== Access not within mapped region at address 0x1 
==3008== at 0x100000B80: initialiseGrille (in ./a.out) 
==3008== by 0x100000AEE: main (in ./a.out) 
==3008== If you believe this happened as a result of a stack 
==3008== overflow in your program's main thread (unlikely but 
==3008== possible), you can try to increase the size of the 
==3008== main thread stack using the --main-stacksize= flag. 
==3008== The main thread stack size used in this run was 8388608. 
==3008== 
==3008== HEAP SUMMARY: 
==3008==  in use at exit: 25,108 bytes in 381 blocks 
==3008== total heap usage: 457 allocs, 76 frees, 31,052 bytes allocated 
==3008== 
==3008== LEAK SUMMARY: 
==3008== definitely lost: 0 bytes in 0 blocks 
==3008== indirectly lost: 0 bytes in 0 blocks 
==3008==  possibly lost: 0 bytes in 0 blocks 
==3008== still reachable: 230 bytes in 11 blocks 
==3008==   suppressed: 24,878 bytes in 370 blocks 
==3008== Rerun with --leak-check=full to see details of leaked memory 
==3008== 
==3008== For counts of detected and suppressed errors, rerun with: -v 
==3008== ERROR SUMMARY: 2 errors from 2 contexts (suppressed: 1 from 1) 
Segmentation fault: 11 

사람이 어떤이 있다면 이 오류를 수정하는 아이디어 ...

+5

'LARGEUR'과'HAUTER'의 값은 무엇입니까? – ewcz

+0

'LARGEUR'과'HAUTER'를위한'# define' 상수를 보여줄 수 있습니까? – RoadRunner

답변

0

동적으로 할당하는 배열의 경우에도 C의 인덱싱은 0부터 시작합니다. 따라서,이 코드

for(int x=1; x <= 15; x++) { 
    for(int y=1;y <= 10; y++) { 
     aGrid[x][y] = ' '; 
    } 
} 

은 다음과 같이 다시 작성해야합니다

for(int x=0 ; x < HAUTEUR ; x++) { 
    for(int y=0 ; y < LARGEUR ; y++) { 
     aGrid[x][y] = ' '; 
    } 
} 

귀하의 오프별로 한 오류가 포인터를 사용할 수 없게 할당 된 메모리 블록의 끝을지나 작성하는 프로그램을 발생합니다.

값이 일치하는 경우에도 수치 대신에 HAUTEURLARGEUR과 같은 기호 상수를 사용하는 것이 더 좋습니다.

+0

좋아요, 문제는 제 할당과 같지만 제 친구는 HAUTEUR과 LARGEUR의 가치를 거꾸로했습니다 ... XD가 도와 줘서 고맙습니다. –