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
2
400
その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
APIとABIの違い
sat
PRO
5
54
ファイルシステムへのアクセス方法
sat
PRO
0
24
ファイルシステム
sat
PRO
1
23
低レイヤソフトウェア技術者が YouTuberとして食っていこうとした話
sat
PRO
7
6.1k
ポーリングと割り込み
sat
PRO
1
78
Rook: Intro and Deep Dive With Ceph
sat
PRO
1
140
会社員しながら本を書いてきた知見の共有
sat
PRO
3
880
デバイスにアクセスするデバイスファイル
sat
PRO
1
60
ファイルシステムのデータを ブロックデバイスへの操作で変更
sat
PRO
1
48
Other Decks in Technology
See All in Technology
これでもう迷わない!Jetpack Composeの書き方実践ガイド
zozotech
PRO
0
290
20250903_1つのAWSアカウントに複数システムがある環境におけるアクセス制御をABACで実現.pdf
yhana
3
530
「何となくテストする」を卒業するためにプロダクトが動く仕組みを理解しよう
kawabeaver
0
290
エラーとアクセシビリティ
schktjm
1
1.2k
未経験者・初心者に贈る!40分でわかるAndroidアプリ開発の今と大事なポイント
operando
3
310
ChatGPTとPlantUML/Mermaidによるソフトウェア設計
gowhich501
1
120
バッチ処理で悩むバックエンドエンジニアに捧げるAWS Glue入門
diggymo
3
180
現場で効くClaude Code ─ 最新動向と企業導入
takaakikakei
1
200
MCPで変わる Amebaデザインシステム「Spindle」の開発
spindle
PRO
3
3.2k
Rustから学ぶ 非同期処理の仕組み
skanehira
1
130
サラリーマンの小遣いで作るtoCサービス - Cloudflare Workersでスケールする開発戦略
shinaps
1
380
データアナリストからアナリティクスエンジニアになった話
hiyokko_data
2
440
Featured
See All Featured
Agile that works and the tools we love
rasmusluckow
330
21k
Building a Scalable Design System with Sketch
lauravandoore
462
33k
Learning to Love Humans: Emotional Interface Design
aarron
273
40k
Code Reviewing Like a Champion
maltzj
525
40k
Understanding Cognitive Biases in Performance Measurement
bluesmoon
29
1.9k
Reflections from 52 weeks, 52 projects
jeffersonlam
352
21k
How to Create Impact in a Changing Tech Landscape [PerfNow 2023]
tammyeverts
53
2.9k
実際に使うSQLの書き方 徹底解説 / pgcon21j-tutorial
soudai
PRO
187
55k
Creating an realtime collaboration tool: Agile Flush - .NET Oxford
marcduiker
31
2.2k
Fashionably flexible responsive web design (full day workshop)
malarkey
407
66k
Refactoring Trust on Your Teams (GOTO; Chicago 2020)
rmw
34
3.1k
Visualization
eitanlees
148
16k
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終端文字列を使う • とても扱いが面倒くさい ◦ バグやセキュリティホールの温床になりがち