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
Sponsored
·
Your Podcast. Everywhere. Effortlessly.
Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
→
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
Machine Check Exception
sat
PRO
2
24
バイナリダンプの模様を読む
sat
PRO
0
67
cpコマンドはディスク上でデータを コピーしないことがある
sat
PRO
3
52
114-ファイルのshallow_copy.pdf
sat
PRO
2
37
113-Btrfsのスナップショット.pdf
sat
PRO
0
16
システム強制終了時にファイルシステムの整合性を保つ~ コピーオンライト編 ~
sat
PRO
0
59
システム強制終了時に ファイルシステムの整合性を保つ ~ ジャーナリング編 ~
sat
PRO
2
62
ファイルシステムの整合性を回復するfsck
sat
PRO
1
64
小学校5,6年生向けキャリア教育 大人になるまでの道
sat
PRO
8
5.2k
Other Decks in Technology
See All in Technology
知見・人・API・DB・予算 ─ ナイナイ尽くしだった人事データ整備 with dbt、5年間の学び
ken6377
1
160
アカウントが増えてからでは遅い? ~ マルチアカウント統制の勘所 ~
kenichinakamura
0
140
CSに"SLO"は要らない、経営層に"99.9%"は伝わらない - SREを全社に"翻訳"する3原則
cscengineer
PRO
0
2.2k
AI時代における最適なQA組織の作り方
ymty
3
400
NDIAS CTF 2026 問題解説会資料
bata_24
0
180
Keeping applications secure by evolving OAuth 2.0 and OpenID Connect
ahus1
PRO
1
150
なぜ私たちのSREプラクティスはなかなか機能しないのか 〜システムより先に組織を見る〜 / Why our SRE practices aren't really working
vtryo
1
1.3k
環境凍結という Toil を倒す -セルフサービス型 Ephemeral テスト環境の 設計と実践
shirouz
1
350
デジタル・デザイン:次の50年を描く「進化する青写真」
y150saya
0
820
Oracle Exadata Database Service on Cloud@Customer X11M (ExaDB-C@C) サービス概要
oracle4engineer
PRO
2
8.3k
美しいコードを書くためにF#を学んでみた話
yud0uhu
1
220
SRE依存からの脱却 運用を開 発チームへ移す、 フルサイ クル開 発体制の実践
joooee0000
0
600
Featured
See All Featured
Sam Torres - BigQuery for SEOs
techseoconnect
PRO
0
300
Marketing to machines
jonoalderson
1
5.6k
What's in a price? How to price your products and services
michaelherold
247
13k
Why Mistakes Are the Best Teachers: Turning Failure into a Pathway for Growth
auna
0
180
Faster Mobile Websites
deanohume
310
32k
SEO in 2025: How to Prepare for the Future of Search
ipullrank
3
3.6k
Taking LLMs out of the black box: A practical guide to human-in-the-loop distillation
inesmontani
PRO
3
2.3k
A designer walks into a library…
pauljervisheath
211
24k
Producing Creativity
orderedlist
PRO
348
40k
Efficient Content Optimization with Google Search Console & Apps Script
katarinadahlin
PRO
1
670
Leveraging Curiosity to Care for An Aging Population
cassininazir
1
310
What Being in a Rock Band Can Teach Us About Real World SEO
427marketing
0
1k
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終端文字列を使う • とても扱いが面倒くさい ◦ バグやセキュリティホールの温床になりがち