久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

      <bdo id='jCeE9'></bdo><ul id='jCeE9'></ul>

  1. <small id='jCeE9'></small><noframes id='jCeE9'>

  2. <tfoot id='jCeE9'></tfoot>

      <i id='jCeE9'><tr id='jCeE9'><dt id='jCeE9'><q id='jCeE9'><span id='jCeE9'><b id='jCeE9'><form id='jCeE9'><ins id='jCeE9'></ins><ul id='jCeE9'></ul><sub id='jCeE9'></sub></form><legend id='jCeE9'></legend><bdo id='jCeE9'><pre id='jCeE9'><center id='jCeE9'></center></pre></bdo></b><th id='jCeE9'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='jCeE9'><tfoot id='jCeE9'></tfoot><dl id='jCeE9'><fieldset id='jCeE9'></fieldset></dl></div>

      <legend id='jCeE9'><style id='jCeE9'><dir id='jCeE9'><q id='jCeE9'></q></dir></style></legend>

      C++中的靜態全局變量

      Static global variables in C++(C++中的靜態全局變量)

      <legend id='yd47R'><style id='yd47R'><dir id='yd47R'><q id='yd47R'></q></dir></style></legend>

            <small id='yd47R'></small><noframes id='yd47R'>

          1. <i id='yd47R'><tr id='yd47R'><dt id='yd47R'><q id='yd47R'><span id='yd47R'><b id='yd47R'><form id='yd47R'><ins id='yd47R'></ins><ul id='yd47R'></ul><sub id='yd47R'></sub></form><legend id='yd47R'></legend><bdo id='yd47R'><pre id='yd47R'><center id='yd47R'></center></pre></bdo></b><th id='yd47R'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='yd47R'><tfoot id='yd47R'></tfoot><dl id='yd47R'><fieldset id='yd47R'></fieldset></dl></div>
            <tfoot id='yd47R'></tfoot>
              <bdo id='yd47R'></bdo><ul id='yd47R'></ul>
                <tbody id='yd47R'></tbody>

              1. 本文介紹了C++中的靜態全局變量的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                問題描述

                限時送ChatGPT賬號..

                我想通過 malloc 方法創建一個整數數組.我希望這個數組是全局的,并且可以在我的程序中的任何地方使用.我把代碼放在一個看起來像這樣的頭文件中:

                I would like to make an array of integers via the malloc method. I want this array to be global and be used anywhere in my program. I put code in a header file that looked like this:

                static int *pieces;
                

                然后我有一個函數,用我想要的數字填充它.該函數位于命名空間中,并且該命名空間在其自己的 .cpp 文件中實現.但是,我將頭文件導入 main.c 并從創建數組的命名空間調用函數,如:

                Then I have a function that fills it with numbers that I want in there. The function is in a namespace and the namespace is implemented in its own .cpp file. However, I import the header file into main.c and call the function from the namespace that creates the array like:

                pieces = malloc(sizeof(int) * 128);
                

                但是當我嘗試在 main 中訪問數組中的數字時(在調用創建我的數組的函數之后),它崩潰并說這些部分沒有被初始化.但是在我擁有的函數中,我可以創建它并很好地操縱其中的數字.我的印象是,通過使部分成為靜態變量,只要任何地方的某個函數發生更改(或設置),就會影響該變量在任何地方的使用.基本上我想說的是為什么即使我在我調用的函數中設置了塊,在 main 中仍然顯示未設置?

                But when I try to access numbers in the array in main (after calling the function that creates my array), it crashes and says that pieces wasn't initialized. But in the function I have I can create it and manipulate the numbers in it just fine. I was under the impression that by making pieces a static variable, whenever some function anywhere changes (or sets it) then that will affect the usage of the variable anywhere. Basically what I'm trying to say is why does pieces appear unset in main, even though I set it in a function that I called?

                推薦答案

                Static 是一個有很多含義的關鍵字,在這個特殊情況下,它的意思是非全局(釋義)

                Static is a keyword with many meanings, and in this particular case, it means not global (paraphrasing)

                這意味著每個.cpp 文件都有自己的變量副本.因此,當您在 main.cpp 中初始化時,它僅在 main.cpp 中初始化.其他文件仍未初始化.

                It means that each .cpp file has its own copy of the variable. Thus, when you initialize in main.cpp, it is initialized ONLY in main.cpp. The other files have it still uninitialized.

                首先要解決這個問題是刪除關鍵字static.這將導致多重定義問題".要解決此問題,您應該在 .cpp 文件中定義變量,并在頭文件中對其進行 extern 聲明.

                First thing to fix this would be to remove the keyword static. That would cause the "Multiple definitions issue". To fix this you should define the variable in a .cpp file and just extern declare it in a header file.

                您只是為其分配內存,不算作初始化.分配后需要將內存初始化為0.

                You are just allocating memory to it, doesnt count as initialization. You need to initialize the memory to 0 after allocation.

                您可以使用 new int[128]() 而不是更冗長的 malloc 語法,這將 也執行初始化?或者你可以走簡單的路(這就是它的目的)并使用 std::矢量

                You can use new int[128]() instead of your more verbose malloc syntax, and this would perform initialization as well? Or you could take the easy road (thats what its there for) and use std::vector

                這篇關于C++中的靜態全局變量的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

                【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

                相關文檔推薦

                Why do two functions have the same address?(為什么兩個函數的地址相同?)
                Why the initializer of std::function has to be CopyConstructible?(為什么 std::function 的初始化程序必須是可復制構造的?)
                mixing templates with polymorphism(混合模板與多態性)
                When should I use the keyword quot;typenamequot; when using templates(我什么時候應該使用關鍵字“typename?使用模板時)
                Dependent name resolution amp; namespace std / Standard Library(依賴名稱解析命名空間 std/標準庫)
                gcc can compile a variadic template while clang cannot(gcc 可以編譯可變參數模板,而 clang 不能)

                  <tbody id='YP4bX'></tbody>

                    <bdo id='YP4bX'></bdo><ul id='YP4bX'></ul>

                        <small id='YP4bX'></small><noframes id='YP4bX'>

                      1. <tfoot id='YP4bX'></tfoot>
                      2. <i id='YP4bX'><tr id='YP4bX'><dt id='YP4bX'><q id='YP4bX'><span id='YP4bX'><b id='YP4bX'><form id='YP4bX'><ins id='YP4bX'></ins><ul id='YP4bX'></ul><sub id='YP4bX'></sub></form><legend id='YP4bX'></legend><bdo id='YP4bX'><pre id='YP4bX'><center id='YP4bX'></center></pre></bdo></b><th id='YP4bX'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='YP4bX'><tfoot id='YP4bX'></tfoot><dl id='YP4bX'><fieldset id='YP4bX'></fieldset></dl></div>

                        <legend id='YP4bX'><style id='YP4bX'><dir id='YP4bX'><q id='YP4bX'></q></dir></style></legend>
                        • 主站蜘蛛池模板: 国产精品国产三级国产aⅴ原创 | 国产精品高潮呻吟久久aⅴ码 | 性色视频 | 久久久久国产一区二区三区不卡 | 国产午夜久久久 | 国产目拍亚洲精品99久久精品 | 91污在线 | 中文字幕在线观看 | 日本不卡免费新一二三区 | 一区在线观看 | 在线日韩精品视频 | 精品一二三区 | 欧美不卡网站 | 亚洲欧美日韩精品久久亚洲区 | 一区二区三区视频在线 | 国产真实乱对白精彩久久小说 | 黄色三级免费 | 欧美嘿咻 | 久久久成 | 免费观看毛片 | 日韩一二区在线观看 | 97伦理| 国产亚洲成av人片在线观看桃 | 欧美精品久久久 | 午夜精品久久久久久久久久久久 | 亚洲美乳中文字幕 | 性一爱一乱一交一视频 | 欧美成人免费 | 亚洲人成免费 | 欧美日韩不卡 | 日本中文字幕在线观看 | 国产在线视频一区二区董小宛性色 | 欧美三区 | 精品国产精品三级精品av网址 | 国产在线观看 | 免费的黄色片子 | 亚洲电影一区二区三区 | 国产精品视频一区二区三区 | 欧美freesex黑人又粗又大 | 成人精品一区亚洲午夜久久久 | 成人毛片在线视频 |