Memory Safety Patterns

Memory Safety Patterns

Teaches an AI coding agent memory-safe patterns such as RAII, ownership and smart pointers across Rust, C++ and C to prevent leaks and use-after-free bugs.

Skill files2 files

references/

SKILL.md77 lines
1
---
2
name: memory-safety-patterns
3
description: Implement memory-safe programming with RAII, ownership, smart pointers, and resource management across Rust, C++, and C. Use when writing safe systems code, managing resources, or preventing memory bugs.
4
---
5
6
# Memory Safety Patterns
7
8
Cross-language patterns for memory-safe programming including RAII, ownership, smart pointers, and resource management.
9
10
## When to Use This Skill
11
12
- Writing memory-safe systems code
13
- Managing resources (files, sockets, memory)
14
- Preventing use-after-free and leaks
15
- Implementing RAII patterns
16
- Choosing between languages for safety
17
- Debugging memory issues
18
19
## Core Concepts
20
21
### 1. Memory Bug Categories
22
23
| Bug Type | Description | Prevention |
24
| -------------------- | -------------------------------- | ----------------- |
25
| **Use-after-free** | Access freed memory | Ownership, RAII |
26
| **Double-free** | Free same memory twice | Smart pointers |
27
| **Memory leak** | Never free memory | RAII, GC |
28
| **Buffer overflow** | Write past buffer end | Bounds checking |
29
| **Dangling pointer** | Pointer to freed memory | Lifetime tracking |
30
| **Data race** | Concurrent unsynchronized access | Ownership, Sync |
31
32
### 2. Safety Spectrum
33
34
```
35
Manual (C) → Smart Pointers (C++) → Ownership (Rust) → GC (Go, Java)
36
Less safe More safe
37
More control Less control
38
```
39
40
## Detailed patterns and worked examples
41
42
Detailed pattern documentation lives in `references/details.md`. Read that file when the navigation tier above is insufficient.
43
44
## Best Practices
45
46
### Do's
47
48
- **Prefer RAII** - Tie resource lifetime to scope
49
- **Use smart pointers** - Avoid raw pointers in C++
50
- **Understand ownership** - Know who owns what
51
- **Check bounds** - Use safe access methods
52
- **Use tools** - AddressSanitizer, Valgrind, Miri
53
54
### Don'ts
55
56
- **Don't use raw pointers** - Unless interfacing with C
57
- **Don't return local references** - Dangling pointer
58
- **Don't ignore compiler warnings** - They catch bugs
59
- **Don't use `unsafe` carelessly** - In Rust, minimize it
60
- **Don't assume thread safety** - Be explicit
61
62
## Debugging Tools
63
64
```bash
65
# AddressSanitizer (Clang/GCC)
66
clang++ -fsanitize=address -g source.cpp
67
68
# Valgrind
69
valgrind --leak-check=full ./program
70
71
# Rust Miri (undefined behavior detector)
72
cargo +nightly miri run
73
74
# ThreadSanitizer
75
clang++ -fsanitize=thread -g source.cpp
76
```
77

© Seth Hobson · MIT · source · posted by @OpenSourceBuffGuy on promptparty.com

@OpenSourceBuffGuy

I find openly licensed prompts and post them here.

0 followers · 14,621 prompts

More by @OpenSourceBuffGuy

See all
Sea turtle carrying floating film studio island prompt image

Sea turtle carrying floating film studio island

Draws a giant sea turtle carrying a floating island film studio through a starry sky, where fairies polish rough film strips into golden film.

image-prompt, nano-banana, 3d-render, cartoon, fantasy-world, creatures, animals, whimsical, dreamy, cinematic, dramatic-lighting, vibrant-colors, bokeh, miniature, widescreen, night-scene, illustration, concept-art, surreal, storytelling, clay-3d

Desert buggy escapes drones past industrial outpost prompt image

Desert buggy escapes drones past industrial outpost

Builds a 15-second desert chase video where a racer in an armored buggy escapes drones past an industrial outpost, using five reference images with set jobs.

video-prompt, hailuo, action, multi-shot, timed-beats, camera-movement, reference-image, character-consistency, negative-prompt, cinematic, sci-fi, cars, landscape, aerial-view, tracking-shot, photorealistic, sound-effects, structured-output, long-form, widescreen

1 view
Cozy Pixel Art Farm Island Village prompt image

Cozy Pixel Art Farm Island Village

Draws a cozy 16-bit pixel-art farming village across six small islands joined by bridges, with exact counts for buildings, animals and crops.

image-prompt, gpt-image, pixel-art, isometric, game-assets, concept-art, maps, worldbuilding, cozy, vibrant-colors, ocean, landscape, wide-shot, widescreen, game-development, video-games, structured-output, whimsical, summer, high-angle, aerial-view, animals, negative-prompt, cityscape

1 view

Reviews

Loading reviews…

Discussion

Loading comments…