리눅스데브코스 [5주차 - 6]<GNU DEBUGGER (GDB) 실습>

심우열·2023년 5월 6일
0

1.

코드

# include <stdio.h>
void main() {
    int i, num, j;
    printf ("Enter the number: "); 
    scanf ("%d", &num );

    for (i=1; i<num; i++) 
        j=j*i;
        
    printf("The factorial of %d is %d\n", num, j);
}

디버그 결과

j에 쓰레기 값이 들어 있음

2.

코드

#include <stdio.h>
void main() 
{
    char *temp= "Paras";
    int i; i=0;
    temp[0]='F';
    for (i=0 ; i < 5 ; i++ )
        printf("%c\n", temp[i]);
    
}

디버그 결과

포인터로 선언된 문자열은 메모리 상에서 code 영역(Read only)에 할당되기 때문에 segment fault 발생

3.

코드

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char **argv) {
    char *buf;
    
    buf = malloc(1<<31); 
    strcpy(buf, "This is Test"); 
    printf("%s\n", buf);

    return 0;
}

컴파일 오류!

디버그 결과

buf에 값을 넣으려고 할때 segment fault 발생, malloc 이 제대로 되지 않아 buf가 null 포인터를 가지기 때문

profile
Dev Ops, "Git, Linux, Docker, Kubernetes, ansible, " .

0개의 댓글