Skip to Content
21st Century C, 2nd Edition
book

21st Century C, 2nd Edition

by Ben Klemens
May 2025
Intermediate to advanced
408 pages
5h 55m
Japanese
O'Reilly Media, Inc.
Content preview from 21st Century C, 2nd Edition

第6章. 相棒のポインター

この作品はAIを使って翻訳されている。ご意見、ご感想をお待ちしている:translation-feedback@oreilly.com

彼がそうだ。

私たちのかわいい歌を好きなのは誰だろう

彼は一緒に歌うのが好きなんだ

彼は銃を撃つのが好きだ。

しかし、彼はその意味を知らない。

ニルヴァーナ、"イン・ブルーム"

音楽についての歌やハリウッドについての映画のように、ポインターは他のデータを説明するデータである。確かに圧倒されるのは簡単だ:一度に、参照への参照、エイリアス化、メモリ管理、malloc に迷い込むことに対処しなければならない。しかし、私たちのとんでもない幸運は、別々のコンポーネントに分解される。例えば、malloc を気にすることなく、エイリアス化としてポインタを使うことができる。 は、90年代の教科書で言われていたほど頻繁に登場する必要はない。一方では、Cの構文はスターを使うので混乱しがちだが、他方では、Cの構文は関数へのポインターのような特に複雑な設定を扱うためのツールを提供してくれる。

この章では、よくあるエラーやよくある混乱点を取り上げる。長い間Cで書いてきた人なら、これらの点は当たり前のことのように思えるだろう。この章は、ポインタを扱うときに少し不安を感じるすべての人(その数は膨大である)を対象としている。

自動メモリー、静的メモリー、手動メモリー

C言語には3つの基本的なメモリー管理モデルがある。さらに読者へのボーナスとして、あと2つのメモリ・モデル(「スレッド・ローカル」でスレッド・ローカル、「巨大なデータセットにmmapを使う」でmmaped)を紹介する。

自動

変数は最初に使うときに宣言し、スコープ外に出ると削除される。static キーワードがなければ、関数内の変数はすべて自動型である。一般的なプログラミング言語には自動型のデータしかない。

静的

静的変数は、プログラミングの全期間を通じて同じ場所に存在する。配列のサイズは起動時に固定されるが、値は変更される可能性がある(つまり、完全に静的というわけではない)。データはmain が起動する前に初期化されるため、初期化は計算を必要としない定数で行わなければならない。関数の外側(ファイル・スコープ内)で宣言された変数や、static キーワードを使った関数の内部で宣言された変数は静的である。静的変数の初期化を忘れると、すべてゼロ(またはNULL )に初期化される。

マニュアル

手動型にはmallocfree が含まれ、セグメンテーションフォールトのほとんどがここで起こる。1また、宣言後に配列のサイズを変更できる唯一のメモリタイプでもある。

表6-1は、データを置くことができる3つの場所の違いを示している。これらの点については、次の数章で詳しく説明する。

表6-1. 3種類のメモリー、3種類の機能バンドル
静的 オート マニュアル

起動時にゼロにセットされる

スコープ限定

開始時に値をセットできる

開始時に定数でない値をセットできる

sizeof 配列のサイズを測る

関数呼び出しにまたがって持続する。

グローバルに対応できる

配列のサイズは実行時にセットできる。

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

Learn C Programming - Second Edition

Learn C Programming - Second Edition

Jeff Szuhay
Tiny C Projects

Tiny C Projects

Daniel Gookin

Publisher Resources

ISBN: 9798341650848