const를 이용한 포인터 제한 질문!

kjw7945의 이미지

#include <stdio.h>

int main()
{
        char *const pText1 = "test_text";
        *pText1 = 'a';

        // 혹은 pText1[0] = 'a';
        // 혹은 *pText1 = 10; 혹은 pText1[0] = 10;

        return 0;
}

char *const :
will not allow you to change where the pointer points, but will allow you
change what is at that place in memory.

라고 합니다. 결론은... const가 포인터 변수의 주소를 const
시킵니다.. 포인터 값은 상관없이 변경 가능합니다..

즉 포인터 변수의 주소 변경은 할 수 없으나 포인터가
가리키는 값의 변경은 가능하다...

위의 코드를 컴파일 하면 오류 없이 컴파일이 되나
실제 실행을 하면 세그멘테이션 오류가 납니다...

어째서 세그멘테이션 오류가 날까요?

몇가지 확인차 작성했던 코드 중에 더 이상한건
int형은 아무런 오류없이 동작합니다..

다음과 같은 코드는 세그멘테이션 오류가 나지 않습니다.
(똑같은 방법으로 코딩한 경우입니다..)

        int a = 20;
        int *const pText2 = &a;
        pText2[0] = 10;
        return 0;

똑같이 포인터의 값을 변경한 문제인데요... int형은 문제가 발생하지 않네요.. 왜 그럴까요..?

( just different between int and char...
why segementation fault ?... T.T)

dondek의 이미지

"test_text" 가 rodata 영역에 저장되어 write 가 가능하지 않은 문제라고 생각되네요.

진리를 나의 수준으로 끌어내리지 마라.
나를 진리의 수준으로 끌어올려라. - 배꼽 중에서

pool007의 이미지

부연하자면,

char *const pText1 = "test_text";

이건 문자열 상수입니다. (string literal)

"test_text"는 상수로 프로그램에 그대로 박혀들어갑니다. (표현이 이상하지만 아무튼 그렇습니다.)

반면에

char *const pText1[20];
strcpy(pText1, "test_text");

한다음 수정하는 경우는 스택에 test_text라는 문자열이 들어가는 경우라 수정이 가능합니다.

--
Passion is like genius; a miracle.

kjw7945의 이미지

감사합니다.

이제 이해가 되네요 ^^

rodata에 쓰여진다 == text_text가 문자열 상수다..

모두 같은 이야기네요 ^^

nthroot의 이미지

'리터럴' 이라는 용어로 쓰죠.. :shock:

------식은이 처------
길이 끝나는 저기엔 아무 것도 없어요. 희망이고 나발이고 아무 것도 없어.

댓글 달기

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