本文へスキップ
製品デスクトップツールTASTECAD図面の確認TASTEFILESファイル管理TASTEFTPファイル転送TASTESSHリモートワークスペースTASTEZIP圧縮と展開TASTEWINMファイル管理開発ツールTASTECODERust 統合開発環境TASTESTUDIO統合開発・テスト環境TASTEETLデータ統合(ETL)サーバーTASTEREMOTEリモートデスクトップサーバーTASTEGEO空間情報サーバーTASTEMAILメールワークスペース投資TASTESTOCK株式自動売買デスクトップアプリはじめるダウンロードドキュメント購入すべての製品
マーケットプレイス
ニュース
技術ニュース今日の開発ニュース開発ドキュメントプログラミング言語RustTypeScriptJavaScriptPythonGoC#JavaCC++KotlinDartWebフロントエンド フレームワークNext.jsReactVue.js · NuxtSvelteKitAngularサーバー フレームワークSpring BootDjangoFastAPIASP.NET CoreNestJS · Expressアプリ・デスクトップ フレームワークTauriFlutterReact NativeElectronアルゴリズムアルゴリズムの基礎主要アルゴリズム発展アルゴリズム実務のアルゴリズム
コミュニティプロジェクト開発ログプロンプトAIツールの問題ショーケースコミュニティガイド
協業技術移転ウェブサイト構築デスクトップアプリ構築リリース・決済パイプライン販売パートナーお問い合わせ
サービスリモートデスクトップインストールパッケージのカスタマイズ
TASTEDEV
製品

デスクトップツール

TASTECAD複雑な図面を、軽やかにひらくTASTEFILES必要なファイルだけを、最速でTASTEFTPファイルが行き交う、いちばん端正な道TASTESSHサーバーとあなたを、より確かにつなぐTASTEZIPより速くまとめ、より安全にひらくTASTEWINM2つの画面で整理する、キーボード中心のファイルマネージャー

開発ツール

TASTECODERust アプリの編集・ビルド・デバッグ・テストまでひとつのウィンドウでTASTESTUDIO編集からリモートテスト、承認した AI 修正までTASTEETL値を見ながら、データを運ぶ

サーバー

TASTEREMOTE自社サーバーで運用するリモートデスクトップTASTEGEOGeoServer 互換の空間情報サーバーを Rust で作り直しますTASTEMAILメールの主導権を、もう一度自分の手に

投資

TASTESTOCKいつ・何を。自分で決めたルールどおりに動く株式自動売買

はじめる

ダウンロード全製品の最新版ドキュメント各製品の使い方購入サブスクリプション・永久ライセンスすべての製品全製品を一覧で
すべての製品
マーケットプレイスニュース
技術

ニュース

今日の開発ニュース言語・フレームワーク・GitHub・AI の公式発表開発ドキュメント言語・フレームワーク・アルゴリズムのガイド

プログラミング言語

RustTypeScriptJavaScriptPythonGoC#JavaCC++KotlinDart

フレームワーク

Webフロントエンド フレームワークNext.jsReactVue.js · NuxtSvelteKitAngular
サーバー フレームワークSpring BootDjangoFastAPIASP.NET CoreNestJS · Express
アプリ・デスクトップ フレームワークTauriFlutterReact NativeElectron

アルゴリズム

アルゴリズムの基礎計算量解析 · ソート · 探索 · 基本データ構造 · 木構造とヒープ主要アルゴリズムグラフ探索 · 最短経路と最小全域木 · 動的計画法 · 貪欲法 · 分割統治法 · バックトラッキング発展アルゴリズム文字列アルゴリズム · 数学と整数論 · ビット演算 · セグメント木 · Union-Find実務のアルゴリズムコーディング面接対策 · LRUキャッシュ · レート制限 · コンシステントハッシュ法
開発ドキュメントをすべて見る
コミュニティ
プロジェクトビルダーと作品を探す開発ログ決定と試行錯誤プロンプト再利用できる指示AIツールの問題Claude・Codex・Gemini の問題と回避策ショーケース完成した成果コミュニティガイドオープンにつくる方法
協業
技術移転Rust・egui でネイティブアプリを作るウェブサイト構築10言語、アカウント、ドキュメントとダウンロードデスクトップアプリ構築Rust・egui、WebView なしリリース・決済パイプライン署名・MSI・deb・rpm・AppImage・ハッシュ販売パートナー紹介して決済額の30%お問い合わせ構築・提携・投資のご相談
サービス
リモートデスクトップブラウザから自分のパソコンへインストールパッケージのカスタマイズ必要な機能だけ、設定は事前に
購入
JA
한국어EnglishDeutschEspañolFrançaisItalianoPortuguês (Brasil)日本語简体中文繁體中文
ログイン
ログイン
TASTEDEV

小さなツールから、より良い開発へ。

support@tastedev.netTASTEDEVを支援
製品製品ラインナップ購入ドキュメントダウンロードマーケットプレイスサポート
コミュニティプロジェクト開発ログプロンプトAIツールの問題ショーケース
ポリシー会社概要プライバシーポリシー利用規約サブスクリプション規約返金ポリシーコミュニティガイドライン
アカウントログイン新規登録
訪問者
今日
昨日
累計閲覧
商号
GXSOFT
代表者
CHOI YOUNG GU
事業者登録番号
853-44-00368
所在地
9F V24, 351 Gyeongin-ro, Michuhol-gu, Incheon, Republic of Korea
電話
+82-10-4404-1523
メール
whitecalvin@tastedev.net
個人情報保護責任者
CHOI YOUNG GU

© 2026 TASTEDEV製品情報は検証済みの開発ソースに基づいています。リリース・改善中

