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
様々なファイルシステム
sat
PRO
0
260
ソースを読む時の思考プロセスの例-MkDocs
sat
PRO
1
270
ソースを読むプロセスの例
sat
PRO
19
14k
メモリマップトファイル
sat
PRO
1
130
「Linux」という言葉が指すもの
sat
PRO
4
220
APIとABIの違い
sat
PRO
5
200
ファイルシステムへのアクセス方法
sat
PRO
0
81
ファイルシステム
sat
PRO
1
75
低レイヤソフトウェア技術者が YouTuberとして食っていこうとした話
sat
PRO
7
6.2k
Other Decks in Technology
See All in Technology
AI時代、“平均値”ではいられない
uhyo
8
2.6k
From Natural Language to K8s Operations: The MCP Architecture and Practice of kubectl-ai
appleboy
0
260
Azure Well-Architected Framework入門
tomokusaba
1
130
AI駆動で進める依存ライブラリ更新 ─ Vue プロジェクトの品質向上と開発スピード改善の実践録
sayn0
1
320
QA業務を変える(!?)AIを併用した不具合分析の実践
ma2ri
0
150
Amazon Athena で JSON・Parquet・Iceberg のデータを検索し、性能を比較してみた
shigeruoda
1
130
AI時代の発信活動 ~技術者として認知してもらうための発信法~ / 20251028 Masaki Okuda
shift_evolve
PRO
1
100
頭部ふわふわ浄酔器
uyupun
0
110
What's new in OpenShift 4.20
redhatlivestreaming
0
290
オブザーバビリティと育てた ID管理・認証認可基盤の歩み / The Journey of an ID Management, Authentication, and Authorization Platform Nurtured with Observability
kaminashi
1
740
オブザーバビリティが育むシステム理解と好奇心
maruloop
2
1.3k
知覚とデザイン
rinchoku
1
600
Featured
See All Featured
Visualizing Your Data: Incorporating Mongo into Loggly Infrastructure
mongodb
48
9.7k
Easily Structure & Communicate Ideas using Wireframe
afnizarnur
194
16k
Large-scale JavaScript Application Architecture
addyosmani
514
110k
Building Better People: How to give real-time feedback that sticks.
wjessup
369
20k
A better future with KSS
kneath
239
18k
Evolution of real-time – Irina Nazarova, EuRuKo, 2024
irinanazarova
9
1k
Designing Experiences People Love
moore
142
24k
The World Runs on Bad Software
bkeepers
PRO
72
11k
How to Create Impact in a Changing Tech Landscape [PerfNow 2023]
tammyeverts
55
3k
Building an army of robots
kneath
305
46k
For a Future-Friendly Web
brad_frost
180
10k
ピンチをチャンスに:未来をつくるプロダクトロードマップ #pmconf2020
aki_iinuma
127
54k
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終端文字列を使う • とても扱いが面倒くさい ◦ バグやセキュリティホールの温床になりがち