• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            c++中const變量真的不可以修改嗎?

            c++中const變量真的不可以修改嗎?

            在學c++的時候,看到的大部分的書籍對const關鍵字的介紹都是:const關鍵字修飾的變量的值是不可被修改的。但是事實真是如此嗎?今天做了一個小的實驗,發現const變量是可以被修改的。c++代碼如下:           
             1 #include <iostream>
             2 using namespace std;
             3 
             4 int main()
             5 {
             6     const int a = 3;
             7     int* p = const_cast<int*>(&a);
             8     *= 4;
             9     cout << "value of p: " << *<< endl;
            10     cout << "value of a: " << a << endl;
            11     cout << "address of p: " << p << endl;
            12     cout << "address of a: " << &<< endl;
            13 
            14     return 0;
            15 }             
            上面代碼第7行將a的地址賦值給指針p,然后第8行修改p所指向的地址中的值,運行結果如下:
            運行結果
            value of p: 4
            value of a: 3
            address of p: 0x7fbffff7fc
            address of a: 0x7fbffff7fc
            如上所示結果,指針p所指向的地址和變量a的地址相同,但是p所指地址中的值已經發生改變。但是為何變量a對應的地址中的值已經由3變為4,但是a的值確實3呢?
            暫時把這個問題擱淺。再來看一下如下的c++代碼:
             1 #include <iostream>
             2 using namespace std;
             3 const int a = 3;
             4 int main()
             5 {
             6     //const int a = 3;
             7     int* p = const_cast<int*>(&a);
             8     *= 4;
             9     cout << "value of p: " << *<< endl;
            10     cout << "value of a: " << a << endl;
            11     cout << "address of p: " << p << endl;
            12     cout << "address of a: " << &<< endl;
            13 
            14     return 0;
            15 }
            如上代碼g++編譯通過,在運行時報錯如下:
            輸出結果
            Segmentation fault (core dumped)
            由此可見,在c++中全局const變量和局部const變量的編譯器處理的方法是不一樣的。查閱資料得知,全局const變量是不分配內存地址的,它編譯器放置在符號表中作為編譯期常量,全局const變量放在只讀數據段中,受到只讀數據段的權限保護,當你修改一個只讀數據段中的內容時,會得到一個運行時錯誤。而局部const變量是放在堆棧之中,因為在內存中有地址,通過修改地址中的值可以達到修改const所指內存中值的目的。

            posted on 2012-04-10 23:49 MrRightLeft 閱讀(3796) 評論(10)  編輯 收藏 引用 所屬分類: C/C++

            評論

            # re: c++中const變量真的不可以修改嗎? 2012-04-11 13:16 迎風而立

            這個修改的并不是a的值啊,而是*p的值。*p和a表示同一個單元。但是當用3給a賦值之后,是a的值不可以改變,而不是*p的值不可以改變。  回復  更多評論   

            # re: c++中const變量真的不可以修改嗎?[未登錄] 2012-04-11 14:10 ithaca

            博主還是沒能解釋為什么第一個程序中a的值沒有變為4  回復  更多評論   

            # re: c++中const變量真的不可以修改嗎? 2012-04-11 14:20 OUR!!CPP

            const_cast<int*>這里已經告訴編譯器把原本放在常量區的變量移出來了。  回復  更多評論   

            # re: c++中const變量真的不可以修改嗎? 2012-04-11 16:28 so

            cout << "value of a: " << a << endl;
            \\
            cout << "value of a: " << 3 << endl;  回復  更多評論   

            # re: c++中const變量真的不可以修改嗎? 2012-04-12 18:09 MrRightLeft

            @OUR!!CPP
            在const_cast<int*>之前打印出a的地址,和應用了const_cast<int*>之后的a的地址比較,是相同的。何來變量被從常量區移出來一說?
              回復  更多評論   

            # re: c++中const變量真的不可以修改嗎? 2012-04-12 18:12 MrRightLeft

            a這個常量不是從相應地址中取出來的。在程序運行時a的值應該是從符號表中取出。所以改變a對應地址中的值,并不會改變a變量的值@ithaca
              回復  更多評論   

            # re: c++中const變量真的不可以修改嗎? 2012-04-17 11:00 Coneagoe

            #include <iostream>
            using namespace std;

            int main() {
            const int N = 22;
            int * pN = const_cast<int*>(&N);
            *pN = 33;
            cout << N << '\t' << &N << endl;
            cout << *pN << '\t' << pN << endl;
            }
            Output:

            22 0xbf91cfa0
            33 0xbf91cfa0


            The above output, obtained with gcc version 4.0.3, could be different on your system, because the behavior is undefined.

            In this example, we used const_cast to obtain a regular pointer to a const int. Because the const int is in stack storage class, we don't get a segmentation fault by attempting to change the memory. The compiler is unable to "optimize out" the int, and the const_cast tells it not to even try.

            Ref:
            http://cartan.cas.suffolk.edu/oopdocbook/html/staticcast.html  回復  更多評論   

            <2012年4月>
            25262728293031
            1234567
            891011121314
            15161718192021
            22232425262728
            293012345

            導航

            統計

            隨筆分類

            隨筆檔案

            文章分類

            文章檔案

            搜索

            最新評論

            閱讀排行榜

            評論排行榜

            久久精品国产精品青草| 热久久国产精品| 久久人人爽人人爽人人片AV不| 久久精品国产黑森林| 青春久久| 97久久精品国产精品青草| 久久最近最新中文字幕大全| 久久久WWW免费人成精品| 亚洲欧美成人综合久久久| 国产亚洲综合久久系列| 久久免费视频6| 国产午夜免费高清久久影院| 狠狠色综合网站久久久久久久| 久久亚洲AV无码精品色午夜| 久久99精品国产自在现线小黄鸭| 久久久久97国产精华液好用吗| 日韩人妻无码一区二区三区久久| 91精品国产色综久久| 国产精品久久婷婷六月丁香| 久久最新精品国产| 人妻无码αv中文字幕久久琪琪布| 成人国内精品久久久久影院VR| 国产亚洲精品久久久久秋霞| 国产真实乱对白精彩久久| 久久人爽人人爽人人片AV| 亚洲精品国产第一综合99久久| 国产高潮国产高潮久久久91 | 亚洲国产小视频精品久久久三级 | 色欲综合久久躁天天躁| 国内精品人妻无码久久久影院| 久久综合久久美利坚合众国| 久久99精品久久久久久9蜜桃| 国产亚洲色婷婷久久99精品| 久久综合亚洲色HEZYO国产| 久久不射电影网| 2022年国产精品久久久久| 久久综合国产乱子伦精品免费| 99久久做夜夜爱天天做精品| 亚洲欧美国产精品专区久久 | 精品久久久久久无码专区不卡| 久久久久精品国产亚洲AV无码|