C ドキュメント
C
Getting StartedSyntax BasicsTypes and DataModules and LibrariesErrors and TestingKey Concepts: Pointers and Memory
ディスカッション

C ガイド · 6/6

Key Concepts: Pointers and Memory

この章は現在、英語でのみ提供しています。

Pointers and manual memory management define C, and cause most of its bugs. Once pointers click, arrays, strings, and dynamic structures turn out to be one idea.

Addresses and pointers

Every object lives at some address. &x yields the address of x, a pointer stores it, and *p (dereferencing) reads or writes the object it points to.

#include <stdio.h>

void swap(int *a, int *b) {   /* modifies the caller's variables */
    int tmp = *a;
    *a = *b;
    *b = tmp;
}

int main(void) {
    int x = 10, y = 20;
    int *p = &x;          /* p holds the address of x */

    printf("address of x: %p\n", (void *)p);
    *p = 15;              /* x is now 15 */
    swap(&x, &y);
    printf("x=%d y=%d\n", x, y);   /* x=20 y=15 */

    return 0;
}

Arguments are still passed by value, but a copied address still reaches the caller's object. Use NULL for pointers with no target and check before dereferencing.

Pointer arithmetic and arrays

Pointer arithmetic counts elements, not bytes: for an int *p, p + 1 is the next int. An array name usually converts to a pointer to its first element, so a[i] is *(a + i).

#include <stdio.h>

int sum(const int *begin, const int *end) {
    int total = 0;
    for (const int *p = begin; p != end; p++) {
        total += *p;
    }
    return total;
}

int main(void) {
    int a[] = {1, 2, 3, 4, 5};
    size_t n = sizeof a / sizeof a[0];

    int *p = a;                         /* same as &a[0] */
    printf("%d %d\n", *(p + 2), a[2]);  /* 3 3 */

    printf("%d\n", sum(a, a + n));      /* a + n: one past the end */
    return 0;
}

A pointer may point into an array or one past its end, but that last position is for comparisons only. Subtracting two pointers gives a ptrdiff_t element count, and only within the same array.

malloc and free

Data sized at run time, or that outlives its function, goes on the heap (<stdlib.h>):

  • malloc(size) allocates size bytes with indeterminate contents.
  • calloc(count, size) allocates and zero-fills.
  • realloc(ptr, size) resizes, possibly moving the block to a new address.
  • free(ptr) releases the block; free(NULL) is a no-op.
#include <stdio.h>
#include <stdlib.h>

int main(void) {
    size_t cap = 4, len = 0;
    int *items = malloc(cap * sizeof *items);
    if (items == NULL) return 1;        /* allocation can fail */

    for (int v = 1; v <= 10; v++) {
        if (len == cap) {
            size_t new_cap = cap * 2;
            int *tmp = realloc(items, new_cap * sizeof *tmp);
            if (tmp == NULL) {          /* items is still valid */
                free(items);
                return 1;
            }
            items = tmp;
            cap = new_cap;
        }
        items[len++] = v * v;
    }
    printf("len=%zu cap=%zu last=%d\n", len, cap, items[len - 1]);

    free(items);
    items = NULL;                       /* guard against reuse */
    return 0;
}
この章について質問がありますか?
前の章Errors and Testing

Assigning realloc straight back to the original pointer leaks the block on failure, hence the temporary. sizeof *items stays right if the element type changes.

Ownership conventions

Nothing frees memory for you, so every allocation needs a clear owner:

  • Pair every xxx_create that allocates with an xxx_destroy.
  • A const T * parameter is borrowed: the function neither frees nor keeps it.
  • Each block has exactly one owner that releases it.
#include <stdlib.h>
#include <string.h>

typedef struct {
    char *name;      /* owned; freed in player_destroy */
    int score;
} Player;

/* Returns a new Player or NULL. Caller releases it with player_destroy. */
Player *player_create(const char *name, int score) {
    Player *p = malloc(sizeof *p);
    if (p == NULL) return NULL;
    p->name = malloc(strlen(name) + 1);
    if (p->name == NULL) {
        free(p);
        return NULL;
    }
    strcpy(p->name, name);       /* safe: buffer sized exactly */
    p->score = score;
    return p;
}

void player_destroy(Player *p) {
    if (p == NULL) return;
    free(p->name);
    free(p);
}

Undefined behavior to avoid

Undefined behavior (UB) means the standard places no requirements on the result: code can work today and break under another compiler or optimization level.

MistakeExamplePrevention
Null dereference*p with p == NULLCheck before dereferencing
Out-of-bounds accessa[n]Pass lengths and check them
Use after freefree(p); p->xSet pointers to NULL after freeing
Double freefree(p); free(p);Give each block one owner
Uninitialized readint x; x + 1Initialize at declaration
Returning a local's addressreturn &local;Allocate or use a caller buffer
Signed overflowINT_MAX + 1Check ranges before the operation
Modifying a string literalchar *s = "hi"; s[0] = 'H';Use char s[] = "hi";

Warnings, sanitizers, and tools like Valgrind catch most of these during development.

Summary

  • Pointers hold addresses: & takes one, * dereferences. Check for NULL first.
  • Pointer arithmetic is valid only within an array or one past its end.
  • Check every malloc, route realloc through a temporary, and free each block exactly once.
  • Make ownership explicit with create/destroy pairs.
  • Find undefined behavior with tools; prevent it with discipline.

Further reading

  • cppreference: C reference, language and library
  • GCC online documentation, compiler options and warnings
  • Clang AddressSanitizer
  • Clang UndefinedBehaviorSanitizer
  • SEI CERT C Coding Standard, rules for secure C
  • ISO/IEC JTC1/SC22/WG14, the C committee and standard drafts

コメント 0件

人気のコメント順新しい順

ログイン · ログインするとコメントできます。