세그멘 테이션 폴트

익명 사용자의 이미지

아래의 소스는 문자열 카피하는 소슨데, 컴파일하고 실행시키니깐,
세그멘테이션 폴트가 납니다.

이유가 뭔지 모르겟습니다.

1 #include
2
3 char *copy_string(char *dest, char *source);
4
5 void main(void)
6 {
7 char *s = "this is source";
8 char *d;
9
10 copy_string(s, d);
11
12 // printf("d is %s", d);
13 // printf("and s is %s\n", *s);
14
15 }
16
17 char *copy_string(char *dest, char *source)
18 {
19 while(1){
20 *dest = *source;
21
22 if(*dest == '\0')
23 return;
24
25 ++dest;
26 ++source;
27 }
28
29 return dest;
30 }

익명 사용자의 이미지

리눅스 wrote..
아래의 소스는 문자열 카피하는 소슨데, 컴파일하고 실행시키니깐,
세그멘테이션 폴트가 납니다.

이유가 뭔지 모르겟습니다.

1 #include<stdio.h>
2
3 char *copy_string(char *dest, char *source);
4
5 void main(void)
6 {
7 char *s = "this is source";
8 char *d;
9
10 copy_string(s, d);
11
12 // printf("d is %s", d);
13 // printf("and s is %s\n", *s);
14
15 }
16
17 char *copy_string(char *dest, char *source)
18 {
19 while(1){
20 *dest = *source;
21
22 if(*dest == '\0')
23 return;
24
25 ++dest;
26 ++source;
27 }
28
29 return dest;
30 }

10 행의 copy_string(s, d) 에서 s,d 를 바꿔 보세요.
copy_string() 를 보심 두번째 인자로 복사할 원문을 넣게 되어 있는데
초기화 안된 포인터를 넣어주니깐, 세그먼테이션 폴트가 나죠.
포인터가 가리키는 메모리를 복사하는데는
님처럼 직접하는 방법도 있겠지만,
memcpy() 함수를 쓰는게 더 좋을것 같습니다.

#include
void *memcpy(void *dest, const void *src, size_t n);
자세한건 man memcpy 해서 보시구요.
열심히 하세요. ^^

익명 사용자의 이미지

mallloc을 이용하여 메모리를 잡으세요~

익명 사용자의 이미지

메인 함수 부분을 이렇게 바꾸면 됩니다.
void main(void)
{
char *s = "this is source";
char *d;
char Buff[ 32 ];

memset( Buff, 0x00, sizeof( Buff ) );
d = Buff;

copy_string(s, d);

printf("d is %s", d);
printf("and s is %s\n", *s); -> printf("and s is %c\n", *s);
또는 -> printf("and s is %s\n", s);

}

댓글 달기

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