{"id":50338727,"url":"https://github.com/tigran-sargsyan-w/cpp-module-01","last_synced_at":"2026-05-29T15:30:24.200Z","repository":{"id":325990742,"uuid":"1103313377","full_name":"tigran-sargsyan-w/cpp-module-01","owner":"tigran-sargsyan-w","description":"This module is designed to help you understand memory allocation, references, pointers to members, and the usage of the switch statement in C++.","archived":false,"fork":false,"pushed_at":"2025-12-11T19:33:49.000Z","size":50,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"main","last_synced_at":"2026-05-06T11:49:03.972Z","etag":null,"topics":["42","42-cursus","brain","cpp","cpp-modules","cpp01","cpp98","harl","harl-filter","memory-allocation","module01","pointers","references","school-42","sed","weapon","zombie","zombie-horde"],"latest_commit_sha":null,"homepage":"","language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/tigran-sargsyan-w.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2025-11-24T17:51:28.000Z","updated_at":"2025-12-14T19:47:56.000Z","dependencies_parsed_at":null,"dependency_job_id":null,"html_url":"https://github.com/tigran-sargsyan-w/cpp-module-01","commit_stats":null,"previous_names":["tigran-sargsyan-w/cpp-module-01"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/tigran-sargsyan-w/cpp-module-01","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tigran-sargsyan-w%2Fcpp-module-01","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tigran-sargsyan-w%2Fcpp-module-01/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tigran-sargsyan-w%2Fcpp-module-01/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tigran-sargsyan-w%2Fcpp-module-01/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/tigran-sargsyan-w","download_url":"https://codeload.github.com/tigran-sargsyan-w/cpp-module-01/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tigran-sargsyan-w%2Fcpp-module-01/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":33659872,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-26T15:22:16.424Z","status":"online","status_checked_at":"2026-05-29T02:00:06.066Z","response_time":107,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["42","42-cursus","brain","cpp","cpp-modules","cpp01","cpp98","harl","harl-filter","memory-allocation","module01","pointers","references","school-42","sed","weapon","zombie","zombie-horde"],"created_at":"2026-05-29T15:30:21.478Z","updated_at":"2026-05-29T15:30:24.193Z","avatar_url":"https://github.com/tigran-sargsyan-w.png","language":"C++","funding_links":[],"categories":[],"sub_categories":[],"readme":"# C++ Module 01 – Memory, References \u0026 Complaining Clients 🧠🧟‍♂️\n\n✅ **Status**: Completed – all exercises \n\n🏫 **School**: 42 – C++ Modules (Module 01) \n\n🏅 Score: 100/100\n\n\u003e *Memory allocation, references, pointers to members and `switch` statements.*\n\n---\n\n## 📚 Table of Contents\n\n* [Description](#-description)\n* [Goals of the Module](#-goals-of-the-module)\n* [Exercises Overview](#-exercises-overview)\n\n  * [ex00 – BraiiiiiiinnnzzzZ](#ex00--braiiiiiiinnnzzzz)\n  * [ex01 – Moar brainz!](#ex01--moar-brainz)\n  * [ex02 – HI THIS IS BRAIN](#ex02--hi-this-is-brain)\n  * [ex03 – Unnecessary violence](#ex03--unnecessary-violence)\n  * [ex04 – Sed is for losers](#ex04--sed-is-for-losers)\n  * [ex05 – Harl 2.0](#ex05--harl-20)\n  * [ex06 – Harl filter](#ex06--harl-filter-optional)\n* [Requirements](#-requirements)\n* [Build \u0026 Run](#-build--run)\n* [Repository Layout](#-repository-layout)\n* [Testing Tips](#-testing-tips)\n* [42 Notes](#-42-notes)\n\n---\n\n## 📝 Description\n\nThis repository contains my solutions to **42’s C++ Module 01 (C++98)**.\n\nModule 01 continues the C++ journey by focusing on:\n\n* **Dynamic memory** and when to use heap vs stack\n* **References vs pointers** (and how they differ)\n* **Pointers to member functions**\n* Using the **`switch` statement** in C++\n* Structuring slightly larger examples while keeping code readable\n\nAll exercises are written in **C++98** and compiled with strict flags (`-Wall -Wextra -Werror -std=c++98`).\n\n---\n\n## 🎯 Goals of the Module\n\nConcepts covered (depending on the exercise):\n\n* Manual **memory allocation** (`new` / `delete`, arrays of objects)\n* **Object lifetime** and destructors (debug messages, leak-free code)\n* **References** vs **pointers** and basic address manipulation\n* **Pointers to member functions** (dynamic behavior without giant `if`/`else` forests)\n* **`switch` / `case`** for simple log-level filtering\n* Splitting code across multiple **`.hpp` / `.cpp`** files\n\nThe idea is to get comfortable with how C++ manages memory and indirection, while still staying in relatively small, focused exercises.\n\n---\n\n## 📦 Exercises Overview\n\n### ex00 – BraiiiiiiinnnzzzZ\n\n\u003e First zombie steps: stack vs heap allocation.\n\n**Goal:**\nImplement a `Zombie` class that:\n\n* Has a private `std::string name`\n* Has a member function `void announce(void);` that prints:\n\n  ```text\n  \u003cname\u003e: BraiiiiiiinnnzzzZ...\n  ```\n* Prints a debug message in its **destructor** when a zombie is destroyed\n\nYou also implement two helper functions:\n\n* `Zombie* newZombie(std::string name);`\n\n  * Allocates a `Zombie` on the **heap**, names it and returns the pointer\n* `void randomChump(std::string name);`\n\n  * Creates a temporary `Zombie`, makes it `announce()`, then lets it be destroyed\n\n**Concepts practiced:**\n\n* Stack vs heap allocation and when each makes sense\n* Constructors / destructors for debug output\n* Ownership / lifetime of dynamically allocated objects\n\n---\n\n### ex01 – Moar brainz!\n\n\u003e One zombie is nice. A whole horde is better.\n\n**Goal:**\nImplement:\n\n```cpp\nZombie* zombieHorde(int N, std::string name);\n```\n\n* Allocates **`N` zombies in one allocation** (array of objects)\n* Initializes all of them with the same `name`\n* Returns a pointer to the **first** zombie in the horde\n* You must `delete[]` the horde when you’re done with it and ensure no leaks\n\n**Concepts practiced:**\n\n* Using `new[]` / `delete[]` for arrays of objects\n* Looping through an array of objects and calling `announce()`\n* Making sure destructors run for each element in the array\n\n---\n\n### ex02 – HI THIS IS BRAIN\n\n\u003e Demystifying references through addresses and values.\n\n**Goal:**\nWrite a program that:\n\n* Has a `std::string` initialized to `\"HI THIS IS BRAIN\"`\n* Declares:\n\n  * `stringPTR` – a **pointer** to that string\n  * `stringREF` – a **reference** to that string\n* Prints:\n\n  * The **memory address** of:\n\n    * the original string\n    * the pointer’s target\n    * the reference’s target\n  * Then the **values** via:\n\n    * the variable\n    * the pointer\n    * the reference\n\n**Concepts practiced:**\n\n* Basic pointer and reference syntax\n* Differences between pointers and references\n* A reference is syntactic sugar with safety rules. It's like a \"const pointer,\" which:\n  * Automatically dereferences (you don't need to write *).\n  * Prohibits NULL (the compiler will slap you if you try).\n  * Prohibits address changes (binding forever).\n* [Look how similar they are(pointers and references)](https://godbolt.org/z/8787dd8Ms) \n* Understanding that a reference is essentially an alias to an existing object\n* Seeing how addresses line up in memory\n\n---\n\n### ex03 – Unnecessary violence\n\n\u003e Two humans, one weapon type: pointer vs reference design.\n\n**Goal:**\n\nImplement a `Weapon` class:\n\n* Private attribute: `std::string type;`\n* `const std::string\u0026 getType() const;`\n* `void setType(const std::string\u0026 newType);`\n\nThen implement two classes: `HumanA` and `HumanB`:\n\n* Both have a **name** and some kind of **Weapon** association\n* Both implement:\n\n  ```text\n  \u003cname\u003e attacks with their \u003cweapon type\u003e\n  ```\n* Differences:\n\n  * `HumanA` is **always armed** – the `Weapon` is passed in the constructor (likely as a reference)\n  * `HumanB` may **start unarmed** and receive a weapon later via `setWeapon()` (often stored as a pointer)\n\n**Concepts practiced:**\n\n* Choosing between **reference** and **pointer** members\n* Understanding when an object must always exist vs may be optional\n* Keeping weapon type in sync across multiple owners via reference/pointer\n\n---\n\n### ex04 – Sed is for losers\n\n\u003e Tiny `sed`-like replacer using only C++ strings.\n\n**Goal:**\nCreate a program:\n\n```bash\n./sed_is_for_losers \u003cfilename\u003e \u003cs1\u003e \u003cs2\u003e\n```\n\n* Opens `\u003cfilename\u003e`\n* Writes a new file `\u003cfilename\u003e.replace`\n* Replaces **every occurrence** of `s1` with `s2` in the content\n* Handles invalid input and I/O errors\n\n**Important rules:**\n\n* You **must not** use `std::string::replace` (depending on your chosen implementation style / subject constraints)\n* Use C++ streams (`std::ifstream`, `std::ofstream`) instead of C-style I/O\n\n**Concepts practiced:**\n\n* File input/output with C++ streams\n* Manual substring search + replace using string operations\n* Edge cases: empty `s1`, `s2`, large files, no matches, etc.\n\n---\n\n### ex05 – Harl 2.0\n\n\u003e Turning a noisy customer into a table of function pointers.\n\n**Goal:**\nImplement a `Harl` class with private member functions:\n\n* `void debug(void);`\n* `void info(void);`\n* `void warning(void);`\n* `void error(void);`\n\nAnd a public function:\n\n```cpp\nvoid complain(std::string level);\n```\n\nThis should:\n\n* Call the corresponding private method based on `level` (`\"DEBUG\"`, `\"INFO\"`, `\"WARNING\"`, `\"ERROR\"`)\n* **Use pointers to member functions** – no giant `if`/`else if` chains\n\nThe subject typically provides example messages for each log level.\n\n**Concepts practiced:**\n\n* Mapping strings → member function pointers\n* Cleaner dispatch vs long condition chains\n* Encapsulating logging behavior inside a class\n\n---\n\n### ex06 – Harl filter (optional)\n\n\u003e Same Harl, but now with log-level filtering and `switch`.\n\n**Goal:**\nCreate a program:\n\n```bash\n./harlFilter \u003clevel\u003e\n```\n\n* `\u003clevel\u003e` is one of: `\"DEBUG\"`, `\"INFO\"`, `\"WARNING\"`, `\"ERROR\"`\n\n* The program prints **all messages from that level and above**, in order. For example:\n\n  * Input: `\"WARNING\"` → prints `WARNING` + `ERROR` messages\n  * Input: invalid string → prints a default line like:\n\n    ```text\n    [ Probably complaining about insignificant problems ]\n    ```\n\n* Executable name is usually **`harlFilter`**\n\n* You **must** use the `switch` statement for this exercise\n\n**Concepts practiced:**\n\n* Mapping strings to integer levels and using `switch`\n* Filtering logs by severity level\n* Reusing the `Harl` class and controlling which levels are displayed\n\n---\n\n## 🛠 Requirements\n\nGeneral requirements for the C++ modules:\n\n* **Compiler**: `c++`\n* **Flags**:\n\n  * `-Wall -Wextra -Werror`\n  * `-std=c++98`\n* **OS**: any Unix-like system (Linux / macOS)\n* **No external libraries** (no C++11, no Boost, etc.)\n* **No `printf`, `malloc`, `free`** \u0026 friends – use C++ standard library instead\n* Do **not** use `using namespace std;` (or any other namespace with `using namespace`)\n\n---\n\n## ▶️ Build \u0026 Run\n\nClone the repository and build each exercise separately:\n\n```bash\ngit clone \u003cthis-repo-url\u003e\ncd cpp-module-01\n```\n\n### ex00 – BraiiiiiiinnnzzzZ\n\n```bash\ncd ex00\nmake\n./zombies   # or whatever executable name you chose\n```\n\n### ex01 – Moar brainz!\n\n```bash\ncd ex01\nmake\n./horde\n```\n\n### ex02 – HI THIS IS BRAIN\n\n```bash\ncd ex02\nmake\n./brain\n```\n\n### ex03 – Unnecessary violence\n\n```bash\ncd ex03\nmake\n./violence\n```\n\n### ex04 – Sed is for losers\n\n```bash\ncd ex04\nmake\n./sed_is_for_losers input.txt s1 s2\n```\n\n### ex05 – Harl 2.0\n\n```bash\ncd ex05\nmake\n./harl\n```\n\n### ex06 – Harl filter\n\n```bash\ncd ex06\nmake\n./harlFilter \"WARNING\"\n```\n\n\u003e Exact executable names may differ depending on my implementation / subject instructions.\n\n---\n\n## 📂 Repository Layout\n\n```text\ncpp-module-01/\n├── ex00/\n│   ├── Makefile\n│   ├── Zombie.hpp / Zombie.cpp\n│   ├── newZombie.cpp\n│   ├── randomChump.cpp\n│   └── main.cpp\n│\n├── ex01/\n│   ├── Makefile\n│   ├── Zombie.hpp / Zombie.cpp\n│   ├── zombieHorde.cpp\n│   └── main.cpp\n│\n├── ex02/\n│   ├── Makefile\n│   └── main.cpp\n│\n├── ex03/\n│   ├── Makefile\n│   ├── Weapon.hpp / Weapon.cpp\n│   ├── HumanA.hpp / HumanA.cpp\n│   ├── HumanB.hpp / HumanB.cpp\n│   └── main.cpp\n│\n├── ex04/\n│   ├── Makefile\n│   ├── main.cpp\n│   ├── *.hpp / *.cpp    # helper classes if any\n│\n├── ex05/\n│   ├── Makefile\n│   ├── Harl.hpp / Harl.cpp\n│   └── main.cpp\n│\n└── ex06/\n    ├── Makefile\n    ├── Harl.hpp / Harl.cpp   # can reuse / adapt from ex05\n    └── main.cpp\n```\n\n---\n\n## 🔍 Testing Tips\n\nSome ideas for manual testing:\n\n* **ex00 / ex01 – Zombies**\n\n  * Verify **destructors** are called when expected (heap vs stack, horde deletion)\n  * Run with Valgrind / a leak checker to make sure there are **no memory leaks**\n\n* **ex02 – HI THIS IS BRAIN**\n\n  * Check that all three printed addresses are consistent:\n\n    * string variable address\n    * `stringPTR` target\n    * `stringREF` target\n\n* **ex03 – Unnecessary violence**\n\n  * Change the weapon type after constructing humans and ensure both see the updated value\n  * Test `HumanB` without a weapon first to ensure it doesn’t crash and behaves predictably\n\n* **ex04 – Sed is for losers**\n\n  * Empty file, no occurrence of `s1`\n  * `s1` at the **start/end** of file and multiple times in a row\n  * Edge case: `s1` and `s2` having different lengths, or `s1` being an empty string\n\n* **ex05 – Harl 2.0**\n\n  * Pass each log level and verify only the correct output appears\n  * Check behavior with invalid levels (should probably do nothing or handle it gracefully)\n\n* **ex06 – Harl filter**\n\n  * Test each valid level (`DEBUG`, `INFO`, `WARNING`, `ERROR`) and verify that:\n\n    * It prints from that level **upwards**\n  * Test with completely invalid strings:\n\n    * Should print something like `[ Probably complaining about insignificant problems ]`\n\n---\n\n## 🧾 42 Notes\n\n* C++ modules have **no enforced Norminette**, but the subject still expects **clean and readable code**.\n* No STL containers or algorithms before the later modules – stick to basic types and `std::string` for now.\n* Headers must be self-sufficient (include their own dependencies) and properly guarded against multiple inclusion.\n\n---\n\nIf you’re a 42 student working on the same module, feel free to browse the code and learn from it — but **write your own implementation**. That’s how you’ll actually understand C++ and survive the exams. 🚀\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftigran-sargsyan-w%2Fcpp-module-01","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftigran-sargsyan-w%2Fcpp-module-01","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftigran-sargsyan-w%2Fcpp-module-01/lists"}