Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
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
440
2
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
その57 C言語を知らない人が見たらびっくりしそうなC言語の特徴 ~文字列編~
以下動画のテキストです。
https://youtu.be/PblZT66BmBk
Satoru Takeuchi
PRO
April 26, 2023
More Decks by Satoru Takeuchi
See All by Satoru Takeuchi
Rook: Intro and Deep Dive with Ceph
sat
PRO
1
38
Machine Check Exception
sat
PRO
2
51
バイナリダンプの模様を読む
sat
PRO
0
71
cpコマンドはディスク上でデータを コピーしないことがある
sat
PRO
3
57
114-ファイルのshallow_copy.pdf
sat
PRO
2
41
113-Btrfsのスナップショット.pdf
sat
PRO
0
22
システム強制終了時にファイルシステムの整合性を保つ~ コピーオンライト編 ~
sat
PRO
0
62
システム強制終了時に ファイルシステムの整合性を保つ ~ ジャーナリング編 ~
sat
PRO
2
68
ファイルシステムの整合性を回復するfsck
sat
PRO
1
74
Other Decks in Technology
See All in Technology
【CEDEC2026】『GRANBLUE FANTASY: Relink - Endless Ragnarok』のバトル制作事例 ~最高のキャラゲーを目指して~
cygames
PRO
0
190
タクシーアプリ『GO』の実践的データ活用〜位置情報データの収集とStreamlitでの可視化〜
mot_techtalk
2
200
もう一度考える SRE チームの作り方・育て方 / Rethinking SRE #1: Building and Growing SRE Teams
rrreeeyyy
5
970
初めてのGitHub Actions / GitHub Actions at First
tooppoo
0
140
名古屋の市バスGTFS-JPデータ×スガキヤ 最寄りバス停検索をAmazon ElastiCache Serverless for Valkeyで最適化する
usanchuu
1
250
最高のシステムプロンプトを作るためにフィードバック機能を導入した話
alchemy1115
1
320
Redmine 7.0 新機能・機能強化解説(OSC2026京都ダイジェスト版)
vividtone
1
190
今こそ聞きたいソフトウェア設計 ドメイン駆動設計再入門
masuda220
PRO
16
5.4k
Forza Horizon 6 のテレメトリ機能で 自動運転に使えそうな学習データを集める話
henjin0
0
120
SmartHR Engineering Team Deck
smarthr
0
180
Data Hubグループ 紹介資料
sansan33
PRO
0
3.1k
組織にどうSREを根付かせるか?〜IVRyの場合〜
abnoumaru
0
320
Featured
See All Featured
Evolution of real-time – Irina Nazarova, EuRuKo, 2024
irinanazarova
9
1.5k
30 Presentation Tips
portentint
PRO
1
360
Understanding Cognitive Biases in Performance Measurement
bluesmoon
32
3k
Technical Leadership for Architectural Decision Making
baasie
3
460
Ten Tips & Tricks for a 🌱 transition
stuffmc
0
160
Data-driven link building: lessons from a $708K investment (BrightonSEO talk)
szymonslowik
1
1.2k
Noah Learner - AI + Me: how we built a GSC Bulk Export data pipeline
techseoconnect
PRO
0
340
Exploring anti-patterns in Rails
aemeredith
3
450
How People are Using Generative and Agentic AI to Supercharge Their Products, Projects, Services and Value Streams Today
helenjbeal
1
260
Large-scale JavaScript Application Architecture
addyosmani
515
110k
Leveraging Curiosity to Care for An Aging Population
cassininazir
1
460
Cheating the UX When There Is Nothing More to Optimize - PixelPioneers
stephaniewalter
287
14k
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終端文字列を使う • とても扱いが面倒くさい ◦ バグやセキュリティホールの温床になりがち