Fix on syntax and intro example text added (#9803)

* fix syntax error on code blocks

* adding some intro text so it get's off the bullet list and wont have render issues
This commit is contained in:
Adrian Martinez Treviño
2026-03-31 07:05:47 +00:00
committed by GitHub
parent 0c3d01b7b0
commit 27a02a2131
@@ -12,34 +12,35 @@ Syntax and Semantics
* Does not support objects or classes.
* Memory management is manual, using functions like `malloc` and `free`.
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
Here's an example of manual memory management in C:
typedef struct {
char name[20];
int score;
} Player;
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main() {
// 1. Allocate: malloc needs the exact byte size
Player* p = (Player*)malloc(sizeof(Player));
typedef struct {
char name[20];
int score;
} Player;
int main() {
// 1. Allocate: malloc needs the exact byte size
Player* p = (Player*)malloc(sizeof(Player));
if (p != NULL) {
// 2. Initialize: You MUST do this manually
strcpy(p->name, "AdrianMtzTrev");
p->score = 0;
if (p != NULL) {
// 2. Initialize: You MUST do this manually
strcpy(p->name, "Hero");
p->score = 0;
printf("Player %s joined with score %d\n", p->name, p->score);
// 3. Free: Just drops the memory
free(p);
}
return 0;
printf("Player %s joined with score %d\n", p->name, p->score);
// 3. Free: Just drops the memory
free(p);
}
```
return 0;
}
```
### C++
@@ -47,47 +48,51 @@ Syntax and Semantics
* Supports both functions and classes.
* Incorporates different programming paradigms.
* Memory management can be manual (like C) or rely on constructors/destructors and smart pointers.
```cpp
#include <iostream>
#include <string>
class Player {
public:
std::string name;
int score;
Here's the equivalent example using C++ classes:
// Constructor: Runs automatically during 'new'
Player(std::string n) : name(n), score(0) {
std::cout << name << " enters the game!\n";
}
```cpp
#include <iostream>
#include <string>
// Destructor: Runs automatically during 'delete'
~Player() {
std::cout << name << " has left the game.\n";
}
};
class Player {
public:
std::string name;
int score;
int main() {
// 1. Create: Allocates AND initializes in one step
Player* p = new Player("Hero");
std::cout << "Current score: " << p->score << std::endl;
// 2. Destroy: Runs the destructor first, then releases memory
delete p;
return 0;
// Constructor: Runs automatically during 'new'
Player(std::string n) : name(n), score(0) {
std::cout << name << " enters the game!\n";
}
```
// Destructor: Runs automatically during 'delete'
~Player() {
std::cout << name << " has left the game.\n";
}
};
int main() {
// 1. Create: Allocates AND initializes in one step
Player* p = new Player("Elizabeth");
std::cout << "Current score: " << p->score << std::endl;
// 2. Destroy: Runs the destructor first, then releases memory
delete p;
return 0;
}
```
With **smart pointers**
```cpp
int main() {
// The "Modern" C++ way - no 'delete' required!
auto p = std::make_unique<Player>("Hero");
std::cout << "Current score: " << p->score << std::endl;
} // p is automatically destroyed here
```
```cpp
int main() {
// The "Modern" C++ way - no 'delete' required!
auto p = std::make_unique<Player>("Ricardo");
std::cout << "Current score: " << p->score << std::endl;
} // p is automatically destroyed here
```
Code Reusability and Modularity