Skip to content

Commit

Permalink
static -> extern
Browse files Browse the repository at this point in the history
  • Loading branch information
mikolasan committed Sep 6, 2024
1 parent ad74bb9 commit 44fdb25
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 20 deletions.
2 changes: 1 addition & 1 deletion basics/global/somewhere.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "somewhere.h"

// int ggg::global_variable = 1; // redefinition of ‘int ggg::global_variable’
int ggg::global_variable = 1; // redefinition of ‘int ggg::global_variable’
// ggg::global_variable = 1; // ‘global_variable’ in namespace ‘ggg’ does not name a type

int mmm::global_variable_that_works = 1;
2 changes: 1 addition & 1 deletion basics/global/somewhere.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

namespace ggg {
static int global_variable = 1;
extern int global_variable;
}

class mmm {
Expand Down
34 changes: 17 additions & 17 deletions cryptography/sha-test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,25 +74,25 @@ digest_array sha256(const std::vector<char>& input)
// }

// deprecated version
// std::string_view sha256(const std::list<std::string_view>& input)
// {
// SHA256_CTX context;
// if (SHA256_Init(&context) == 0) {
// std::cerr << "sha256 context init failed" << std::endl;
// }
std::string_view sha256_deprecated(const std::list<std::string_view>& input)
{
SHA256_CTX context;
if (SHA256_Init(&context) == 0) {
std::cerr << "sha256 context init failed" << std::endl;
}

// for (const auto& i : input) {
// if (SHA256_Update(&context, i.data(), i.size()) == 0) {
// std::cerr << "sha256 update failed" << std::endl;
// }
// }
for (const auto& i : input) {
if (SHA256_Update(&context, i.data(), i.size()) == 0) {
std::cerr << "sha256 update failed" << std::endl;
}
}

// std::array<unsigned char, SHA256_DIGEST_LENGTH> digest;
// if (SHA256_Final(digest.data(), &context) == 0) {
// std::cerr << "sha256 final failed" << std::endl;
// }
// return std::string_view(digest.begin(), digest.end());
// }
std::array<unsigned char, SHA256_DIGEST_LENGTH> digest;
if (SHA256_Final(digest.data(), &context) == 0) {
std::cerr << "sha256 final failed" << std::endl;
}
return std::string_view(digest.begin(), digest.end());
}

int main(int argc, char const *argv[])
{
Expand Down
4 changes: 3 additions & 1 deletion src/basics/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Like really global global variable that is defined in one compilation unit but c
**somewhere.h**

```cpp
static int global_variable;
extern int global_variable;
```

**somewhere.cpp**
Expand All @@ -24,3 +24,5 @@ void foo()
global_variable = 2;
}
```

## Heap VS stack

1 comment on commit 44fdb25

@mikolasan
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.