Upgrade to PRO for Only $50/Year—Limited-Time Offer! 🔥
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
その57 C言語を知らない人が見たらびっくりしそうなC言語の特徴 ~文字列編~
Search
Satoru Takeuchi
PRO
April 26, 2023
Technology
2
410
その57 C言語を知らない人が見たらびっくりしそうなC言語の特徴 ~文字列編~
以下動画のテキストです。
https://youtu.be/PblZT66BmBk
Satoru Takeuchi
PRO
April 26, 2023
Tweet
Share
More Decks by Satoru Takeuchi
See All by Satoru Takeuchi
eBPF
sat
PRO
1
97
waruiBPF
sat
PRO
0
94
eBPFとwaruiBPF
sat
PRO
4
3.1k
Pythonのコードの気になる行でスタックトレースを出す
sat
PRO
0
89
ソースコードを読むときの思考プロセスの例 ~markdownのレンダリング方法を知りたかった2 markdownパッケージ~
sat
PRO
0
180
様々なファイルシステム
sat
PRO
0
330
ソースを読む時の思考プロセスの例-MkDocs
sat
PRO
1
410
ソースを読むプロセスの例
sat
PRO
22
18k
メモリマップトファイル
sat
PRO
1
170
Other Decks in Technology
See All in Technology
フルカイテン株式会社 エンジニア向け採用資料
fullkaiten
0
9.9k
AWS re:Invent 2025~初参加の成果と学び~
kubomasataka
1
200
2025年のデザインシステムとAI 活用を振り返る
leveragestech
0
310
Oracle Database@Azure:サービス概要のご紹介
oracle4engineer
PRO
2
200
TED_modeki_共創ラボ_20251203.pdf
iotcomjpadmin
0
150
通勤手当申請チェックエージェント開発のリアル
whisaiyo
3
480
マイクロサービスへの5年間 ぶっちゃけ何をしてどうなったか
joker1007
21
8.3k
さくらのクラウド開発ふりかえり2025
kazeburo
2
1.2k
Amazon Bedrock Knowledge Bases × メタデータ活用で実現する検証可能な RAG 設計
tomoaki25
6
2.4k
2025-12-27 Claude CodeでPRレビュー対応を効率化する@機械学習社会実装勉強会第54回
nakamasato
4
1.1k
AI との良い付き合い方を僕らは誰も知らない
asei
0
270
Connection-based OAuthから学ぶOAuth for AI Agents
flatt_security
0
380
Featured
See All Featured
AI Search: Where Are We & What Can We Do About It?
aleyda
0
6.7k
Testing 201, or: Great Expectations
jmmastey
46
7.8k
Why You Should Never Use an ORM
jnunemaker
PRO
61
9.7k
Navigating Algorithm Shifts & AI Overviews - #SMXNext
aleyda
0
1k
AI Search: Implications for SEO and How to Move Forward - #ShenzhenSEOConference
aleyda
1
1k
Google's AI Overviews - The New Search
badams
0
870
Claude Code のすすめ
schroneko
67
210k
StorybookのUI Testing Handbookを読んだ
zakiyama
31
6.5k
Fantastic passwords and where to find them - at NoRuKo
philnash
52
3.5k
Tell your own story through comics
letsgokoyo
0
770
Leveraging LLMs for student feedback in introductory data science courses - posit::conf(2025)
minecr
0
89
Sam Torres - BigQuery for SEOs
techseoconnect
PRO
0
150
Transcript
C言語を知らない人がびっくりしそうな C言語の特徴 ~文字列編~ Apr. 23rd, 2023 Satoru Takeuchi twitter: satoru_takeuchi
はじめに • 想定聴衆 ◦ 「C言語は聞いたことはあるがどんなものか知らない」という人 • はなすこと ◦ C言語の特徴を紹介 ◦
今回のテーマは文字列 • 環境 ◦ OS: Ubuntu 20.04/x86_64 ◦ gcc: Ubuntu 9.4.0-1ubuntu1~20.04.1
C言語における文字列とは? • “string”といった文字列専用の型は存在しない • 便宜的にNULL文字(‘\0’)で終わるcharの配列を文字列として使っている ◦ これをNULL終端文字列やASCIIZなどと呼ぶ ◦ 標準ライブラリの文字列操作関数も NULL終端文字列を扱う
• めんどくさい
初期化 char s[] = “foo”; メモリ ‘f’ ‘o’ ‘o’ ‘\0’
s
比較 char s1[] = “foo”; char s2[] = “bar”; //
strcmp()は第一引数と第二引数が同じ文字列なら 0、そうでないなら非0を返す // 同じでない場合の定義はもうちょっと複雑。気になれば man strcmpを参照 strcmp(s1, s1); strcmp(s1, s2); strcmp(s2, s1); メモリ ‘f’ ‘o’ ‘o’ ‘\0’ s1 ‘b’ ‘r’ ‘a’ ‘\0’ s2 …
長さの確認 char s[] = “foo”; int n = strlen(s); //
foo(s, n); ‘f’ ‘o’ ‘o’ ‘\0’ s NULL文字が見つかるまで走査
コピー char s1[] = “foo”; char s2[5]; strcpy(s2, s1); char
s3[]; s3 = strdup(s1); // 標準ライブラリ関数ではない&後でメモリ解放が必要 メモリ ‘f’ ‘o’ ‘o’ ‘\0’ s1 ‘f’ ‘o’ ‘o’ ‘\0’ s2 … コピー
コピーの落とし穴: ポインタのコピーをしてしまう char s1[] = “foo”; char s2[]; s2 =
s1; ‘f’ ‘o’ ‘o’ ‘\0’ s1 s2
コピーの落とし穴: バッファ長が足りない char s1[] = “foo”; char s2[2]; strcpy(s2, s1);
// strncpy(s2, s1, sizeof(s2)); // s2[sizeof(s2)-1] = ‘\0’ メモリ ‘f’ ‘o’ ‘o’ ‘\0’ s1 ‘f’ ‘o’ ‘o’ ‘\0’ s2 … コピー 破壊!
連結 char s1[] = “foo”; char s2[] = “bar”; //
char s3[] = a + b はできない char s3[10]; strcpy(s3, s1); strcat(s3, s2); ‘f’ ‘o’ ‘o’ ‘\0’ … ‘b’ ‘a’ ‘r’ ‘\0’ ‘f’ ‘o’ ‘o’ ‘\0’ s1 s2 s3 strcpy(s3, s1) ‘f’ ‘o’ ‘o’ ‘\0’ … ‘b’ ‘a’ ‘r’ ‘\0’ ‘f’ ‘o’ ‘o’ ‘b’ ‘a’ ‘r’ ‘\0’ s1 s2 s3 strcat()
まとめ • C言語では文字列を扱う専用の型は存在しない • 一般にNULL終端文字列を使う • とても扱いが面倒くさい ◦ バグやセキュリティホールの温床になりがち