Skip to content

Commit 596b1f6

Browse files
committed
Button
1 parent 8b705be commit 596b1f6

File tree

82 files changed

+2946
-634
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

82 files changed

+2946
-634
lines changed

C++/01 C++ - Introduction.md

+57-2
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ Let's make a constant variable of type double (which means it's a decimal number
156156

157157
Also, constants are usually declared with all caps
158158

159-
!! Note, when you declare variables outside of a function, they're **GLOBAL** they can be read by all functions. But if they're declared WITHIN a fucntion, they're **LOCAL** and can only be read by the function in question.
159+
!! Note, when you declare variables outside of a function, they're **GLOBAL** they can be read by all functions. But if they're declared WITHIN a function, they're **LOCAL** and can only be read by the function in question.
160160

161161
**Other variable data types you have are**
162162
`char` (Stores a single character, a single byte)
@@ -169,7 +169,7 @@ Also, constants are usually declared with all caps
169169
`unsigned int` (Same size as signed version)
170170
`long double` (Not less than double)
171171

172-
Example Declaration:
172+
**Example Variable Initialisation:**
173173

174174
```c++
175175
bool isTrue = true;
@@ -180,6 +180,55 @@ char myChar = 'a';
180180
// Double quotes " declare strings (a null terminated array)
181181
// So, if you did char myChar = "a"; It will throw an error! As what you're really saying is
182182
// char myChar = {'a','\0'}; Which is too big for the array
183+
184+
// You can also initialise multiple variables OF THE SAME TYPE on the same line
185+
int x = 6, y = 2;
186+
```
187+
188+
189+
190+
#### **Multiple ways to initialise a variable**
191+
192+
But it actually turns out that there are three ways to initialise a variable!
193+
194+
You **generally** (not always!) want to prioritise the way you initialise variables in this order, because as we go down, memory use becomes less efficient (assignment creates takes up more space in memory since memory is allocated to the variable (which will be empty) in addition to the data.)
195+
196+
- Brace initialisation (Supported from C++11 onwards) (aka uniform initialisation)
197+
- Direct initialisation (Almost as good as brace, but not supported for some types)
198+
- Copy Initialisation (Assignment) (Bad for memory, and also not supported for some types)
199+
200+
**Brace Initialisation**
201+
202+
Read more: https://mbevin.wordpress.com/2012/11/16/uniform-initialization/
203+
204+
```c++
205+
int variable{5}; // Now stores 5
206+
int empty_variable{}; // Empty
207+
208+
int error_variable{4.5}; // Brace initialisation disallows 'narrowing' type conversions
209+
// You'll get an error!
210+
211+
212+
int array_var[] {1, 2, 3, 4, 5}; // Oh look an array!
213+
std::vector<int> vector_var {1, 2, 3, 4, 5}; // Oh my a vector! (Read about vectors later)
214+
std::set<int> set_var {1, 2, 3, 4, 5}; // Check Python tutorial for set
215+
std::map<int, std::string> map_var { {0, "zero"}, {1, "one"}, {2, "two"} }; // A 'DICT'!
216+
```
217+
218+
**Direct Initialisation**
219+
220+
```c++
221+
int variable(525); // Now stores 5
222+
223+
// Note: variable() will not work! You can't initialise an empty variable that way
224+
```
225+
226+
**Copy Initialisation** (Lowest priority, try to avoid)
227+
228+
```c++
229+
// We've seen this before
230+
231+
int a = 5; // Woo
183232
```
184233

185234

