[{"data":1,"prerenderedAt":-1},["ShallowReactive",2],{"doc-seo-685742-105":3,"detail-sidebar-cat-0-en-105":81,"doc-detail-685742-en":130,"doc-similar-685742":154},{"code":4,"msg":5,"data":6},0,"ok",{"site_id":7,"language":8,"slug":9,"title":10,"keywords":11,"description":12,"schema_data":13,"social_meta":74,"head_meta":76,"extra_data":78,"updated_unix":80},105,"en","08-c-lists-personal-software-engineering-lists-in-c-but-first-how-much-space-is-needed","08-C-Lists - Personal Software Engineering - Lists in C - But First-How Much Space Is Needed?","","材料以“Lists in C”为核心，围绕在 C 语言中分配与计算数据所需内存展开。先解释 strlen 与 malloc 的字符串长度推导逻辑，进而讲解 sizeof 在基础类型、指针、数组、以及函数形参数组退化为指针时的字节含义。随后讨论结构体的大小由成员类型与对齐填充决定，最后引入链表的基本思想，并给出单向链表节点结构与 malloc/free 动态分配的示例，用指针维护头结点与 next 指向关系。",{"@graph":14,"@context":73},[15,34,56],{"@type":16,"itemListElement":17},"BreadcrumbList",[18,23,27,31],{"item":19,"name":20,"@type":21,"position":22},"https://docshare.wps.com","Home","ListItem",1,{"item":24,"name":25,"@type":21,"position":26},"https://docshare.wps.com/document/","Document",2,{"item":28,"name":29,"@type":21,"position":30},"https://docshare.wps.com/document/technology/","Technology",3,{"item":32,"name":10,"@type":21,"position":33},"https://docshare.wps.com/document/08-c-lists-personal-software-engineering-lists-in-c-but-first-how-much-space-is-needed/685742/",4,{"url":32,"name":10,"@type":35,"image":36,"author":41,"headline":10,"publisher":44,"fileFormat":47,"inLanguage":8,"description":12,"dateModified":48,"datePublished":49,"encodingFormat":47,"isAccessibleForFree":50,"interactionStatistic":51},"DigitalDocument",{"url":37,"@type":38,"width":39,"height":40},"https://docshare.wps.com/thumbnails/08-c-lists-personal-software-engineering-lists-in-c-but-first-how-much-space-is-needed/685742.png","ImageObject",300,407,{"name":42,"@type":43},"Hazel","Person",{"url":19,"name":45,"@type":46},"DocShare","Organization","application/pdf","2026-12-01","2026-11-07",true,{"@type":52,"interactionType":53,"userInteractionCount":55},"InteractionCounter",{"@type":54},"ViewAction",6,{"@type":57,"mainEntity":58},"FAQPage",[59,65,69],{"name":60,"@type":61,"acceptedAnswer":62},"为什么在 C 里需要使用 sizeof 来估算空间？","Question",{"text":63,"@type":64},"因为不同类型（如 int、double、struct 等）需要的字节数不同。sizeof 用于获取保存某个类型或变量所需的字节数。","Answer",{"name":66,"@type":61,"acceptedAnswer":67},"为什么函数参数中的数组形参 sizeof(buffer) 结果会与预期不同？",{"text":68,"@type":64},"因为数组实参在函数形参中会退化为指针。示例中 void foo(char buffer[81]) 等价于 void foo(char *buffer)，因此 sizeof(buffer) 取的是指针大小。",{"name":70,"@type":61,"acceptedAnswer":71},"链表相比数组有什么优势？",{"text":72,"@type":64},"数组需要连续的内存空间且位置固定，而链表的每个节点可在内存中处于不同位置。链表能更灵活地动态增长和缩小，并可使用 malloc/free 分配释放节点空间。","https://schema.org",{"og:url":32,"og:type":75,"og:title":10,"og:site_name":45,"og:description":12},"article",{"robots":77,"canonical":32},"index,follow",{"doc_id":79,"site_id":7},685742,1794017226,{"code":4,"msg":82,"data":83},"success",[84,88,92,96,101,104,109,114,119,122,126],{"id":22,"doc_module":4,"doc_module_name":25,"category_name":85,"show_sort_weight":86,"slug":87},"Story & Novel",90,"story-novel",{"id":26,"doc_module":4,"doc_module_name":25,"category_name":89,"show_sort_weight":90,"slug":91},"Literature",80,"literature",{"id":33,"doc_module":4,"doc_module_name":25,"category_name":93,"show_sort_weight":94,"slug":95},"Exam",70,"exam",{"id":97,"doc_module":4,"doc_module_name":25,"category_name":98,"show_sort_weight":99,"slug":100},5,"Comic",60,"comic",{"id":55,"doc_module":4,"doc_module_name":25,"category_name":29,"show_sort_weight":102,"slug":103},50,"technology",{"id":105,"doc_module":4,"doc_module_name":25,"category_name":106,"show_sort_weight":107,"slug":108},7,"Healthcare",40,"healthcare",{"id":110,"doc_module":4,"doc_module_name":25,"category_name":111,"show_sort_weight":112,"slug":113},8,"Research & Report",30,"research-report",{"id":115,"doc_module":4,"doc_module_name":25,"category_name":116,"show_sort_weight":117,"slug":118},9,"Religion & Spirituality",20,"religion-spirituality",{"id":117,"doc_module":4,"doc_module_name":25,"category_name":120,"show_sort_weight":117,"slug":121},"World Cup","world-cup",{"id":123,"doc_module":4,"doc_module_name":25,"category_name":124,"show_sort_weight":123,"slug":125},10,"Lifestyle","lifestyle",{"id":127,"doc_module":4,"doc_module_name":25,"category_name":128,"show_sort_weight":97,"slug":129},19,"General","general",{"code":4,"msg":82,"data":131},{"doc_id":79,"user_id":132,"nickname":42,"user_avatar":133,"doc_module":4,"category_id":55,"category_name":29,"doc_title":10,"doc_description":12,"doc_content":134,"file_id":135,"file_url":136,"file_type":137,"file_size":138,"view_count":55,"is_deleted":4,"is_public":22,"is_downloadable":22,"audit_status":22,"page_count":139,"language":140,"language_code":8,"site_id":7,"html_lang":8,"table_of_contents":141,"faqs":142,"seo_title":143,"seo_description":12,"update_tm":80,"read_time":144,"preview_page_count":26,"preview_urls":145},137441390410,"https://ap-avatar.wpscdn.com/avatar/2000252f4ab5702993?_k=1776741390130283984","Lists in C  \nPersonal Software Engineering  \nBut First-How Much Space Is Needed?  \nFor strings, we can use strlen:  \nchar *p copy = malloc( strlen(\"Hello\")+1 ) ;  \nBut what about other types (ints, doubles, structs, etc.)? This is the purpose of the sizeof operator!  \nsizeof for basic types  \nsizeof (type) = \\#bytes needed to hold a type value  \nsizeof (variable) = \\#bytes needed to hold variable's type.  \nExamples (current 32 and 64 bit systems):  \nsizeof(char) = 1 sizeof(short) = 2  \nsizeof (int) = 4  \nsizeof(float) = 4  \nsizeof(long) = 8 sizeof(double) = 8  \nsizeof(char *) = 4 (32-bit systems) / 8 (64-bit systems)  \nNOTE: all pointers to any type have the same size!  \nsizeof for array types  \ndouble sampledata[100] ;  \nsizeof(sampledata) ; // = 100 * 8 = 800  \nchar string[81] ;  \nsizeof(string) ; // = 81 * 1 = 81  \nBUT  \nvoid foo(char buffer[81]) { . . . }  \nsizeof(buffer) ; // = 8 !!  \nWHY?  \nBecause array arguments are really pointers! The function header above above is equivalent to:  \nvoid foo(char *buffer) { . . . }  \nsizeof for structs  \ntypedef struct  node {  \nint contents ;  \nstruct  node *next ;  \n} node ;  \nsizeof(node) == \\# bytes required to hold the structure.  \n== sizeof (int) + size(node *) + padding  \nPadding is needed to assure data are aligned on the proper boundary: ints on 4 byte boundaries  \nshorts on 2 byte boundaries  \ndoubles and pointers on 8 byte boundaries  \nPadding is dictated by the way CPU's access memory.  \nWhat are lists?  \nAn array holds multiple items in memory- And they are all sequential (in memory)  \nA list also holds multiple items in memory  \n-But each item is in a different (unknown) location in memory!  \nLists are therefore much more useful (flexible) than arrays, because you don’t need large amounts of sequential memory to hold all the items  \nSingly Linked Lists  \nA (singly) linked list comprises a set of nodes, each node having a pointer to the next node in the list.  \nWe keep a pointer to the first node in a list head pointer.  \n100   \nnext  \n150   \nnext  \n800   \nnext  \np_head   \nSince lists can grow and shrink dynamically, space for the list nodes is allocated and released dynamically using malloc and free.  \nLinked List Example in C  \ntypedef struct  node {  \nint contents ;  \nstruct  node *next ;  \n} node ;  \nnode *p head = NULL ;  \nnode *np = malloc ( sizeof(node) ) ; np->contents = 800 ; np->next = p head ; p head = np ;  \nnp = malloc ( sizeof(node) ) ; np->contents = 150 ;  \nnp->next = p head ; p head = np ;  \nnp = malloc ( sizeof(node) ) ; np->contents = 100 ;  \nnp->next = p head ; p head = np ;  \nLinked List Example in C  \ntypedef struct  node { int contents ;  \nstruct  node *next ;} node ;  \nDefinition of the node type with a field to hold information (contents) and a pointer to the next node. NULL will mark the list end.  \nnode *p head = NULL ;  \nnode *np = malloc( sizeof(node) ) ; np->contents = 800 ; np->next = p head ; p head = np ;  \nnp = malloc( sizeof(node) ) ; np->contents = 150 ;  \nnp->next = p head ; p head = np ;  \nnp = malloc( sizeof(node) ) ; np->contents = 100 ;  \nnp->next = p head ; p head = np ;  \nLinked List Example in C  \ntypedef struct  node { int contents ;  \nstruct  node *next ;} node ;  \np_head = NULL for the initial (empty) list.  \nnode *p head = NULL ;  \nnode *np = malloc ( sizeof(node) ) ; np->contents = 800 ; np->next = p head ; p head = np ;  \nnp = malloc ( sizeof(node) ) ; np->contents = 150 ;  \nnp->next = p head ; p head = np ;  \nnp = malloc ( sizeof(node) ) ; np->contents = 100 ;  \nnp->next = p head ; p head = np ;","cbCaisa8Iuc5Da6T","https://ap.wps.com/l/cbCaisa8Iuc5Da6T","pdf",232828,17,"English","# Personal Software Engineering\n## But First-How Much Space Is Needed?\n## sizeof for basic types and pointers\n## sizeof for array types and function parameters\n## sizeof for structs and padding\n## What are lists?\n## Singly Linked Lists\n## Linked List Example in C","[{\"question\":\"为什么在 C 里需要使用 sizeof 来估算空间？\",\"answer\":\"因为不同类型（如 int、double、struct 等）需要的字节数不同。sizeof 用于获取保存某个类型或变量所需的字节数。\"},{\"question\":\"为什么函数参数中的数组形参 sizeof(buffer) 结果会与预期不同？\",\"answer\":\"因为数组实参在函数形参中会退化为指针。示例中 void foo(char buffer[81]) 等价于 void foo(char *buffer)，因此 sizeof(buffer) 取的是指针大小。\"},{\"question\":\"链表相比数组有什么优势？\",\"answer\":\"数组需要连续的内存空间且位置固定，而链表的每个节点可在内存中处于不同位置。链表能更灵活地动态增长和缩小，并可使用 malloc/free 分配释放节点空间。\"}]","08-C-Lists - Personal Software Engineering - Lists in C - But First-How Much Space Is Needed? | PDF",43,[146,150],{"page_no":22,"url":147,"width":148,"height":149},"https://res-academy.cache.wpscdn.com/upload/kdocshare/2026-11-09/ca3a810ff243faa2d565564918d33f81.webp",983,737,{"page_no":26,"url":151,"width":152,"height":153},"https://res-academy.cache.wpscdn.com/upload/kdocshare/2026-11-09/51919be01ff72cc26529aa9c5f937f5b.webp",982,736,{"list":155,"token":11},[156,163,169,175,182,188,194,201,207,214],{"doc_id":157,"doc_title":158,"file_type":137,"home_preview_img_url":159,"slug":160,"view_count":161,"category_name":29,"create_tm":162},407800,"计算机组成原理","https://weboffice-temporary-prod-sg.obs.ap-southeast-3.myhuaweicloud.com:443/export/thumbnail/export/thumbnail/110229921169418/last/300/407?AWSAccessKeyId=HPUAE6OPDRI9QZXR6WJG&Expires=1796178419&Signature=kWEJA3lZVkqqJCR8Yr2y%2BzxuwD8%3D","computer-organization-principles",37,1790576093,{"doc_id":164,"doc_title":165,"file_type":137,"home_preview_img_url":166,"slug":167,"view_count":26,"category_name":29,"create_tm":168},732129,"PSoC® Mixed-Signal Array - Functional Overview","https://weboffice-temporary-prod-sg.obs.ap-southeast-3.myhuaweicloud.com:443/export/thumbnail/export/thumbnail/110230031401986/last/300/407?AWSAccessKeyId=HPUAE6OPDRI9QZXR6WJG&Expires=1796178419&Signature=b9ezeN0ZqMohR%2B8cAwPfk94CFfY%3D","psoc-mixed-signal-array-functional-overview",1795378523,{"doc_id":170,"doc_title":171,"file_type":137,"home_preview_img_url":172,"slug":173,"view_count":123,"category_name":29,"create_tm":174},664785,"SurTrac Aluminum Cantilever Gate System - Specifications - Performance by Design","https://weboffice-temporary-prod-sg.obs.ap-southeast-3.myhuaweicloud.com:443/export/thumbnail/export/thumbnail/110229983098267/last/300/407?AWSAccessKeyId=HPUAE6OPDRI9QZXR6WJG&Expires=1796178419&Signature=X22EB1hhuykI8u1ZFyf%2BM350LOc%3D","surtrac-aluminum-cantilever-gate-system-specifications-performance-by-design",1793272487,{"doc_id":176,"doc_title":177,"file_type":137,"home_preview_img_url":178,"slug":179,"view_count":180,"category_name":29,"create_tm":181},18759,"Coding For Absolute Beginners And Cybersecurity","https://weboffice-temporary-prod-sg.obs.ap-southeast-3.myhuaweicloud.com:443/export/thumbnail/export/thumbnail/110229708598152/last/300/407?AWSAccessKeyId=HPUAE6OPDRI9QZXR6WJG&Expires=1796178419&Signature=MLzxqXRN7Y%2B6kbh7hFCmQSvNpXU%3D","coding-for-absolute-beginners-and-cybersecurity",51,1777070335,{"doc_id":183,"doc_title":184,"file_type":137,"home_preview_img_url":185,"slug":186,"view_count":33,"category_name":29,"create_tm":187},664318,"Eclipse P600s Manual - v1.2","https://weboffice-temporary-prod-sg.obs.ap-southeast-3.myhuaweicloud.com:443/export/thumbnail/export/thumbnail/110229982183544/last/300/407?AWSAccessKeyId=HPUAE6OPDRI9QZXR6WJG&Expires=1796178419&Signature=DOfMitdgajbTvqTWGArwksyyBqQ%3D","eclipse-p600s-manual-v12",1793251589,{"doc_id":189,"doc_title":190,"file_type":137,"home_preview_img_url":191,"slug":192,"view_count":30,"category_name":29,"create_tm":193},720440,"DHT11 Temperature and Humidity Sensor - Specification - Connecting Diagram - Sample Code","https://weboffice-temporary-prod-sg.obs.ap-southeast-3.myhuaweicloud.com:443/export/thumbnail/export/thumbnail/110230024396067/last/300/407?AWSAccessKeyId=HPUAE6OPDRI9QZXR6WJG&Expires=1796178419&Signature=n6fgyIjDcwPENYINNG9zhBeFo4o%3D","dht11-temperature-and-humidity-sensor-specification-connecting-diagram-sample-code",1795037377,{"doc_id":195,"doc_title":196,"file_type":137,"home_preview_img_url":197,"slug":198,"view_count":199,"category_name":29,"create_tm":200},655631,"UML Class Diagram - Overview, Components, and Relationships","https://weboffice-temporary-prod-sg.obs.ap-southeast-3.myhuaweicloud.com:443/export/thumbnail/export/thumbnail/110229975286400/last/300/407?AWSAccessKeyId=HPUAE6OPDRI9QZXR6WJG&Expires=1796178419&Signature=gNHuRvKb%2Fq5OdaUmT%2B39Boka6W8%3D","uml-class-diagram-overview-components-and-relationships",18,1792962953,{"doc_id":202,"doc_title":203,"file_type":137,"home_preview_img_url":204,"slug":205,"view_count":33,"category_name":29,"create_tm":206},679456,"STACK RECLAIMER CHAINS - Stack Reclaimers Overview - Engineering Chain Division","https://weboffice-temporary-prod-sg.obs.ap-southeast-3.myhuaweicloud.com:443/export/thumbnail/export/thumbnail/110229996515967/last/300/407?AWSAccessKeyId=HPUAE6OPDRI9QZXR6WJG&Expires=1796178419&Signature=q5eiC5nbdQa5BUNWjzz1LoAqmw8%3D","stack-reclaimer-chains-stack-reclaimers-overview-engineering-chain-division",1793858747,{"doc_id":208,"doc_title":209,"file_type":137,"home_preview_img_url":210,"slug":211,"view_count":212,"category_name":29,"create_tm":213},203511,"Snack Innovations - Dynamic FRM 公式与配方管理解决方案","https://weboffice-temporary-prod-sg.obs.ap-southeast-3.myhuaweicloud.com:443/export/thumbnail/export/thumbnail/110229882938768/last/300/407?AWSAccessKeyId=HPUAE6OPDRI9QZXR6WJG&Expires=1796178419&Signature=8JW2TxOVyNDKMRu2mFCayST561U%3D","snack-innovations-dynamic-frm-formula-and-recipe-management-solution",12,1788561606,{"doc_id":215,"doc_title":216,"file_type":137,"home_preview_img_url":217,"slug":218,"view_count":212,"category_name":29,"create_tm":219},210961,"SSND10S - LOW SPEED（CONTAINER HANDLING）NON-DRIVE STEER AXLE 规格尺寸图","https://weboffice-temporary-prod-sg.obs.ap-southeast-3.myhuaweicloud.com:443/export/thumbnail/export/thumbnail/110229884551147/last/300/407?AWSAccessKeyId=HPUAE6OPDRI9QZXR6WJG&Expires=1796178419&Signature=SIfRhxmO%2FjiVbwkHKUdf1uBLqww%3D","ssnd10s-low-speed-container-handling-non-drive-steer-axle-dimension-drawing",1788676701]