c/assets/notes_01_c-basic_06_xdx_index.md.LNeOlrR9.js

65 lines
38 KiB
JavaScript
Raw Normal View History

import{_ as s,c as a,o as n,a6 as t}from"./chunks/framework.CZKtKhAm.js";const e="/c/assets/1.L8V3GBrc.png",l="/c/assets/2.CdvhiwcU.png",d="/c/assets/3.D74t3-Xt.png",i="/c/assets/4.DCQsqYYC.gif",p="/c/assets/5.CUjMoXNF.gif",o="/c/assets/6.BWbajkuB.gif",c="/c/assets/7.C1JnBDr7.png",r="/c/assets/8.DwN2p3bo.png",u="/c/assets/9.DdSfz-Pf.gif",b="/c/assets/10.BMocjVt7.gif",f=JSON.parse('{"title":"第一章:颇具争议的指针","description":"","frontmatter":{},"headers":[],"relativePath":"notes/01_c-basic/06_xdx/index.md","filePath":"notes/01_c-basic/06_xdx/index.md","lastUpdated":1722316351000}'),h={name:"notes/01_c-basic/06_xdx/index.md"},g=t('<blockquote><ul><li><code>指针</code> C <code></code><code></code></li><li><code></code> C <code></code> C </li></ul></blockquote><h1 id="-" tabindex="-1"> <a class="header-anchor" href="#-" aria-label="Permalink to &quot;&quot;"></a></h1><h2 id="_1-1-" tabindex="-1">1.1 <a class="header-anchor" href="#_1-1-" aria-label="Permalink to &quot;1.1 &quot;"></a></h2><ul><li> C C <code></code><code></code><code>使</code></li></ul><div class="note custom-block github-alert"><p class="custom-block-title">NOTE</p><p></p><p> C 使</p></div><ul><li><p>使</p><ul><li> <strong>Null Pointer Dereference</strong>NULL访</li><li> <strong>Dangling Pointers</strong></li><li> <strong></strong></li><li> <strong></strong></li></ul></li><li><p></p><ul><li> <strong></strong>使</li><li> <strong></strong></li><li> <strong>使</strong>使 C++ 使</li></ul></li></ul><div class="important custom-block github-alert"><p class="custom-block-title">IMPORTANT</p><p></p><ul><li> JavaGoRust </li><li><EFBFBD><EFBFBD>
<span class="line"><span></span></span>
<span class="line"><span># 项目名称和版本号</span></span>
<span class="line"><span>project(c-study VERSION 1.0 LANGUAGES C)</span></span>
<span class="line"><span></span></span>
<span class="line"><span># 设置 C 标准</span></span>
<span class="line"><span>set(CMAKE_C_STANDARD 23)</span></span>
<span class="line"><span>set(CMAKE_C_STANDARD_REQUIRED True)</span></span>
<span class="line"><span></span></span>
<span class="line"><span># 辅助函数用于递归查找所有源文件</span></span>
<span class="line"><span>function(collect_sources result dir)</span></span>
<span class="line"><span> file(GLOB_RECURSE new_sources &quot;\${dir}/*.c&quot;)</span></span>
<span class="line"><span> set(\${result} \${\${result}} \${new_sources} PARENT_SCOPE)</span></span>
<span class="line"><span>endfunction()</span></span>
<span class="line"><span></span></span>
<span class="line"><span># 查找顶层 include 目录如果存在</span></span>
<span class="line"><span>if (EXISTS &quot;\${CMAKE_SOURCE_DIR}/include&quot;)</span></span>
<span class="line"><span> include_directories(\${CMAKE_SOURCE_DIR}/include)</span></span>
<span class="line"><span>endif ()</span></span>
<span class="line"><span></span></span>
<span class="line"><span># 查找所有源文件</span></span>
<span class="line"><span>set(SOURCES)</span></span>
<span class="line"><span>collect_sources(SOURCES \${CMAKE_SOURCE_DIR})</span></span>
<span class="line"><span></span></span>
<span class="line"><span># 用于存储已经处理过的可执行文件名防止重复</span></span>
<span class="line"><span>set(EXECUTABLE_NAMES)</span></span>
<span class="line"><span></span></span>
<span class="line"><span># 创建可执行文件</span></span>
<span class="line"><span>foreach (SOURCE \${SOURCES})</span></span>
<span class="line"><span> # 获取文件的相对路径</span></span>
<span class="line"><span> file(RELATIVE_PATH REL_PATH \${CMAKE_SOURCE_DIR} \${SOURCE})</span></span>
<span class="line"><span> # 将路径中的斜杠替换为下划线生成唯一的可执行文件名</span></span>
<span class="line"><span> string(REPLACE &quot;/&quot; &quot;_&quot; EXECUTABLE_NAME \${REL_PATH})</span></span>
<span class="line"><span> string(REPLACE &quot;\\\\&quot; &quot;_&quot; EXECUTABLE_NAME \${EXECUTABLE_NAME})</span></span>
<span class="line"><span> string(REPLACE &quot;.&quot; &quot;_&quot; EXECUTABLE_NAME \${EXECUTABLE_NAME})</span></span>
<span class="line"><span></span></span>
<span class="line"><span> # 处理与 CMakeLists.txt 文件同名的问题</span></span>
<span class="line"><span> if (\${EXECUTABLE_NAME} STREQUAL &quot;CMakeLists_txt&quot;)</span></span>
<span class="line"><span> set(EXECUTABLE_NAME &quot;\${EXECUTABLE_NAME}_exec&quot;)</span></span>
<span class="line"><span> endif ()</span></span>
<span class="line"><span></span></span>
<span class="line"><span> # 检查是否已经创建过同名的可执行文件</span></span>
<span class="line"><span> if (NOT EXECUTABLE_NAME IN_LIST EXECUTABLE_NAMES)</span></span>
<span class="line"><span> list(APPEND EXECUTABLE_NAMES \${EXECUTABLE_NAME})</span></span>
<span class="line"><span></span></span>
<span class="line"><span> # 创建可执行文件</span></span>
<span class="line"><span> add_executable(\${EXECUTABLE_NAME} \${SOURCE})</span></span>
<span class="line"><span></span></span>
<span class="line"><span> # 查找源文件所在的目录并添加为包含目录头文件可能在同一目录下</span></span>
<span class="line"><span> get_filename_component(DIR \${SOURCE} DIRECTORY)</span></span>
<span class="line"><span> target_include_directories(\${EXECUTABLE_NAME} PRIVATE \${DIR})</span></span>
<span class="line"><span></span></span>
<span class="line"><span> # 检查并添加子目录中的 include 目录如果存在</span></span>
<span class="line"><span> if (EXISTS &quot;\${DIR}/include&quot;)</span></span>
<span class="line"><span> target_include_directories(\${EXECUTABLE_NAME} PRIVATE \${DIR}/include)</span></span>
<span class="line"><span> endif ()</span></span>
<span class="line"><span></span></span>
<span class="line"><span> # 检查并添加 module 目录中的所有 C 文件如果存在</span></span>
<span class="line"><span> if (EXISTS &quot;\${DIR}/module&quot;)</span></span>
<span class="line"><span> file(GLOB_RECURSE MODULE_SOURCES &quot;\${DIR}/module/*.c&quot;)</span></span>
<span class="line"><span> target_sources(\${EXECUTABLE_NAME} PRIVATE \${MODULE_SOURCES})</span></span>
<span class="line"><span> endif ()</span></span>
<span class="line"><span> endif ()</span></span>
<span class="line"><span>endforeach ()</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br><span class="line-number">34</span><br><span class="line-number">35</span><br><span class="line-number">36</span><br><span class="line-number">37</span><br><span class="line-number">38</span><br><span class="line-number">39</span><br><span class="line-number">40</span><br><span class="line-number">41</span><br><span class="line-number">42</span><br><span class="line-number">43</span><br><span class="line-number">44</span><br><span class="line-number">45</span><br><span class="line-number">46</span><br><span class="line-number">47</span><br><span class="line-number">48</span><br><span class="line-number">49</span><br><span class="line-number">50</span><br><span class="line-number">51</span><br><span class="line-number">52</span><br><span class="line-number">53</span><br><span class="line-number">54</span><br><span class="line-number">55</span><br><span class="line-number">56</span><br><span class="line-number">57</span><br><span class="line-number">58</span><br><span class="line-number">59</span><br><span class="line-number">60</span><br><span class="line-number">61</span><br><span class="line-number">62</span><br><span class="line-number">63</span><br><span class="line-number">64</span><br></div></div><ul><li> CLion </li></ul><p><img src="`+u+'" alt=""></p><ul><li> CLion valgrind </li></ul><p><img src="'+b+'" alt=""></p><h1 id="--" tabindex="-1"> <a class="header-anchor" href="#--" aria-label="Permalink to &quot;&quot;"></a></h1><h2 id="_4-1-" tabindex="-1">4.1 <a class="header-anchor" href="#_4-1-" aria-label="Permalink to &quot;4.1 &quot;"></a></h2><h1 id="--" tabindex="-1"> <a class="header-anchor" href="#--" aria-label="Permalink to &quot;&quot;"></a></h1><h2 id="_5-1-" tabindex="-1">5.1 <a class="header-anchor" href="#_5-1-" aria-label="Permalink to &quot;5.1 &quot;"></a></h2><h2 id="_5-2-" tabindex="-1">5.2 <a class="header-anchor" href="#_5-2-" aria-label="Permalink to &quot;5.2 &quot;"></a></h2><ul><li><p> C <code></code><code></code></p></li><li><p></p><ul><li> <strong></strong><EFBFBD>