@@ -844,3 +893,9 @@ int b = a; // Error: name 'a' is not in scope
844893
. |\-^-/| .
845894
/| } O.=.O { |\
846895
```
896+
897+
898+
899+
------
900+
901+
[![Yeah! Buy the DRAGON a COFFEE!](E:/coding-notes/_assets/COFFEE%20BUTTON%20%E3%83%BE(%C2%B0%E2%88%87%C2%B0%5E).png)](https://www.buymeacoffee.com/methylDragon)

C++/02 C++ - Functions and File IO.md

+89
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,90 @@ addNumbers(1, 2); // It'll return 3
7878
7979

8080

81+
#### **Returning arrays**
82+
83+
Read more + code source: https://www.geeksforgeeks.org/return-local-array-c-function/
84+
85+
Turns out you **can't return arrays** from functions just like that. It's not so simple.
86+
87+
This is a little pre-mature since we have to go into OOP and/or pointers to do it.. but.. Here are the several ways to "return an array" from a function.
88+
89+
**Return a dynamically allocated array pointer**
90+
91+
Like so: `int *arr = new int[100];`
92+
93+
```c++
94+
int *fun()
95+
{
96+
int *arr = new int[100]; // HERE IT IS!
97+
98+
arr[0] = 10;
99+
arr[1] = 20;
100+
101+
return arr; // Return the pointer to the array!
102+
}
103+
104+
int main()
105+
{
106+
int *ptr = fun();
107+
cout << ptr[0] << " " << ptr[1]; // Prints 10 20
108+
return 0;
109+
}
110+
```
111+
112+
**Return a static array**
113+
114+
Like so: `static int arr[100];`
115+
116+
```c++
117+
int *fun()
118+
{
119+
static int arr[100] // HERE IT IS!
120+
121+
arr[0] = 10;
122+
arr[1] = 20;
123+
124+
return arr; // Return the pointer to the array!
125+
}
126+
127+
int main()
128+
{
129+
int *ptr = fun();
130+
cout << ptr[0] << " " << ptr[1]; // Prints 10 20
131+
return 0;
132+
}
133+
```
134+
135+
**Use Structs** (Probably my preferred method)
136+
137+
```c++
138+
struct arrWrap
139+
{
140+
int arr[100];
141+
};
142+
143+
struct arrWrap fun() // Function returns the struct
144+
{
145+
struct arrWrap x;
146+
147+
x.arr[0] = 10;
148+
x.arr[1] = 20;
149+
150+
return x;
151+
}
152+
153+
int main()
154+
{
155+
struct arrWrap x = fun();
156+
cout << x.arr[0] << " " << x.arr[1]; // And you can access the struct members
157+
return 0;
158+
}
159+
```
160+
161+
162+
163+
164+
81165
### 2.2 Function Overloading <a name="2.2"></a>
82166
83167
[go to top](#top)
@@ -202,3 +286,8 @@ if(! writer2) { //Check to see if the filestream is open
202286
/| } O.=.O { |\
203287
```
204288
289+
290+
291+
------
292+
293+
[![Yeah! Buy the DRAGON a COFFEE!](E:/coding-notes/_assets/COFFEE%20BUTTON%20%E3%83%BE(%C2%B0%E2%88%87%C2%B0%5E).png)](https://www.buymeacoffee.com/methylDragon)

C++/03 C++ - Pointers.md

+75-3
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ I'll be adapting it from the ever amazing Derek Banas: https://www.youtube.com/w
2828
2.5 [Arrays with Pointers](#2.5)
2929
2.6 [Indirection (Pointers of Pointers)](#2.6)
3030
2.7 [When to use Pointers and when to use References](#2.7)
31+
2.8 [Memory Management](#2.8)
3132

3233

3334

@@ -253,7 +254,7 @@ Basic concept: When an array is initialised in C++, each successive array elemen
253254
```c++
254255
// Let's try this out!
255256
int numbers[5] = {1, 2, 3, 4, 5}; // Initialise an array
256-
int* numbersPtr = numbers; // And a pointer!
257+
int* numbersPtr = numbers; // And a pointer which stores the pointer to the first element!
257258

258259
cout << "Before: " << *numbersPtr << endl;
259260

@@ -316,7 +317,6 @@ https://stackoverflow.com/questions/5580761/why-use-double-pointer-or-why-use-po
316317
> - but now you want to change that address.
317318
> - you could, by doing pointer1 = pointer2, and pointer1 would now have the address of pointer2.
318319
> - BUT! if you want a function to do that for you, and you want the result to persist after the function is done, you need do some extra work, you need a new pointer3 just to point to pointer1, and pass pointer3 to the function.
319-
> - here is a fun example (take a look at the output bellow first, to understand!):
320320
321321
Another example of indirection being used, is with strings!
322322

@@ -348,8 +348,80 @@ Pointers can also be altered to point to other memory addresses! But references
348348

349349

350350

351+
### 2.8 Memory Management <a name="2.8"></a>
352+
353+
[go to top](#top)
354+
355+
Read more: http://www.cplusplus.com/doc/tutorial/dynamic/
356+
357+
I have to say it here, since we've covered pointers. The reason why C++ is so powerful (and hard), is because you have to control how memory is managed in the program. This means cleaning up after yourself if you want to no longer use variables, controlling the memory flow, assigning pointers, and all the like. It goes really, really deep.
358+
359+
So here's a small primer! For **dynamic (runtime) memory management.**
360+
361+
362+
363+
#### **new and new[]**
364+
365+
> You use `new` if you want objects to persist until you `delete` it. But if you don't specifically need that, DO NOT USE NEW because it's expensive memory-wise.
366+
367+
`new` reserves memory, and returns a pointer to the memory that was allocated! If you assign an array, then it returns the pointer to the first element of the array.
368+
369+
```c++
370+
int * foo; // Initialise a pointer
371+
int * bar; // Another one
372+
373+
foo = new int; // And allocate memory to it
374+
bar = new int [5]; // Or perhaps an array of size 5?
375+
```
376+
377+
> But be careful! Memory allocated in this way needs to be disposed of manually using `delete.`
378+
>
379+
> Otherwise it becomes a memory leak, which, if accumulated enough such that your program runs out of the memory allocated to it, crashes.
380+
381+
382+
383+
#### **delete and delete[]**
384+
385+
`delete` is how you do garbage disposal. Be wary of using pointers you've deleted though! Since the memory space previously allocated to the pointer could have been used for other things, or would be filled with garbage.
386+
387+
```c++
388+
delete ptr; // To delete pointers
389+
delete[] array_ptr; // To delete array pointers
390+
```
391+
392+
393+
394+
#### **Assignment Failures**
395+
396+
From: http://www.cplusplus.com/doc/tutorial/dynamic/
397+
398+
If you try to store a variable that will take too much memory than the memory that is available, the assignment statement will fail.
399+
400+
**You can catch this exception!**
401+
402+
Alternatively, if you want to still let it pass, then use `nothrow`, which will return a null pointer instead (which is still bad, and you still have to verify if it worked, so it's less efficient, but easier to write intially...)
403+
404+
```c++
405+
foo = new int [5]; // if allocation fails, an exception is thrown
406+
407+
// Or using (nothrow)
408+
409+
int * bar;
410+
bar = new (nothrow) int [5];
411+
if (bar == nullptr) { // Check to see if bar was assigned a null pointer
412+
// error assigning memory. Take measures.
413+
}
414+
```
415+
416+
417+
351418
```
352419
. .
353420
. |\-^-/| .
354421
/| } O.=.O { |\
355-
```
422+
```
423+
424+
425+
------
426+
427+
[![Yeah! Buy the DRAGON a COFFEE!](E:/coding-notes/_assets/COFFEE%20BUTTON%20%E3%83%BE(%C2%B0%E2%88%87%C2%B0%5E).png)](https://www.buymeacoffee.com/methylDragon)

0 commit comments

Comments
 (0)