메모리 주소 이상합니다. 도와주십시오~~ ^^

kgykingdom의 이미지

hex 문자열을 입력 받아서 버퍼에 차례로 입력하는 함수 입니다.

그런데 메모리 카피 할때 자꾸 세그먼트 폴트 나서 gdb로 디버깅 해 봤는데.. 문제를 통 알수가 없더군요.

그래서 혹시나 메모리를 잘못 참조 하지 않을까 싶어.. 버퍼의 메모리 주소를 확인해 봤는데.. 이상한 점이 있네요. 아래 좀 봐주시길 바랍니다.

UINT16 InputData(UINT8* DLDU)
{
        char buf[2048]={0};
        char hexString[2]={0};
        UINT8 i=0;
        UINT8 byte=0;
        UINT16 pos=0;
        UINT8 cnt=0;
        char* p=NULL;
 
 
        printf("input data > ");
 
        if (NULL != fgets(buf, sizeof(buf), stdin)) {
 
                p = strrchr(buf, '\n');
                if (p)
                        *p = 0;
 
                for (; i < strlen(buf); i++) {
 
                        if (buf[i] == ' ' && cnt == 2) {
                                put_byte(DLDU, &pos, &byte, 1);
                                cnt = 0;
                                byte = 0;
 
                        } else {
                                hexString[cnt] = buf[i];
                                cnt++;
                                if (cnt == 2)
                                        sscanf(hexString, "%x", (UINT32*)&byte);
 
                        }
                }
 
        }
 
 
        sscanf(hexString, "%x", (UINT32*)&byte);
        put_byte(DLDU, &pos, &byte, 1);
 
        printf("input() DLDU: %p\n", DLDU);
 
        return pos;
}
 
bool test(UINT8* DLDU)
{
   printf("before DLDU; %p\n", DLDU);
 
   pos = InputData(DLDU);
 
   printf("after DLDU; %p\n", DLDU);
 
}

위는 코드고 아래는 결과 입니다. 처음 버퍼의 주소가 첫번째 buf address고 함수 들어가기 전 주소가 두번째. 그리고 입력 데이터 받고요..

그리고 함수 리턴하기 전에 주소 찍은것이 input DLDU 마지막이 리턴하고 바로 출력한 버퍼 주소 입니다. 그런데 리턴하기 전에는 정상적이던 주소가

리턴하니 엉뚱한대로 가 있네요.. 이짓 저짓 다 해봤는데.. 가끔 printf() 막 넣으면 정상적으로 나올때도 있습니다. 그러니 더 이상하네요.. ^^;;

fgets()함수에 문제가 있는듯 한것 같은데.. (그냥 하드 코딩하면 괜찮더군요.. ) 도통 모르겠네요.

고수님들의 간절히 도움 기다립니다..

buf address: 0xbf971e50
before DLDU; 0xbf971e50
input data > 01 00 00 03 f2 01 31 30 2e 36 32 2e 32 30 37 2e 33 34 00 00 00 00 00 00 0f a9 03 10 81 00 c4 00 0a 00 02 00 01 00 08 00
input DLDU: 0xbf971e50
<span>after DLDU; 0xbf000000</span>
세그멘테이션 오류
grassman의 이미지

p = strrchr(buf, '\n'); 이렇게 사용할 경우 buf의 위치는 항상 buf의 처음 시작 주소입니다. 만약 buf의 첫 byte가 '\n'이라면.... 앞 주소를 계속 검색해서 '\n'을 찾을 겁니다. 즉, buf의 영역을 벗어난 위치를 검색하는 것입니다. 입력된 문자열에서 '\n'을 찾는다면 p = strchr(buf, '\n');이 맞습니다.

grassman의 이미지

hexString은 3byte가 되어야 합니다. 문자열의 끝에는 항상 '\0'가 들어가야 하기 때문입니다. 그리고 byte는 UINT32로 선언해 주시기 바랍니다.

kgykingdom의 이미지

정말 답변 감사드립니다. 문제는 byte 선언에서 발생한 것 같습니다. 그런데.. 아무래도 이유는 잘 모르겠네요. ^^ 즐거운 하루 보내십시오.

댓글 달기

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
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.