مخصصة الساحة المسلحة الفردية. C89 متوافق.
arena.h يعد مخصصو Arena طريقة بسيطة لتحقيق إدارة الذاكرة الديناميكية بشكل أسهل وأسرع وأكثر أمانًا من خلال السماح بتحرير مخصصات متعددة كمجموعة. يتم ذلك عن طريق تخصيص الذاكرة في المناطق الكبيرة ثم توزيع أجزاء من تلك الذاكرة حسب الحاجة ، مما يقلل من كمية مكالمات malloc (والتي تكون بطيئة مقارنة بحساب المؤشر البسيط).
عندما تدمر الساحة ، فأنت تحرره أيضًا وجميع محتوياته ، مما يقلل من كمية المكالمات free التي تكون بطيئة أيضًا. المضي قدمًا ، يمكنك مسح الساحات ببساطة عن طريق إعادة تعيين مؤشرات الذاكرة الخاصة بهم إلى 0 ، مما يتيح لك إعادة استخدامها والتخلص من الحاجة إلى المزيد من malloc و free 's.
يمكنك معرفة المزيد عن مخصصات Arena/Zone/Region من خلال قراءة هذا المقال الرائع.
سأبقي هذا قصير. أحافظ على الامتثال C89 للمتعة ، ليس لأنني أستخدمه. أنا شخصياً متعة C11. إذا كنت تعتقد أن C89 هو الطريقة الوحيدة ، حسنًا ، جيد لك! لكنك مخطئ.
عندما أشارك هذا المشروع مع المبرمجين الآخرين ، فإن أحد أكثر الردود شيوعًا التي أتلقاها هو شيء على غرار: يجب ألا تضع رمز التنفيذ/المنطق أبدًا في ملف رأس! أواجه مشكلة في هذا البيان لثلاثة أسباب ...
إنه يوضح أن البناء الضار الذي عفا عليه الزمن ، ونتيجة لذلك ، لا يزال يتم تطبيقه في نظام التعليم ، حيث يتم تقديم البناء المذكور عادة.
نادراً ما يكون للشخص الذي يدلي هذا البيان سببًا فعليًا للاعتقاد به. هل يفكر هذا الشخص أبدًا في سبب "عدم وضع رمز التنفيذ/المنطق أبدًا في ملف رأس"؟ ببساطة تجدد ما سمعوه دون أي أساس لسبب اختيارهم للاتفاق عليه لا يساعدني بأي شكل من الأشكال ، ويجب ألا يتوقع هذا الشخص أن أقبله كما فعلوا.
إن أكبر وأكثر الانتقادات الصالحة لمكتبات الرأس/الرئاسة الواحدة هو أن التغيير في الرأس يتطلب إعادة تجميع جميع الملفات التي تتضمنها. في حالة مشروعي ، فإن إجراء تغييرات على arena.h ، على الرغم من أن التنفيذ الفعلي موجود فقط في وحدة الترجمة بأن #define 's The ARENA_IMPLEMENTATION Macro ، سيؤدي إلى إعادة بناء جميع الملفات التي تتضمنها. الحل؟ بمجرد أن يكون arena.h في الحالة المطلوبة ، توقف عن إجراء التغييرات!
الارتباط بطيئة ومعقدة. غالبًا ما يكافح العديد من المبتدئين في تعلم عملية الربط ، كما أنهم أيضًا أكبر سبب عندما يتعلق الأمر بكتابة رمز غير آمن. هذا وحده سبب كافٍ بالنسبة لي لجعل هذا الساحة مخصصًا لمكتبة رأس فقط. الكود الخاص بي (في حالته الحالية) صغير جدًا ، حوالي 300 سطر. لماذا أجعلك تبني وربط مثل هذا التنفيذ الصغير عندما يمكنك ببساطة #include مرة واحدة والبدء في استخدامه خارج الصندوق؟ إذا كان لديك مشكلة في ذلك ، فإن هذا المخصص في تنسيق الرئاسة الواحد لا يمنعك من اتباع بناء المصدر+رأس إذا كنت ترغب في ذلك. هيك ، لا تتردد في توتره وجعله مصدر+رأس ، إنه مفتوح المصدر لسبب ما!
لا ينفذ هذا مخصصًا على مستوى kernel ، ولكن بدلاً من ذلك يلتف malloc و free (مكتبة قياسية أو مخصصة ، اختيارك).
على الرغم من أنني أعتقد أنه يجب أن يكون البرنامج مفتوحًا ، إلا أنني لا أعتقد أنه من الأخلاقي طلب برنامج يستخدم هذه المكتبة ليكون أيضًا مفتوحًا. في العصر الحديث للتكنولوجيا والحالة الحالية للعالم ، يعد كتابة رمز آمن الذاكرة أكثر أهمية من أي وقت مضى. لهذا السبب ، يتم ترخيص هذا البرنامج بموجب إصدار ترخيص Apache 2.0. يتم تشجيعك بشدة على قراءة LICENSE (المدرجة أدناه ، في arena.h ، وملفها الخاص في هذا الريبو) إذا كنت تفكر في استخدام البرنامج ، إلا إذا كنت تعتقد أنك على دراية بنسبة 100 ٪ بالشروط والأحكام.
Apache License
Version 2.0, January 2004
http://www.apache.org/licenses/
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
1. Definitions.
"License" shall mean the terms and conditions for use, reproduction,
and distribution as defined by Sections 1 through 9 of this document.
"Licensor" shall mean the copyright owner or entity authorized by
the copyright owner that is granting the License.
"Legal Entity" shall mean the union of the acting entity and all
other entities that control, are controlled by, or are under common
control with that entity. For the purposes of this definition,
"control" means (i) the power, direct or indirect, to cause the
direction or management of such entity, whether by contract or
otherwise, or (ii) ownership of fifty percent (50%) or more of the
outstanding shares, or (iii) beneficial ownership of such entity.
"You" (or "Your") shall mean an individual or Legal Entity
exercising permissions granted by this License.
"Source" form shall mean the preferred form for making modifications,
including but not limited to software source code, documentation
source, and configuration files.
"Object" form shall mean any form resulting from mechanical
transformation or translation of a Source form, including but
not limited to compiled object code, generated documentation,
and conversions to other media types.
"Work" shall mean the work of authorship, whether in Source or
Object form, made available under the License, as indicated by a
copyright notice that is included in or attached to the work
(an example is provided in the Appendix below).
"Derivative Works" shall mean any work, whether in Source or Object
form, that is based on (or derived from) the Work and for which the
editorial revisions, annotations, elaborations, or other modifications
represent, as a whole, an original work of authorship. For the purposes
of this License, Derivative Works shall not include works that remain
separable from, or merely link (or bind by name) to the interfaces of,
the Work and Derivative Works thereof.
"Contribution" shall mean any work of authorship, including
the original version of the Work and any modifications or additions
to that Work or Derivative Works thereof, that is intentionally
submitted to Licensor for inclusion in the Work by the copyright owner
or by an individual or Legal Entity authorized to submit on behalf of
the copyright owner. For the purposes of this definition, "submitted"
means any form of electronic, verbal, or written communication sent
to the Licensor or its representatives, including but not limited to
communication on electronic mailing lists, source code control systems,
and issue tracking systems that are managed by, or on behalf of, the
Licensor for the purpose of discussing and improving the Work, but
excluding communication that is conspicuously marked or otherwise
designated in writing by the copyright owner as "Not a Contribution."
"Contributor" shall mean Licensor and any individual or Legal Entity
on behalf of whom a Contribution has been received by Licensor and
subsequently incorporated within the Work.
2. Grant of Copyright License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
copyright license to reproduce, prepare Derivative Works of,
publicly display, publicly perform, sublicense, and distribute the
Work and such Derivative Works in Source or Object form.
3. Grant of Patent License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
(except as stated in this section) patent license to make, have made,
use, offer to sell, sell, import, and otherwise transfer the Work,
where such license applies only to those patent claims licensable
by such Contributor that are necessarily infringed by their
Contribution(s) alone or by combination of their Contribution(s)
with the Work to which such Contribution(s) was submitted. If You
institute patent litigation against any entity (including a
cross-claim or counterclaim in a lawsuit) alleging that the Work
or a Contribution incorporated within the Work constitutes direct
or contributory patent infringement, then any patent licenses
granted to You under this License for that Work shall terminate
as of the date such litigation is filed.
4. Redistribution. You may reproduce and distribute copies of the
Work or Derivative Works thereof in any medium, with or without
modifications, and in Source or Object form, provided that You
meet the following conditions:
(a) You must give any other recipients of the Work or
Derivative Works a copy of this License; and
(b) You must cause any modified files to carry prominent notices
stating that You changed the files; and
(c) You must retain, in the Source form of any Derivative Works
that You distribute, all copyright, patent, trademark, and
attribution notices from the Source form of the Work,
excluding those notices that do not pertain to any part of
the Derivative Works; and
(d) If the Work includes a "NOTICE" text file as part of its
distribution, then any Derivative Works that You distribute must
include a readable copy of the attribution notices contained
within such NOTICE file, excluding those notices that do not
pertain to any part of the Derivative Works, in at least one
of the following places: within a NOTICE text file distributed
as part of the Derivative Works; within the Source form or
documentation, if provided along with the Derivative Works; or,
within a display generated by the Derivative Works, if and
wherever such third-party notices normally appear. The contents
of the NOTICE file are for informational purposes only and
do not modify the License. You may add Your own attribution
notices within Derivative Works that You distribute, alongside
or as an addendum to the NOTICE text from the Work, provided
that such additional attribution notices cannot be construed
as modifying the License.
You may add Your own copyright statement to Your modifications and
may provide additional or different license terms and conditions
for use, reproduction, or distribution of Your modifications, or
for any such Derivative Works as a whole, provided Your use,
reproduction, and distribution of the Work otherwise complies with
the conditions stated in this License.
5. Submission of Contributions. Unless You explicitly state otherwise,
any Contribution intentionally submitted for inclusion in the Work
by You to the Licensor shall be under the terms and conditions of
this License, without any additional terms or conditions.
Notwithstanding the above, nothing herein shall supersede or modify
the terms of any separate license agreement you may have executed
with Licensor regarding such Contributions.
6. Trademarks. This License does not grant permission to use the trade
names, trademarks, service marks, or product names of the Licensor,
except as required for reasonable and customary use in describing the
origin of the Work and reproducing the content of the NOTICE file.
7. Disclaimer of Warranty. Unless required by applicable law or
agreed to in writing, Licensor provides the Work (and each
Contributor provides its Contributions) on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied, including, without limitation, any warranties or conditions
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
PARTICULAR PURPOSE. You are solely responsible for determining the
appropriateness of using or redistributing the Work and assume any
risks associated with Your exercise of permissions under this License.
8. Limitation of Liability. In no event and under no legal theory,
whether in tort (including negligence), contract, or otherwise,
unless required by applicable law (such as deliberate and grossly
negligent acts) or agreed to in writing, shall any Contributor be
liable to You for damages, including any direct, indirect, special,
incidental, or consequential damages of any character arising as a
result of this License or out of the use or inability to use the
Work (including but not limited to damages for loss of goodwill,
work stoppage, computer failure or malfunction, or any and all
other commercial damages or losses), even if such Contributor
has been advised of the possibility of such damages.
9. Accepting Warranty or Additional Liability. While redistributing
the Work or Derivative Works thereof, You may choose to offer,
and charge a fee for, acceptance of support, warranty, indemnity,
or other liability obligations and/or rights consistent with this
License. However, in accepting such obligations, You may act only
on Your own behalf and on Your sole responsibility, not on behalf
of any other Contributor, and only if You agree to indemnify,
defend, and hold each Contributor harmless for any liability
incurred by, or claims asserted against, such Contributor by reason
of your accepting any such warranty or additional liability.
END OF TERMS AND CONDITIONS
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Copyright 2024 Carter Dugan
يمكن العثور على توثيق لمخصص الساحة في arena.h . هناك تعليق في الجزء العلوي من ملف الرأس مع تعليمات سريعة للاستخدام لراحتك.
للحصول على ملف واحد في وحدة ترجمة واحدة ، تحتاج إلى ما يلي:
#define ARENA_IMPLEMENTATION
#include "arena.h" من هذه النقطة ، يمكن للملفات الأخرى في وحدات الترجمة الأخرى ببساطة #include "arena.h" بشكل طبيعي. هناك وحدات ماكرو إضافية يمكنك تحديدها/استخدامها. انظر القسم الخاص بالوظائف ووحدات الماكرو.
هناك اثنين من الهياكل المحددة في arena.h . هذا يسرد كل واحد جنبا إلى جنب مع أعضائه.
Arena_Allocation بنية البيانات لتخصيص الساحة. متاح فقط عند تعريف ARENA_DEBUG .
size_t index الفهرس في الساحة التي يوجد فيها بداية التخصيص.size_t size حجم الذاكرة المخصصة لهذا التخصيص في البايتات.char *pointer المؤشر المرتبط بالتخصيص.struct Arena_Allocation_s *next التخصيص التالي في القائمة المرتبطة. Arena بنية البيانات للساحة.
char *region منطقة الذاكرة المخصصة.size_t index فهرس المنطقة ليتم توزيع المؤشر التالي.size_t size حجم الذاكرة المخصصة للساحة في البايتات.unsigned long allocations عدد المخصصات التي تم تقديمها. متاح فقط عندما يتم تعريف ARENA_DEBUG .Arena_Allocation *head_allocation أول تخصيص تم إجراؤه في الساحة (المستخدم في قائمة مرتبطة). متاح فقط عندما يتم تعريف ARENA_DEBUG . /*
Allocate and return a pointer to memory to the arena
with a region with the specified size. Providing a
size of zero results in a failure.
Parameters:
size_t size | The size (in bytes) of the arena
memory region.
Return:
Pointer to arena on success, NULL on failure
*/
Arena * arena_create ( size_t size );
/*
Return a pointer to a portion of specified size of the
specified arena's region. Nothing will restrict you
from allocating more memory than you specified, so be
mindful of your memory (as you should anyways) or you
will get some hard-to-track bugs. By default, memory is
aligned by alignof(size_t), but you can change this by
#defining ARENA_DEFAULT_ALIGNMENT before #include'ing
arena.h. Providing a size of zero results in a failure.
Parameters:
Arena *arena | The arena of which the pointer
from the region will be
distributed
size_t size | The size (in bytes) of
allocated memory planned to be
used.
Return:
Pointer to arena region segment on success, NULL on
failure.
*/
ARENA_INLINE void * arena_alloc ( Arena * arena , size_t size );
/*
Same as arena_alloc, except you can specify a memory
alignment for allocations.
Return a pointer to a portion of specified size of the
specified arena's region. Nothing will restrict you
from allocating more memory than you specified, so be
mindful of your memory (as you should anyways) or you
will get some hard-to-track bugs. Providing a size of
zero results in a failure.
Parameters:
Arena *arena | The arena of which the pointer
from the region will be
distributed
size_t size | The size (in bytes) of
allocated memory planned to be
used.
unsigned int alignment | Alignment (in bytes) for each
memory allocation.
Return:
Pointer to arena region segment on success, NULL on
failure.
*/
void * arena_alloc_aligned ( Arena * arena , size_t size , unsigned int alignment );
/*
Copy the memory contents of one arena to another.
Parameters:
Arena *src | The arena being copied, the source.
Arena *dest | The arena being copied to. Must be created/allocated
already.
Return:
Number of bytes copied.
*/
ARENA_INLINE size_t arena_copy ( Arena * dest , Arena * src );
/*
Reset the pointer to the arena region to the beginning
of the allocation. Allows reuse of the memory without
realloc or frees.
Parameters:
Arena *arena | The arena to be cleared.
*/
ARENA_INLINE void arena_clear ( Arena * arena );
/*
Free the memory allocated for the entire arena region.
Parameters:
Arena *arena | The arena to be destroyed.
*/
ARENA_INLINE void arena_destroy ( Arena * arena );
/*
Returns a pointer to the allocation struct associated
with a pointer to a segment in the specified arena's
region.
Parameters:
Arena *arena | The arena whose region should
have a portion pointed to by
ptr.
void *ptr | The ptr being searched for
within the arena in order to
find an allocation struct
associated with it.
*/
Arena_Allocation * arena_get_allocation_struct ( Arena * arena , void * ptr );
/*
Adds an arena allocation to the arena's linked list of
allocations under debug.
Parameters:
Arena *arena | The arena whose allocation list
should be added to
size_t size | The size of the allocation being
added.
*/
void arena_add_allocation ( Arena * arena , size_t size );
/*
Deletes the arena's linked list of allocations under
debug.
Parameters:
Arena *arena | The arena whose allocation list
is being deleted.
*/
void arena_delete_allocation_list ( Arena * arena ); في الكود الخاص بك ، يمكنك تحديد بعض وحدات الماكرو الاختيارية. ARENA_MALLOC و ARENA_FREE و ARENA_MEMCPY يمكن تخصيصها في وظائف malloc البديلة ، مثلها ، free ، على memcpy ، وسيستخدمها arena.h بدلاً من وظائف المكتبة القياسية. يمكنك الوصول إلى وظائف تصحيح إضافية لتتبع المخصصات من خلال تحديد ARENA_DEBUG . أخيرًا ، يمكنك أيضًا تحديد قيمة افتراضية لمحاذاة التخصيص من خلال تحديد قيمة لـ ARENA_DEFAULT_ALIGNMENT . انظر أدناه للحصول على أمثلة.
// All of these are optional
// Replace standard library functions
#define ARENA_MALLOC <stdlib_malloc_like_allocator>
#define ARENA_FREE <stdlib_free_like_deallocator>
#define ARENA_MEMCPY <stdlib_memcpy_like_copier>
// for debug functionality:
#define ARENA_DEBUG
// If you would like to change the default alignment for
// allocations:
#define ARENA_DEFAULT_ALIGNMENT <alignment_value> هناك أيضًا ماكرو لتحديد محاذاة الأنواع. مثل كل شيء آخر ، فهو أيضًا صديق C89 ، على الرغم من أنه عند التجميع تحت C11 ، فإنه سيستخدم stdalign.h 's alignof .
ARENA_ALIGNOF ( type ) // Gives alignment of `type` تمت كتابة الرمز للبناء مع أي مترجم يدعم معيار C89 ويعمل على أي منصة. ومع ذلك ، هناك بعض المشكلات في البناء على Windows مع Makefile . يرجى قراءة جميع المعلومات أدناه.
تم تجميع الاختبارات والأمثلة وتشغيلها بنجاح تحت المترجمين والإصدارات التالية:
تم تجميع الاختبارات والأمثلة وتشغيلها بنجاح على أنظمة التشغيل التالية:
لاحظ أن Makefile لم يتم تكوينه حاليًا للعمل على Windows عند تشغيل $ make test بسبب استخدام Valgrind وغيار ملحقات .exe . يجب أن يكون هذا إصلاحًا بسيطًا وهو موجود في قائمة TODO الخاصة بي (لا تتردد في فتح مشكلة وإصلاح هذا بنفسك!).
يحتوي هذا المشروع على إرشادات بسيطة للغاية للمساهمة.
لأي مساهمات في مدونة arena.h ، يرجى فتح طلب سحب فقط إذا كنت تعالج مشكلة لتلبية طلب ميزة أو إصلاح خطأ . اتبع نمط الكود وقم بتشغيل الاختبارات الآلية قبل فتح طلب سحب. يجب اجتياز جميع الاختبارات.
لطلبات الميزات أو الأخطاء ، يرجى فتح مشكلة . يمكنك بعد ذلك معالجة هذه المشكلة بنفسك.
لأي شيء آخر ، افتح مشكلة وسنناقشها.
arena.hفي الوقت الحالي ، لا توجد وثائق لأسلوب الكود ، ولكن يجب أن تكون بسيطة نسبيًا بما يكفي لالتقاطها من خلال قراءة التعليمات البرمجية الحالية لمعظم الأشياء. إذا كنت تواجه مشكلة ، فلا تتردد في فتح مشكلة ل FR. إذا كان موجودًا بالفعل ، فقم بالتعليق عليه واصفًا ما تربكه.
إذا قمت بتعديل arena.h على الإطلاق ، فيجب عليك إجراء الاختبارات. انظر القسم التالي.
إذا قمت بإضافة ميزة داخل arena.h ، فيجب عليك إنشاء اختبار أو اختبارات كافية في test.c
إذا قمت بإضافة ميزة داخل arena.h ، فيجب عليك مثالًا كافيًا في code_examples/ وأضفها إلى makefile ، لكن ليس مطلوبًا.
إذا قمت بتغيير arena.h على الإطلاق ، قم بإجراء الاختبارات قبل فتح العلاقات العامة . إذا فتحت العلاقات العامة مع تعديلات على الكود ولم تمر الاختبارات ، فدعم على العلاقات العامة التي تفيد الاختبار الذي تعتقد أنه خاطئ ويمنعك من اجتياز جميع الاختبارات. إذا فشل أي اختبار ولم يكن لدى PR الخاص بك تعليق يدعي تصحيح الاختبار الفاشل ، فسيتم تجاهل العلاقات العامة الخاصة بك.
خارج معالجة الأخطاء وطلبات الميزات ، تلبية طلب ميزة أو إصلاح الأخطاء للوظائف داخل arena.h يسمح بتعديل أو إضافة رمز الاختبار ذي الصلة داخل test.c ، ويجب أن تفعل ذلك إذا كنت تريد الاعتراف بالعلاقات العامة الخاصة بك. هناك وثائق للاختبار رمز داخل test.c في الجزء العلوي من الملف في شكل تعليقات.
يجب أن تمر الاختبارات أيضًا عبر Valgrind Free ، ويجب أن تكون arena.h متوافقة مع C89. يجب عليك التحقق من ذلك باستخدام Makefile ، ولكن إذا لم تستطع أو لا ترغب في ذلك أو لا ترغب في test.c .
-Werror -Wall -Wextra -Wstrict-prototypes -Wold-style-definition -Wmissing-prototypes -Wmissing-declarations -Wdeclaration-after-statement
وللتوافق ، تجميع test_compliance.c مع
-pedantic -std=c89 -Werror -Wall -Wextra -Wstrict-prototypes -Wold-style-definition -Wmissing-prototypes -Wmissing-declarations -Wdeclaration-after-statement
كما قلت ، يمكنك أن تفعل كل هذا مع Makefile
$ make test
المعرفات
variable_name . بالنسبة إلى المؤشرات ، يجب إرفاق العلامة النجمية ( * ) بالاسم المتغير ، وليس النوع ، eg type *variable_name .type function_name(p1, p2, ...) . في حالة المؤشرات ، يجب إرفاق العلامة النجمية ( * ) بنوع الكثافة ، eg type* function_name(p1, p2, ...) . يجب أن يكون للوظائف إعلان إلى الأمام مع تعليق للوثائق أعلاه حول الجزء العلوي من الملف ، تأكد من أن الطلب بالنسبة للوظائف الأخرى متسقة.struct StructName . يجب أن تكون typedef'd وتقع حول الجزء العلوي من ملف الرأس.ARENA_ ، على سبيل المثال ARENA_MACRO_NAME . يرجى تجنب إضافة وحدات الماكرو دون استشارة لي أولاً . لا تتردد في إجراء تغييرات على وحدات الماكرو الموجودة.تعتمد المساحة البيضاء على أهمية خط الكود لمن حوله. إذا كنت لا تفهم معنى هذه النقاط ، فيرجى إلقاء نظرة على الرمز. يجب تنسيقه على النحو التالي:
يجب أن يتم التحقق من الخطأ كلما كان ذلك ممكنًا وتقليد سلوك تطبيقات المكتبة القياسية ، مثل إرجاع NULL on Error في الوظائف التي ترجع مؤشرات أو إرجاع قيم خطأ عدد صحيح من وظائف عدد صحيح. يجب عليك استخدام التحقق من الأخطاء المبكرة ، مما يعني التحقق من الأخطاء بمجرد إنتاجها ، على سبيل المثال. التحقق من NULL عاد بعد مكالمة malloc فاشلة.
يجب أن تصف التعليقات سبب قيامك بشيء ما ، وليس ما فعلته. بمعنى آخر ، يجب أن يكون الكود الخاص بك محسوسًا ذاتيًا. يجب أن تكون الوثائق للوظائف في شكل تعليقات فوق الوظيفة بالتنسيق التالي:
/*
Description of function, description of function description of function.
Description of function description of function, description of function
description of function.
Parameters:
paramter1_type paramter1_name | Description of parameter 1, description
of parameter one description of paramter
1.
paramter2_type paramter2_name | Description of parameter 2, description
of parameter one description of paramter
2.
Return:
Description of return value, description of return value description of return value.
*/