-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
34 changed files
with
2,035 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
const { readdirSync } = require('node:fs'); | ||
|
||
let exitCode = 0; | ||
|
||
const next = ['concepts', 'terms']; | ||
|
||
function checkPath(path, i) { | ||
for (const entry of readdirSync(path)) { | ||
const entryDir = readdirSync(`${path}/${entry}`); | ||
if (!entryDir.includes(`${entry}.md`)) { | ||
console.error(`\n${path}/${entry} should contain a ${entry}.md file\n`); | ||
exitCode = 1; | ||
} | ||
if (i < 2 && entryDir.includes(next[i])) { | ||
checkPath(`${path}/${entry}/${next[i]}`, i + 1); | ||
} | ||
} | ||
} | ||
|
||
checkPath('content', 0); | ||
|
||
process.exit(exitCode); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
content/c/concepts/user-input/user-input.md | ||
content/ruby/concepts/sets/sets.md |
59 changes: 59 additions & 0 deletions
59
content/cpp/concepts/variables/terms/volatile-variables/volatile-variables.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
--- | ||
Title: 'Volatile Variables' | ||
Description: 'Ensures variables avoid compiler optimization, allowing correct access when their values may change due to external factors.' | ||
Subjects: | ||
- 'Computer Science' | ||
- 'Code Foundations' | ||
Tags: | ||
- 'Memory' | ||
- 'Variables' | ||
- 'Variable Types' | ||
CatalogContent: | ||
- 'learn-c-plus-plus' | ||
- 'paths/computer-science' | ||
--- | ||
|
||
**Volatile variables** in C++ are not optimized or cached by the compiler. Marking a variable as volatile is appropriate when its value may be altered by external factors beyond the program's control. This instructs the compiler to read the most recent value from memory instead of a potentially outdated cached version. However, it's important to note that declaring a variable as volatile does not ensure atomicity or synchronize memory between threads; it solely prevents compiler optimization, which is particularly crucial in multithreaded environments. | ||
|
||
## Syntax | ||
|
||
To declare a variable as volatile, the `volatile` keyword needs to be placed before the variable type: | ||
|
||
```pseudo | ||
volatile data_type variable_name; | ||
``` | ||
|
||
## Example | ||
|
||
A volatile variable signals a worker thread to stop running and performing tasks in the following example. The `volatile` keyword prevents the compiler from optimizing away, continuously checking the variable while in the loop. The worker thread will continue until the `isRunning` variable is false. | ||
|
||
```cpp | ||
#include <iostream> | ||
#include <thread> | ||
#include <chrono> | ||
class VolatileExample { | ||
private: | ||
// 'volatile' tells the compiler not to optimize this variable, ensuring that each iteration of the following loop fetches the latest value. | ||
volatile bool isRunning = true; | ||
public: | ||
void runTask() { | ||
while (isRunning) { | ||
std::cout << "Task is running..." << std::endl; | ||
std::this_thread::sleep_for(std::chrono::seconds(1)); | ||
} | ||
std::cout << "Task has stopped." << std::endl; | ||
} | ||
void stopTask() { | ||
isRunning = false; // Changing 'isRunning' to false to stop the task. | ||
} | ||
}; | ||
int main() { | ||
VolatileExample example; | ||
std::thread workerThread(&VolatileExample::runTask, &example); | ||
// The task runs for a while before stopping it. | ||
std::this_thread::sleep_for(std::chrono::seconds(5)); | ||
example.stopTask(); // Stop the task | ||
workerThread.join(); // Waits for the thread to finish. | ||
return 0; | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
--- | ||
Title: 'Amend' | ||
Description: 'Modifies the most recent Git commit without creating a new one.' | ||
Subjects: | ||
- 'Bash/Shell' | ||
- 'Developer Tools' | ||
Tags: | ||
- 'Command Line' | ||
- 'Documentation' | ||
- 'Error Handling' | ||
- 'Git' | ||
CatalogContent: | ||
- 'learn-git' | ||
- 'paths/computer-science' | ||
--- | ||
|
||
**Amend** is a Git feature that allows developers to modify the most recent commit. It is commonly used to correct mistakes such as adding a missing file, fixing an incorrect commit message, or making minor adjustments without creating an entirely new commit. | ||
|
||
This feature is particularly helpful for maintaining a clean and concise commit history, ensuring that changes are logically grouped and easy to understand. | ||
|
||
## Syntax | ||
|
||
The syntax for using the amend feature in Git is: | ||
|
||
```pseudo | ||
git commit --amend | ||
``` | ||
|
||
To amend both the content of the commit and the commit message, the following would be used: | ||
|
||
```pseudo | ||
git commit --amend -m "Your new commit message" | ||
``` | ||
|
||
## Example 1 | ||
|
||
Suppose, the developer created and committed a feature, but forgot to include a file: | ||
|
||
```shell | ||
# Original commit | ||
echo "Initial code" > feature.txt | ||
echo "Forgotten content" > forgotten-file.txt | ||
git add feature.txt # Dev forgot to add forgotten-file.txt | ||
git commit -m "Add initial code for the feature" | ||
``` | ||
|
||
Here's the original commit history: | ||
|
||
```shell | ||
git log --oneline | ||
abc1234 Add initial code for the feature | ||
``` | ||
|
||
The developer realized that he hasn't included a file in the original commit. So, he performs the amend operation to both include the file in that commit and change the commit message: | ||
|
||
```shell | ||
# Amending the original commit | ||
git add forgotten-file.txt | ||
git commit --amend -m "Add initial code for the feature and forgotten file" | ||
``` | ||
|
||
Here's the amended commit history: | ||
|
||
```shell | ||
git log --oneline | ||
def5678 Add initial code for the feature and forgotten file # Commit abc1234 has been replaced by def5678 | ||
``` | ||
|
||
## Example 2 | ||
|
||
Suppose, the developer is going through another commit history: | ||
|
||
```shell | ||
git log --oneline | ||
abc1234 Original commit message | ||
``` | ||
|
||
While going through it, the developer didn't like the above commit message. So, he decides to use the amend operation to only change the commit message without touching the contents of the commit: | ||
|
||
```shell | ||
git commit --amend -m "Corrected commit message" | ||
``` | ||
|
||
Here's the amended commit history: | ||
|
||
```shell | ||
git log --oneline | ||
abc1234 Corrected commit message | ||
``` | ||
|
||
> **Note:** Use `git commit --amend` carefully if the commit has already been shared with others, as it rewrites history. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
--- | ||
Title: 'Checkout' | ||
Description: 'The git checkout command switches, creates and restores branches in the working directory to a specific state.' | ||
Subjects: | ||
- 'Bash/Shell' | ||
- 'Developer Tools' | ||
Tags: | ||
- 'Git' | ||
- 'GitHub' | ||
CatalogContent: | ||
- 'learn-git' | ||
- 'learn-the-command-line' | ||
- 'paths/computer-science' | ||
--- | ||
|
||
The **`git checkout`** command switches, creates and restores branches in the working directory to a specific state. The `git checkout` command also allows switching to a specific commit without changing branches. | ||
|
||
## Syntax | ||
|
||
Checkout with branches: | ||
|
||
```pseudo | ||
git checkout [options] <branch-name> | ||
``` | ||
|
||
- `<branch-name>` specifies the name of the branch to switch to or create. | ||
- `[options]` optional flags that can be used with the checkout command. Here are some of the most commonly used options: | ||
- `-b` creates a new branch with the specified name and switches to it immediately. | ||
- `-` returns to the previously checked-out branch. This flag does not need the `<branch-name>`. | ||
- `-f` (--force) forces the checkout, discarding any local changes in the working directory. | ||
|
||
Checkout to a specific commit: | ||
|
||
```pseudo | ||
git checkout <commit-hash> | ||
``` | ||
|
||
## Switch to an existing branch | ||
|
||
The following command will switch to an already existing branch, created previously with the [git branch](https://www.codecademy.com/resources/docs/git/branch) command: | ||
|
||
```pseudo | ||
git checkout existing-branch | ||
``` | ||
|
||
> **Note**: From Git 2.23, the new specific `git switch` command has been introduced to switch branches, making it clearer and safer than `git checkout` because it avoids the ambiguity of the latter's multi-purpose nature. | ||
## Create and switch to a new branch | ||
|
||
It is possible to create and switch to a new branch with a single command using the `-b` option: | ||
|
||
```pseudo | ||
git checkout -b new-branch | ||
``` | ||
|
||
## Restore a file from a specific commit | ||
|
||
`git checkout` also allows to restore a file from a specific commit using its hash: | ||
|
||
```pseudo | ||
git checkout <commit-hash> -- example.txt | ||
``` | ||
|
||
## Examine a Previous Commit | ||
|
||
`git checkout` also allows temporarily moving to a specific commit without changing branches. This state is called **detached `HEAD` state**: | ||
|
||
```pseudo | ||
git checkout <commit-hash> | ||
``` | ||
|
||
The detached `HEAD` state allows to: | ||
|
||
- Examine the state of the repository at that specified commit. | ||
- Create new branches if the developer needs to start from that point. | ||
- Any code changes made in this state will not be associated with any existing branch unless a new branch is created. |
Oops, something went wrong.