diff options
author | msglm <msglm@techchud.xyz> | 2023-01-14 05:31:48 -0600 |
---|---|---|
committer | msglm <msglm@techchud.xyz> | 2023-01-14 05:31:48 -0600 |
commit | 9d53d8857eaa1c9405894a88ca75bc4657e42f35 (patch) | |
tree | eb1efc1d028b949dd83bb710c68be8eff58f26e7 /C++/Pointers | |
download | school-code-master.tar.gz school-code-master.tar.bz2 school-code-master.zip |
Diffstat (limited to 'C++/Pointers')
-rw-r--r-- | C++/Pointers/Pointers.cpp | 87 |
1 files changed, 87 insertions, 0 deletions
diff --git a/C++/Pointers/Pointers.cpp b/C++/Pointers/Pointers.cpp new file mode 100644 index 0000000..44a46a1 --- /dev/null +++ b/C++/Pointers/Pointers.cpp @@ -0,0 +1,87 @@ +// Name: msglm +// Date: Oct 26th 2022 +// Work Name: Pointers +// Description: an experiment in bad solutions to bad problems (but much more forgivable than other solutions) + + +////What is a pointer? +//Pointers are variables whos entire job is to point to another location of memory. + +////What are advantanges of using pointers in our programs? +//Pointers are great for causing side effects in your program, especially for dealing with IO issues. For example, I often create an ifstream variable and pass it by reference to a function that deals with the boilerplate initalization code. +// +//Pointers also tend to be used in advanced data structures like linked-lists, double linked-lists. +// +//Pointers can also be used for dynamic memory allocation +// +//Pointers are also amazing at digging out security holes, if you're interested in doing that for some reason. This is because humans are notoriously terrible at keeping track of things and tend to destroy things that shouldn't ne or forget to destroy things when they should. +//Because of this problem, humans invented the garbage collector, a program that automatically deals with that for people. This removes nasty pointer issue from your life, but at the cost of CPU cycles. +//Sometimes, that just wont cut it. No one wants to waste CPU cycles checking if something should be freed when they're running an Operating System, so people start to bring back the pointer and all of it's lovely security flaws and segmentation faults that come with it. +//Then, one day, someone invented the "Borrow Checker", a tool to help programmers ensure memory safety while still being able to use pointers. +//However, like anything shiny, new, and modern, it's actually a hellish and complex ploy made to distract man from the one true programming paradigm that would save us all from dangling pointer hell: Stack-orientated programming. +//That's right, FORTH is the solution to all problems, boot up a copy of CollapseOS (http://collapseos.org/) and start programming on the stack! +// +//jokes aside, all solutions to this problem are horrible. Garbage collection and stack-based programming seems to be the least bad, but both are still awful. Garbage collection is SLOW. Borrow checkers or code pre-processors may be the true solution in the long run, but borrow checkers are their own complex beasts of rules and pain. On top of that, the only real "borrow checker" language is Rust and Rust's syntax somehow makes me appreciate Java. + + +////How have we been using pointers in our programs already? +//Pass-by-reference may be consdiered the use of pointers, however, references are the constants of the pointer world and are different enough that most people consdier them incopatable, however, it's the closest the class has gotten so far. + +////Name and describe three differences between references and pointers +// 1. References are may not be reassigned upon being created +// 2. References must reference an datum already in existance +// 3. References cannot ever be NULL + +//Write a short program demonstrating how pointers work (use at least one function please and no copy/paste from any source, make up your own program based on what you read) + + +// Name: msglm +// Date: Oct 26th 2022 +// Work Name: Truth-To-Lies +// Description: Using pointer arithmatic and other dark magic, turn a truthful statement into a lie. + +#include <iostream> +#include <string> +using namespace std; + +int main() { + +char text[] = "Pointers are a security disaster, borrow checkers are way too complex, garbage collection is slow, and stack-orientated programming is too limited, but all these problems are merely hetrodoxical qubbling when compared to the heathenry that is OOP and its inheretence disaster."; +char *pointerText; + +pointerText = text; + +pointerText = pointerText + 1; +cout << *pointerText; +pointerText = pointerText + 34; +cout << *pointerText; +pointerText = pointerText + 30; +cout << *pointerText; +pointerText = pointerText + 5; +cout << *pointerText; +pointerText = pointerText + 16; +cout << *pointerText; +pointerText = pointerText + 5; +cout << *pointerText; +pointerText = pointerText + 28; +cout << *pointerText; +pointerText = pointerText + 4; +cout << *pointerText; +pointerText = pointerText + 1; +cout << *pointerText; +pointerText = pointerText + 34; +cout << *pointerText; +pointerText = pointerText + 13; +cout << *pointerText; +pointerText = pointerText + 13; +cout << *pointerText; +cout << "!" << endl; + +} +/* + This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License Version 3 ONLY as published by the Free Software Foundation. + + This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License along with this program. If not, see <https://www.gnu.org/licenses/>. + */ |