陳佳憲
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Versions and GitHub Sync Note Insights Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       owned this note    owned this note      
    Published Linked with GitHub
    Subscribed
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    Subscribe
    # 你所不知道的c語言 contributed by <[plusline](https://github.com/plusline)> :::danger 你的提問呢?回顧你過去開發的程式,難道沒想到概念上有衝突或者激發出更多想法嗎? :notes: jserv ::: :::info 只要是宣告為指標,不論型態(int、float或是 struct),空間裡應該都是存放一個位置,既然都是存放位置,為甚麼要在宣告為指標的時候區別型態? ::: ## 指標篇 - function pointer 定義:A function pointer, also called a subroutine pointer or procedure pointer, is a pointer that points to a function. 之前只有在C++的 sort function 看過 ```clike= //qsort for c void qsort (void* base, size_t num, size_t size, int (*compar)(const void*,const void*)); ``` #### 練習: ##### 程式碼 ```clike= #include <stdio.h> void function_point(){ printf("call function_point\n"); return; } int main(){ printf("enter main\n"); int (*ptr)()=function_point; printf("ptr:%p\n",&ptr); printf("function_point:%p\n",&ptr); ptr(); return 0; } ``` ##### 結果 ```clike= enter main ptr:0x7ffc3f3c81c0 function_point:0x7ffc3f3c81c0 call function_point ``` ##### 討論 ptr 和 function_point 能呼叫同一個函式且位置相同。 --- ##### 問題 - 該如何解釋 qsort(3) 的參數和設計考量呢? qsort 的第三個參數是 funtion pointer ,目的是為了實踐不一樣的排列方式,通常在比較字串時我們會用 Alphabetical order,直接從字串首開始比較 ascii code 的大小,不過像我們作業系統中資料夾的排序,則是使用接近 Natural sort order 的方式排序。 - 為何我看到的程式碼往往寫成 `struct list **lpp; for(lpp = &list; *lpp != NULL; lpp = &(*lpp)->next)` 呢? 宣告的時候要用指標的指標才能讓傳入函式之後的操作能夠確實的改動 link list。後面的複雜難懂的操作是為了配合宣告。 #### 練習1: - 程式碼 ```clike= #include <stdio.h> #include <stdlib.h> struct node{ int data; struct node *next; }; void go_through(struct node **list){ struct node **lpp; for(lpp = list; *lpp != NULL; lpp = &(*lpp)->next){ printf("data:%d\n",(*lpp)->data); } } int main(){ struct node a,b,c; struct node *head=&a; a.data=12; a.next=&b; b.data=30; b.next=&c; c.data=64; c.next=NULL; go_through(&head); return 0; } ``` ##### 結果 ```clike= data:12 data:30 data:64 ``` ##### 討論 我對於指標的指標的掌握度不高,幾乎是用 trial and error 的方式做出來了,我想應該是吃了太多 C++ 語法糖的緣故。 我之後再嘗試插入節點,這樣才看得出指標的指標的強大,不然只是要遍歷所有節點好像沒有使用的必要。 #### 練習2: ##### 程式碼 ```clike= #include <stdio.h> #include <stdlib.h> struct node{ int data; struct node *next; struct node *prev }; void go_through(struct node *list){ struct node **lpp; for(lpp = &list; *lpp != NULL; lpp = &(*lpp)->next){ printf("data:%d\n",(*lpp)->data); } } void insert(struct node **n, int data){ struct node *ptr; ptr=malloc(sizeof(struct node)); ptr->data=data; ptr->next=(*n)->next; ptr->prev=*n; (*n)->next->prev=ptr; (*n)->next=ptr; } void insert2(struct node *n, int data){ struct node *ptr; ptr=malloc(sizeof(struct node)); ptr->data=data; ptr->next=n->next; ptr->prev=n; n->next->prev=ptr; n->next=ptr; } void dele(struct node **n){ (*n)->prev->next=(*n)->next; (*n)->next->prev=(*n)->prev; *n=(*n)->next; } void dele2(struct node *n){ n->prev->next=n->next; n->next->prev=n->prev; n=n->next; } void dele3(struct node n){ (n).prev->next=(n).next; (n).next->prev=(n).prev; } int main(){ struct node a,b,c; struct node *head=&a; a.data=12; a.prev=NULL; a.next=&b; b.data=30; b.next=&c; b.prev=&a; c.data=64; c.prev=&b; c.next=NULL; struct node *ptr=&b; printf("ptr:%p\n",ptr); insert(&ptr,99); printf("ptr insert 99:%p\n",ptr); insert(&ptr,66); printf("ptr insert 66:%p\n",ptr); insert2(ptr,88); printf("ptr insert2 88:%p\n",ptr); dele(&ptr); printf("ptr dele: %p\n",ptr); dele2(ptr); printf("ptr dele2: %p\n",ptr); dele3(*ptr); printf("ptr dele3: %p\n",ptr); go_through(head); return 0; } ``` ##### 結果 ```clike= ptr:0x7ffed72b3be0 ptr insert 99:0x7ffed72b3be0 ptr insert 66:0x7ffed72b3be0 ptr insert2 88:0x7ffed72b3be0 ptr dele: 0x560e16e756b0 ptr dele2: 0x560e16e756b0 ptr dele3: 0x560e16e756b0 data:12 data:66 data:99 data:64 ``` ##### 討論 函式 dele3 傳入的只是該節點的副本,在子函數中操作完之後,並不能實際影響到 main 的實際結構。 本來 stuct 在函式間傳遞就是用位址,因此只需要用一個*就能作到指標的指標的效果,在函式dele中,我用了**顯的多此一舉。 --- - lvalue and rvalue: > An lvalue refers to an object that persists beyond a single expression. > An rvalue is a temporary value that does not persist beyond the expression that uses it. ---[MSDA](https://msdn.microsoft.com/en-us/library/f90831hc.aspx) lvalue 和 rvalue 的區別是生命週期的長短,lvalue 有一個生命週期長的位置,在賦值完仍持續存在。如: ```click= int a; a=3; ``` 在第二行執行完後 a 仍存在,3則消滅了。 --- - forward declaration >a forward declaration is a declaration of an identifier (denoting an entity such as a type, a variable, a constant, or a function) for which the programmer has not yet given a complete definition. ---[wiki: forward declaration](https://en.wikipedia.org/wiki/Forward_declaration) 在具體定義之前先宣告,已達到函式的封閉性,也增加日後修改的彈性和可讀性。 --- ##### 如何在指標中隱藏數據? 在系統中數據會有最小單位,因此所有的記憶空間為了要配合最小單位,而將地址化分成最小單位的整數倍。 假設那個最小單位為4bits,那定址的時候所有的地址將會是以00結尾。 因此我們可以在其中偷偷藏進一些資訊。像是linux kernel 的rb-tree,就在紀錄父結點的位置的最低有效位放進自己的顏色。 ```click struct rb_node { unsigned long __rb_parent_color; struct rb_node *rb_right; struct rb_node *rb_left; } __attribute__((aligned(sizeof(long)))); ``` [linux/rbtree.h](https://github.com/torvalds/linux/blob/6f0d349d922ba44e4348a17a78ea51b7135965b1/include/linux/rbtree.h) 在 rbtree.h 裡,有定義rb_node的結構,其中有一個unsigned long的變數,用來保存父節點和自己的顏色。 ```click #define RB_RED 0 #define RB_BLACK 1 ``` [rbtree_augmented.h](https://github.com/torvalds/linux/blob/6f0d349d922ba44e4348a17a78ea51b7135965b1/tools/include/linux/rbtree_augmented.h) ```click static inline void rb_set_black(struct rb_node *rb) { rb->__rb_parent_color |= RB_BLACK; } ``` [linux/lib/rbtree.c](https://github.com/torvalds/linux/blob/master/lib/rbtree.c) RB_RED只有最低位為1,而父節點的位置最兩位永遠為0,所以在讀取之前,可以借用最低一位儲存顏色。 :::danger 從其他地方複製內容時,應該要附上出處。另外,需要將用語改為繁體中文和台灣地區科技術語。 RB tree 要附上 Linux 核心程式碼,再來討論 :notes: jserv :::

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully