c-basics-memory

c-basics-memory is a cursor rule for Cursor from wangqiqi/cursor-ai-rules. It costs 0 tokens per session (655 once invoked), scanned A, original, MIT.

A set of Chinese-language rules for managing memory and other resources in C programs. It covers error codes, cleanup patterns, resource-owning wrappers, and goto-based cleanup.

In plain words
What is it for?
Use it when writing or reviewing C code that allocates memory, opens files, handles errors, or needs to release multiple resources safely.
Why use it?
It helps prevent memory leaks, unfinished cleanup, and unclear error handling when C code allocates several resources or operations fail partway through.

Cursor rule for Cursor

Install

Getting it into your agent

One page per mod, every tool's command on it. A separate URL per tool would split the same page into five that compete with each other.

agentmods
npx agentmods add rules/wangqiqi/cursor-ai-rules/c-basics-memory
Clone the repo
git clone --depth 1 https://github.com/wangqiqi/cursor-ai-rules

Made for: Cursor.

Wrote this? Show the measurements

A badge with what this costs and how it scanned, read live from this page, so it follows the numbers instead of freezing them. Markdown for a README, HTML for a documentation site or a project page.

agentmods badge for c-basics-memory

README.md
[![agentmods](https://agentmods.dev/badge/rules/wangqiqi/cursor-ai-rules/c-basics-memory.svg)](https://agentmods.dev/rules/wangqiqi/cursor-ai-rules/c-basics-memory)
Your own site
<a href="https://agentmods.dev/rules/wangqiqi/cursor-ai-rules/c-basics-memory"><img src="https://agentmods.dev/badge/rules/wangqiqi/cursor-ai-rules/c-basics-memory.svg" alt="Measured on agentmods" height="20"></a>
Per session 0 Nothing until a file matches its globs; then the whole rule loads.
When invoked 655 The whole file, excluding the scripts and references it only reads on demand.
Security scan A 0 findings. Scan, not verified.
Origin original No closer match found in the catalogue.
Token cost

What it costs to keep this loaded

Counted locally with the o200k_base tokenizer, which is exact for GPT models; Claude uses its own tokenizer and its counts differ. Treat this as one consistent yardstick across the catalogue rather than a bill. Prices are per million input tokens.

ModelPer sessionOnce invoked
Fable 5 $0.00000 $0.00655
Opus 5 $0.00000 $0.00328
Sonnet 5 $0.00000 $0.00131
Haiku 4.5 $0.00000 $0.00065

Measured 4d ago against content hash 2189a4a8a3cb, method: parsed. Prices are Anthropic first-party input rates as of 2026-08-30, from the pricing page.

Security

Grade A, and why

c-basics-memory scanned grade A with 0 findings against 26 rules in 11 categories — prompt injection, anti-refusal, data exfiltration, privilege escalation, supply chain, agent snooping, system-prompt leakage, SSRF and excessive agency — measured 4d ago.

A static scan of the body, not an audit. Every finding is printed with the line that produced it so you can judge whether it matters here. A mod is markdown that instructs an agent; that is exactly why what it instructs is worth reading.

Nothing flagged

None of the 26 patterns this scan looks for appear in this file: no shell pipes, no recursive deletes, no credential paths, no hidden text, no instruction-override or anti-refusal phrasing, no agent-config snooping. That is not a guarantee, it is the absence of the things that are checkable.

.cursor/rules/tech/c-basics-memory.mdc · 113 lines

How it starts

The opening of the file, as written. The whole thing — 113 lines — stays where its author put it; the contents beside it link to each section on GitHub.

C 语言内存与资源管理 (Memory)

本规则由 @c-basics 引用

错误处理

typedef enum {
    ERROR_SUCCESS = 0,
    ERROR_INVALID_ARGUMENT,
    ERROR_OUT_OF_MEMORY,
    ERROR_FILE_NOT_FOUND
} ErrorCode;

#define RETURN_IF_ERROR(expr) \
    do { ErrorCode err = (expr); if (err != ERROR_SUCCESS) return err; } while (0)

#define GOTO_IF_ERROR(expr, label) \
    do { ErrorCode err = (expr); if (err != ERROR_SUCCESS) goto label; } while (0)

RAII 风格资源管理

typedef struct {
    FILE* file;
    bool valid;
} FileHandle;

ErrorCode file_handle_open(FileHandle* handle, const char* filename);
void file_handle_close(FileHandle* handle);

// 使用
ErrorCode process_file(const char* filename) {
    FileHandle handle = {0};
    ErrorCode err = file_handle_open(&handle, filename);
    if (err != ERROR_SUCCESS) return err;
    // 使用文件...
    file_handle_close(&handle);
    return ERROR_SUCCESS;
}

goto 清理 (Linux 内核风格)

ErrorCode complex_operation(const char* input) {
    void* buffer1 = NULL;
    void* buffer2 = NULL;
    FileHandle file = {0};

    buffer1 = malloc(1024);
    if (!buffer1) return ERROR_OUT_OF_MEMORY;
    buffer2 = malloc(2048);
    if (!buffer2) goto cleanup_buffer1;

    if (file_handle_open(&file, input) != ERROR_SUCCESS)
        goto cleanup_buffers;

    // 主逻辑...
    if (fails()) goto cleanup_all;

cleanup_all:   file_handle_close(&file);
cleanup_buffers: free(buffer2);
cleanup_buffer1: free(buffer1);
    return err;
}

内存池

typedef struct {
    void* buffer;
    size_t buffer_size;
    size_t used;
    void** free_list;
    size_t free_count;
} MemoryPool;

MemoryPool* memory_pool_create(size_t block_size, size_t block_count);
void* memory_pool_alloc(MemoryPool* pool);
void memory_pool_free(MemoryPool* pool, void* ptr);
void memory_pool_destroy(MemoryPool* pool);

缓存友好结构

#define CACHE_LINE_SIZE 64
typedef struct {
    int id;
    char padding[60];  // 填充到缓存行
} CacheAlignedItem;

原则

  • MUST 配对 malloc/free、fopen/fclose
  • MUST 检查分配返回值
  • MUST 使用 goto 统一清理多资源

Read the full file on GitHub · 113 lines

Changes

What this file has done since we first saw it

Hashed on every crawl. A supply-chain change to an agent config is a question of when, not whether, so the history is kept rather than the latest state alone.

  1. 4d ago First seen · 113 lines · 0 tokens per session scan A 2189a4a8a3cb

Subscribe to this mod's changes

c-basics-memory is a cursor rule published in the GitHub repository wangqiqi/cursor-ai-rules (16 stars, last pushed 3mo ago), licensed MIT. It costs nothing until one of its globs matches a file; then it loads 655 tokens. A static security scan graded it A with 0 findings. No closer match exists in the catalogue, so it is treated as the original; first seen 2026-08-30.