• <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 閱讀(3787) 評論(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年6月>
            272829303112
            3456789
            10111213141516
            17181920212223
            24252627282930
            1234567

            導航

            統計

            隨筆分類

            隨筆檔案

            文章分類

            文章檔案

            搜索

            最新評論

            閱讀排行榜

            評論排行榜

            日韩欧美亚洲综合久久影院Ds | 色综合久久88色综合天天 | 99精品久久久久久久婷婷| 亚洲精品高清国产一久久| 久久影视国产亚洲| 久久无码人妻一区二区三区午夜| 色偷偷888欧美精品久久久| 亚洲婷婷国产精品电影人久久| 麻豆成人久久精品二区三区免费 | 国产精品久久久亚洲| 精品久久久久中文字幕一区| 狼狼综合久久久久综合网| 久久久99精品成人片中文字幕| 色综合久久中文字幕无码| 久久亚洲AV永久无码精品| 国产成人久久激情91| 国产激情久久久久久熟女老人 | 青青国产成人久久91网| 国产精品久久久久久久久软件| 99久久免费国产特黄| 久久久免费精品re6| 亚洲综合久久夜AV | 久久久久亚洲精品男人的天堂| 99久久精品久久久久久清纯| 久久人人爽人人爽人人AV| 77777亚洲午夜久久多人| 久久久久久免费视频| 久久久久黑人强伦姧人妻| 丁香久久婷婷国产午夜视频| 精品熟女少妇a∨免费久久| 精产国品久久一二三产区区别| 欧美精品乱码99久久蜜桃| 99久久免费国产精品特黄| 久久伊人影视| 中文字幕久久亚洲一区| 少妇熟女久久综合网色欲| 色狠狠久久综合网| 东方aⅴ免费观看久久av| 久久精品国产亚洲AV蜜臀色欲| 久久AV高潮AV无码AV| 久久久免费精品re6|