[추가질문][C언어]세그멘테이션 오류입니다(포인터관련...)

ecstasy5001의 이미지

정태영님 감사합니다 우선 해결은 됐는데 왜그런지 이해가 안가서 추가 질문 합니다.

    while( *pptr )
    {
        printf("1\n");
        add = (long int *)*pptr;
        printf("2\n");
        myen.s_addr = *add;
        printf("3\n");
        printf("%s\n", inet_ntoa(myen));
        printf("4\n");
        pptr++;
    }

윗 부분에서 myen.s_addr = *addr; 이부분을 수정했더니 잘 돌아갔습니다.

헌데 저 부분을 수정하지 않고 실행을 시키면
printf("4\n"); 까지 실행이되고 printf("1\n")이 실행이 안됩니다.
그 의미는 결국 while( *pptr) 이 부분이 잘못된 참조를 하고 있다는 소리 아닌가요??
그리고 또한가지 해당 도메인이 가지고 있는 모든 아이피를 가져오는데
출력 결과는 꼭 마지막 아이피를 얻어오고 나서 세그멘테이션 오류가 나는 이유는 뭘까요??(테스트 해봄)
myen->s_addr = *add; 이부분이 잘못된 곳이면 처음 while루프 문을 돌때부터
세그멘테이션 오류가 나야 되는거 아닌가요??

//////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////

아.. 계속해서 봤는데 왜그런지 정말 모르겠습니다.

int main(int argc, char **argv)
{
    struct hostent *myent;
    struct in_addr *myen;
    char **pptr;
    long int *add;
    int i = 0;
 
    myent = gethostbyname(argv[1]);
    if (myent == NULL)
    {
        perror("ERROR: ");
        exit(0);
    }
 
    printf("%s\n", myent->h_name);         
    pptr = myent->h_addr_list;
    while( *pptr )
    {
        printf("1\n");
        add = (long int *)*pptr;
        printf("2\n");
        myen->s_addr = *add;
        printf("3\n");
        printf("%s\n", inet_ntoa(*myen));
        printf("4\n");
        pptr++;
    }
    return 0;
}

인자로 들어온 값을 gethostbyname해서 ip목록을 출력해주는 프로그램입니다.

그런데 꼭 마지막까지 출력을 해놓고는 세그멘테이션 오류가 발생합니다..

./GetHostByName www.naver.com
www.naver.com
1
2
3
61.247.208.7
4
1
2
3
222.122.84.200
4
1
2
3
222.122.84.250
4
1
2
3
61.247.208.6
4
Segmentation fault

www.yahoo.com으로 실행했을때 입니다..

./GetHostByName www.yahoo.com
www.yahoo-ht3.akadns.net
1
2
3
209.131.36.158
4
Segmentation fault

while( *pptr ) 이부분에서 오류가 나는게 확실한거 같은데 왜오류가 나는지 조언좀 부탁드리겠습니다.

정태영의 이미지

pptr 이 NULL terminated 라는 보장이 없는 상태에서 while( *pptr ){ ... } 식으로 코드를 짜셨기 때문입니다.

Quote:
    for( i = 0 ; i <  myent->h_length ; i++, pptr++ )
    {
        printf("1\n");
        add = (long int *)*pptr;
        printf("2\n");
        myen->s_addr = *add;
        printf("3\n");
        printf("%s\n", inet_ntoa(*myen));
        printf("4\n");
    }

위와 같은 식으로 짜셔야 하지 않을까 싶습니다. ;)

--
오랫동안 꿈을 그리는 사람은 그 꿈을 닮아간다...

http://mytears.org ~(~_~)~
나 한줄기 바람처럼..

오랫동안 꿈을 그리는 사람은 그 꿈을 닮아간다...

http://mytears.org ~(~_~)~
나 한줄기 바람처럼..

정태영의 이미지

아 제가 man 페이지를 대강 읽었네요 --;; 문제는 위에께 아닙니다. pptr 은 null terminated 가 맞습니다.

Quote:
struct in_addr *myen;
...
 
add = (long int *)*pptr;
printf("2\n");
myen->s_addr = *add;
printf("3\n");
printf("%s\n", inet_ntoa(*myen));

보시면 알겠지만 myen 은 struct in_addr 의 포인터입니다. 그런데 메모리를 할당하지 않고 사용하셨네요. 이는 아래와 같이 고치시면 해결이 가능합니다.

Quote:
struct in_addr myen;
 
...
 
myen.s_addr = *add;
printf("3\n");
printf("%s\n", inet_ntoa(myen));

대강 보고 요상한 답변을 달아서 죄송 - -;; 하네요/

--
오랫동안 꿈을 그리는 사람은 그 꿈을 닮아간다...

http://mytears.org ~(~_~)~
나 한줄기 바람처럼..

오랫동안 꿈을 그리는 사람은 그 꿈을 닮아간다...

http://mytears.org ~(~_~)~
나 한줄기 바람처럼..

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.