Upgrade to Pro — share decks privately, control downloads, hide ads and more …

今日から使えるC11

 今日から使えるC11

C11の新機能の紹介です

9361878d459f1709feec780518946ee5?s=128

NARUSE, Yui

May 11, 2016
Tweet

More Decks by NARUSE, Yui

Other Decks in Programming

Transcript

  1. ࠓ೔͔Β࢖͑ΔC11 naruse

  2. C Standards w ,3 w $ $3VCZ͸ݪଇ$ͱ͞Ε͍ͯΔ  w $

    w $ w $Y
  3. C99ͷ৽ػೳ • ΠϯϥΠϯؔ਺ • Mixed declarations and code • enumએݴͰͷ৑௕ͳΧϯ

    Ϛ • Մม௕഑ྻ • ՄมҾ਺ϚΫϩ • //ίϝϯτ • __func__ • long long, intXX_t,boolͳͲ ͷܕ • snprintfͳͲͷؔ਺ • ܕ૯শ਺ֶؔ਺ (tgmath.h)
  4. C11 Changes • conditional (optional) features (including some that were

    previously mandatory) • support for multiple threads of execution including an improved memory sequencing model, atomic objects, and thread-local storage (<stdatomic.h> and <threads.h>) • additional floating-point characteristic macros (<float.h>) • querying and specifying alignment of objects (<stdalign.h>, <stdlib.h>) • Unicode characters and strings (<uchar.h>) (originally specified in ISO/IEC TR 19769:2004) • type-generic expressions • static assertions • anonymous structures and unions • no-return functions • macros to create complex numbers (<complex.h>) • support for opening files for exclusive access • removed the gets function (<stdio.h>) • added the aligned_alloc, at_quick_exit, and quick_exit functions (<stdlib.h>) • (conditional) support for bounds-checking interfaces (originally specified in • ISO/IEC TR 24731−1:2007) • (conditional) support for analyzability
  5. C11 • getsͷ࡟আ • Bounds-checking interfaces • fopen(“foo.txt”,”wx”) • _Alignas,

    alignof, aligned_alloc • _Noreturn • _Generic • threads.h • stdatomic.h • ແ໊ߏ଄ମɾແ໊union • _Static_assert
  6. getsͷ࡟আ • 2007೥ʹඇਪ঑ͱͳ͍͕͍ͬͯͨͭʹ࡟আ • char *gets(char *s); • ‘\n’·ͰಡΈࠐΉ •

    → ֬อ͢΂͖όοϑΝͷ௕͕͞ࣄલʹΘ͔Βͳ͍ • →όοϑΝΦʔόʔϥϯෆՄආ
  7. Bounds-checking interfaces • ෟଐॻK (normative) • rsize_tܕ • _sγϦʔζ

  8. rsize_t • sizeof(rsize_t) == sizeof(size_t) • RSIZE_MAX͸(SIZE_MAX >>1)΍࠷΋େ͖ͳ ΦϒδΣΫτͷαΠζΛఆٛ͢Δ͜ͱ͕ਪ঑ •

    https://www.jpcert.or.jp/sc-rules/c-int01-c.html
  9. _sγϦʔζ • Microsoft༝དྷ (͕ͩ࣌ʑطଘͷMSͷͱએݴ͕ ҧ͏) • ISO/IEC TR 24731-1ΛܦͯෟଐॻKʹऩ࿥ •

    70ݸͷ*_sͱ͍͏໊લͷؔ਺
  10. • abort_handler_s • asctime_s • bsearch_s • ctime_s • fopen_s

    • fprintf_s • freopen_s • fscanf_s • fwprintf_s • fwscanf_s • getenv_s • gets_s • gmtime_s • ignore_handler_s • L_tmpnam_s • localtime_s • mbsrtowcs_s • mbstowcs_s • memcpy_s • memmove_s • memset_s • printf_s • qsort_s • scanf_s • set_constraint_h andler_s • snprintf_s • snwprintf_s • sprintf_s • sscanf_s • strcat_s • strcpy_s • strerror_s • strerrorlen_s • strncat_s • strncpy_s • strnlen_s • strtok_s • swprintf_s • swscanf_s • TMP_MAX_S • tmpfile_s • tmpnam_s • vfprintf_s • vfscanf_s • vfwprintf_s • vfwscanf_s • vprintf_s • vscanf_s • vsnprintf_s • vsnwprintf_s • vsprintf_s • vsscanf_s • vsnprintf_s • vsnwprintf_s • vsprintf_s • vsscanf_s • vswprintf_s • vswscanf_s • vwprintf_s • vwscanf_s • wcrtomb_s • wcscat_s • wcscpy_s • wcsncat_s • wcsncpy_s • wcsnlen_s • wcsrtombs_s • wcstok_s • wcstombs_s • wctomb_s • wmemcpy_s • wmemmove_s • wprintf_s • wscanf_s
  11. fopen( , “x”) • open(2) (POSIX)ʹ͋ΔO_EXCLͷɺfopen൛ • wx, wbx, w+x,

    w+bx, wb+x • ϑΝΠϧ͕طʹ͋ͬͨΒΤϥʔʹ͢Δ
  12. ΞϥΠϝϯτؔ࿈ • _Alignas (alignas): __attribute__((align)) Ͱ͢ • _Alignof (alignof): __alignof__Ͱ͢

    • ׅހ಺ͷ໊લ͸ stdalign.h • ͲͪΒ΋GCC 2.95Ͱ͢Ͱʹ͋Δ
  13. aligned_alloc • posix_memalignͰ͢ɻ • kosaki͞Μ͕ৄ͍͠ͷͰͦͬͪΛݟ·͠ΐ͏ • http://www.slideshare.net/kosaki55tea/glibc-malloc • https://www.youtube.com/watch?v=0-vWT-t0UHg

  14. _Noreturn • __attribute__((noreturn))Ͱ͢ɻ

  15. _Generic • ΅͘ͷߟ͑ͨ࠷ڧͷtgmath.hΈ͍ͨͳϥΠϒϥϦ Λͭ͘ΕΔ • __builtin_choose_exprͱ __builtin_types_compatible_pͱ__typeof__Ͱ࡞ΕΔ • ࣮͸__typeof__͚ͩͰͭ͘ΕΔʂ

  16. threads.h εϨουϩʔΧϧετϨʔδ • _Thread_localهԱҬࢦఆࢠ (__thread) • ม਺໊ʹ͚ͭΔ͚ͩͰεϨουϩʔΧϧʹͳΔ • tss_create/tss_delete/tss_set/tss_get •

    POSIXͷTLSΈ͍ͨͳAPI΋͋Δ • pthread_key_create/pthread_key_delete/pthread_setspecific/ pthread_getspecific
  17. threads.h εϨουAPI • pthread APIͷ໊લΛม͚͑ͨͩ • pthread_* → thrd_* •

    pthread_mutex_* → mtx_* • pthread_cond_* → cond_*
  18. atomic • ݴޠϨϕϧ: _Atomicܕम০ࢠ • ؔ਺: atomic_* • (GCCͷ__atomic_*ͱ͸ҟͳΔ)

  19. ·ͱΊ • C99͸จ๏ͷมߋ͕ଟ͔ͬͨ • Ͱ΋࠷ۙͷ͸͍͍ͩͨαϙʔτ͍ͯ͠Δ • C11͸จ๏ͷมߋ͸গͳ͍ • จ๏ͷมߋΛ࢖Θͳͯ͘΋ಉ͜͡ͱ͕ग़དྷΔ •

    ͭ·ΓɺC11ͷػೳ͸(configureͱ͔Ͱ͕Μ͹Ε͹)ࠓ೔͔ Β࢖͑Δʂ
  20. ͦͷଞ • __builtin_constant_p • __builtin_unreachable • __builtin_types_compatible_p • __builtin_choose_expr •

    __builtin_expect • https://gcc.gnu.org/onlinedocs/gcc/Other-Builtins.html • __has_builtin, __has_feature, __has_extension, __has_attribute, __has_include • http://clang.llvm.org/docs/LanguageExtensions.html
  21. • C99 • http://www.open-std.org/jtc1/sc22/wg14/www/docs/n869/ • http://www.open-std.org/JTC1/SC22/WG14/www/docs/n897.pdf • http://seclan.dll.jp/c99d/c99d00.htm • C11

    • http://www.open-std.org/JTC1/SC22/WG14/www/docs/ n1570.pdf • http://port70.net/~nsz/c/ • https://gcc.gnu.org/wiki/C11Status