mirror of
https://github.com/Aexiar/c.git
synced 2024-10-22 12:05:45 +00:00
65 lines
38 KiB
JavaScript
65 lines
38 KiB
JavaScript
|
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 "第一章:颇具争议的指针""></a></h1><h2 id="_1-1-概述" tabindex="-1">1.1 概述 <a class="header-anchor" href="#_1-1-概述" aria-label="Permalink to "1.1 概述""></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>① 既然指针很危险,那么通过一系列的手段将指针包装或屏蔽,以达到程序安全的目的(这是现代化的高级编程语言解决的思路,如:Java、Go、Rust 等)。</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 "\${dir}/*.c")</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 "\${CMAKE_SOURCE_DIR}/include")</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 "/" "_" EXECUTABLE_NAME \${REL_PATH})</span></span>
|
|||
|
<span class="line"><span> string(REPLACE "\\\\" "_" EXECUTABLE_NAME \${EXECUTABLE_NAME})</span></span>
|
|||
|
<span class="line"><span> string(REPLACE "." "_" 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 "CMakeLists_txt")</span></span>
|
|||
|
<span class="line"><span> set(EXECUTABLE_NAME "\${EXECUTABLE_NAME}_exec")</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 "\${DIR}/include")</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 "\${DIR}/module")</span></span>
|
|||
|
<span class="line"><span> file(GLOB_RECURSE MODULE_SOURCES "\${DIR}/module/*.c")</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 "第四章:指针的理解和定义(⭐)""></a></h1><h2 id="_4-1-概述" tabindex="-1">4.1 概述 <a class="header-anchor" href="#_4-1-概述" aria-label="Permalink to "4.1 概述""></a></h2><h1 id="第五章-指针的运算-⭐" tabindex="-1">第五章:指针的运算(⭐) <a class="header-anchor" href="#第五章-指针的运算-⭐" aria-label="Permalink to "第五章:指针的运算(⭐)""></a></h1><h2 id="_5-1-概述" tabindex="-1">5.1 概述 <a class="header-anchor" href="#_5-1-概述" aria-label="Permalink to "5.1 概述""></a></h2><h2 id="_5-2-总结" tabindex="-1">5.2 总结 <a class="header-anchor" href="#_5-2-总结" aria-label="Permalink to "5.2 总结""></a></h2><ul><li><p>在 C 语言中,<code>普通变量</code>和<code>指针变量</code>支持的操作有所不同,以下是对这两类变量各自支持操作的总结。</p></li><li><p>普通变量是直接存储数据的变量。对于普通变量,支持的操作包括:</p><ul><li>① <strong>赋值操作</strong>:给变<EFBFBD>
|