commit 19de230e109773c7cdcdbe140992cca793f8317e46048e1ece5506f8b71b83c3 Author: Tyler Beckman Date: Fri Sep 20 00:56:59 2024 -0600 upload diff --git a/.vscode/c_cpp_properties.json b/.vscode/c_cpp_properties.json new file mode 100644 index 0000000..357fb81 --- /dev/null +++ b/.vscode/c_cpp_properties.json @@ -0,0 +1,18 @@ +{ + "configurations": [ + { + "name": "linux-gcc-x64", + "includePath": [ + "${workspaceFolder}/**" + ], + "compilerPath": "/usr/bin/gcc", + "cStandard": "${default}", + "cppStandard": "${default}", + "intelliSenseMode": "linux-gcc-x64", + "compilerArgs": [ + "" + ] + } + ], + "version": 4 +} \ No newline at end of file diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..e1d91e6 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,24 @@ +{ + "version": "0.2.0", + "configurations": [ + { + "name": "C/C++ Runner: Debug Session", + "type": "cppdbg", + "request": "launch", + "args": [], + "stopAtEntry": false, + "externalConsole": false, + "cwd": "/home/ty/Dev/School/CSCI200/Sets/2/L2B", + "program": "/home/ty/Dev/School/CSCI200/Sets/2/L2B/build/Debug/outDebug", + "MIMode": "gdb", + "miDebuggerPath": "gdb", + "setupCommands": [ + { + "description": "Enable pretty-printing for gdb", + "text": "-enable-pretty-printing", + "ignoreFailures": true + } + ] + } + ] +} \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..6e42470 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,59 @@ +{ + "C_Cpp_Runner.cCompilerPath": "gcc", + "C_Cpp_Runner.cppCompilerPath": "g++", + "C_Cpp_Runner.debuggerPath": "gdb", + "C_Cpp_Runner.cStandard": "", + "C_Cpp_Runner.cppStandard": "", + "C_Cpp_Runner.msvcBatchPath": "", + "C_Cpp_Runner.useMsvc": false, + "C_Cpp_Runner.warnings": [ + "-Wall", + "-Wextra", + "-Wpedantic", + "-Wshadow", + "-Wformat=2", + "-Wcast-align", + "-Wconversion", + "-Wsign-conversion", + "-Wnull-dereference" + ], + "C_Cpp_Runner.msvcWarnings": [ + "/W4", + "/permissive-", + "/w14242", + "/w14287", + "/w14296", + "/w14311", + "/w14826", + "/w44062", + "/w44242", + "/w14905", + "/w14906", + "/w14263", + "/w44265", + "/w14928" + ], + "C_Cpp_Runner.enableWarnings": true, + "C_Cpp_Runner.warningsAsError": false, + "C_Cpp_Runner.compilerArgs": [], + "C_Cpp_Runner.linkerArgs": [], + "C_Cpp_Runner.includePaths": [], + "C_Cpp_Runner.includeSearch": [ + "*", + "**/*" + ], + "C_Cpp_Runner.excludeSearch": [ + "**/build", + "**/build/**", + "**/.*", + "**/.*/**", + "**/.vscode", + "**/.vscode/**" + ], + "C_Cpp_Runner.useAddressSanitizer": false, + "C_Cpp_Runner.useUndefinedSanitizer": false, + "C_Cpp_Runner.useLeakSanitizer": false, + "C_Cpp_Runner.showCompilationTime": false, + "C_Cpp_Runner.useLinkTimeOptimization": false, + "C_Cpp_Runner.msvcSecureNoWarnings": false +} \ No newline at end of file diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..923096a --- /dev/null +++ b/Makefile @@ -0,0 +1,68 @@ +TARGET = L2B +SRC_FILES = main.cpp + +# Tyler's custom makefile extensions +.DEFAULT_GOAL := all # Necessary so `make` doesn't run the "pack" target, as it is declared before "all" +.PHONY: pack clean-run c run + +## Adds only the necessary files for build into a .tar.gz file, named appropriately +ARCHIVED_FILES = Makefile $(SRC_FILES) $(SRC_FILES:.cpp=.h) $(SRC_FILES:.cpp=.hpp) +pack: + tar --ignore-failed-read -czvf $(TARGET).tar.gz $(shell echo $(ARCHIVED_FILES) | xargs ls -d 2>/dev/null) + +## Runs the pack target and then attempts to build & run the program to make sure it functions correctly +pack-test: pack + $(eval TMP := $(shell mktemp -d)) + tar -xvzf $(TARGET).tar.gz --directory $(TMP) + make -C $(TMP) + $(TMP)/$(TARGET) + rm -rf $(TMP) + +## An extension of the clean command that is shorter to type and removes a potential .tar.gz file +c: clean + $(DEL) -f $(TARGET).tar.gz + +## Simply builds and then executes the program +run: all + ./$(TARGET) + +# NO EDITS NEEDED BELOW THIS LINE + +CXX = g++ +CXXFLAGS = -O2 +CXXFLAGS_DEBUG = -g +CXXFLAGS_WARN = -Wall -Wextra -Wunreachable-code -Wshadow -Wpedantic +CPPVERSION = -std=c++17 + +OBJECTS = $(SRC_FILES:.cpp=.o) + +ifeq ($(shell echo "Windows"), "Windows") + TARGET := $(TARGET).exe + DEL = del + Q = +else + DEL = rm -f + Q = " +endif + +all: $(TARGET) + +$(TARGET): $(OBJECTS) + $(CXX) -o $@ $^ + +.cpp.o: + $(CXX) $(CXXFLAGS) $(CPPVERSION) $(CXXFLAGS_DEBUG) $(CXXFLAGS_WARN) -o $@ -c $< + +clean: + $(DEL) -f $(TARGET) $(OBJECTS) Makefile.bak + +depend: + @sed -i.bak '/^# DEPENDENCIES/,$$d' Makefile + @$(DEL) sed* + @echo $(Q)# DEPENDENCIES$(Q) >> Makefile + @$(CXX) -MM $(SRC_FILES) >> Makefile + +.PHONY: all clean depend + +# DEPENDENCIES +main.o: main.cpp diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..9bbc4c4 --- /dev/null +++ b/main.cpp @@ -0,0 +1,110 @@ +#include +#include +#include + +int main(void) { + // Part 1 + std::cout << "! ------------- [Part 1] -------------" << std::endl; + + std::cout << "! Setting integer 1 and 2 to 4 and 5 respectively, directly" + << std::endl; + int iNum = 4; + int iNum2 = 5; + + std::cout << "! Creating pointer 1 and 2 with null pointer values" + << std::endl; + int *pINum1 = nullptr; + int *pINum2 = nullptr; + + std::cout << "! Setting pointer 1 and 2 to the memory addresses of integer 1 " + "and 2 respectively" + << std::endl; + pINum1 = &iNum; + + pINum2 = &iNum2; + + std::cout << "Pointer 1 address: " << pINum1 << std::endl; + std::cout << "Integer 1 address: " << &iNum << std::endl << std::endl; + + std::cout << "Pointer 2 address: " << pINum2 << std::endl; + std::cout << "Integer 2 address: " << &iNum2 << std::endl << std::endl; + + std::cout << "Integer 1 value (dereferenced from pointer 1): " << *pINum1 + << std::endl; + + std::cout << "Integer 2 value (dereferenced from pointer 2): " << *pINum2 + << std::endl; + + // Part 2 + std::cout << std::endl + << "! ------------- [Part 2] -------------" << std::endl; + + std::cout << "! Setting integer 1 value directly to 6" << std::endl; + iNum = 6; + + std::cout << "Integer 1 value (directly): " << iNum << std::endl; + + std::cout << "Integer 1 value (dereferenced from pointer 1): " << *pINum1 + << std::endl; + + // Part 3 + std::cout << std::endl << "------------- [Part 3] -------------" << std::endl; + + std::cout << "! Setting value referenced by pointer 1 to a 7" << std::endl; + *pINum1 = 7; + + std::cout << "Integer 1 value (directly): " << iNum << std::endl; + + // Part 4 + std::cout << std::endl << "------------- [Part 4] -------------" << std::endl; + + std::cout << "! Setting pointer 2 address to the same as pointer 1" + << std::endl; + pINum2 = pINum1; + + std::cout << "Pointer 2 address: " << pINum2 << std::endl; + + std::cout << "Value of pointer 2 dereference: " << *pINum2 << std::endl; + + std::cout << "! Setting underlying value of pointer 2 to 8" << std::endl; + *pINum2 = 8; + + std::cout << std::endl + << "Integer 1 value (dereferenced from pointer 1): " << *pINum1 + << std::endl; + std::cout << "Integer 1 value (dereferenced from pointer 2): " << *pINum2 + << std::endl; + std::cout << "Integer 1 value (directly): " << iNum << std::endl << std::endl; + + std::cout << "Integer 2 value (directly): " << iNum2 << std::endl; + + // Part 5 + std::cout << std::endl << "------------- [Part 5] -------------" << std::endl; + + std::cout << "! Creating a double pointer with a null pointer value" + << std::endl; + double *pDNum = nullptr; + + // pDNum = &iNum; error: cannot convert ‘int*’ to ‘double*’ in assignment + + // pDNum = pINum1; error: cannot convert ‘int*’ to ‘double*’ in assignment + + std::cout << "! Creating a double with value 14.25" << std::endl; + double dNum = 14.25; + + std::cout << "! Setting double pointer to the memory address of the double" + << std::endl; + pDNum = &dNum; + + std::cout << "Address of double (from double pointer): " << pDNum + << std::endl; + std::cout << "Value of double (from double pointer): " << *pDNum << std::endl; + + std::cout << "! Assigning the value pointed at by pointer 1 to the value " + "pointed at by the double pointer" + << std::endl; + *pDNum = *pINum1; + + std::cout << "Value of double (directly): " << dNum << std::endl; + std::cout << "Value of double (from double pointer): " << *pDNum << std::endl; +} \ No newline at end of file