diff --git a/lab1/.vscode/settings.json b/lab1/.vscode/settings.json new file mode 100644 index 0000000..9664ac8 --- /dev/null +++ b/lab1/.vscode/settings.json @@ -0,0 +1,58 @@ +{ + "files.associations": { + "array": "cpp", + "atomic": "cpp", + "bit": "cpp", + "*.tcc": "cpp", + "cctype": "cpp", + "charconv": "cpp", + "clocale": "cpp", + "cmath": "cpp", + "compare": "cpp", + "concepts": "cpp", + "cstdarg": "cpp", + "cstddef": "cpp", + "cstdint": "cpp", + "cstdio": "cpp", + "cstdlib": "cpp", + "ctime": "cpp", + "cwchar": "cpp", + "cwctype": "cpp", + "deque": "cpp", + "string": "cpp", + "unordered_map": "cpp", + "vector": "cpp", + "exception": "cpp", + "algorithm": "cpp", + "functional": "cpp", + "iterator": "cpp", + "memory": "cpp", + "memory_resource": "cpp", + "numeric": "cpp", + "optional": "cpp", + "random": "cpp", + "string_view": "cpp", + "system_error": "cpp", + "tuple": "cpp", + "type_traits": "cpp", + "utility": "cpp", + "format": "cpp", + "fstream": "cpp", + "initializer_list": "cpp", + "iomanip": "cpp", + "iosfwd": "cpp", + "iostream": "cpp", + "istream": "cpp", + "limits": "cpp", + "new": "cpp", + "numbers": "cpp", + "ostream": "cpp", + "span": "cpp", + "sstream": "cpp", + "stdexcept": "cpp", + "streambuf": "cpp", + "text_encoding": "cpp", + "typeinfo": "cpp", + "variant": "cpp" + } +} \ No newline at end of file diff --git a/lab1/Makefile b/lab1/Makefile index aa95b23..8b08bc0 100644 --- a/lab1/Makefile +++ b/lab1/Makefile @@ -1,17 +1,58 @@ -# The following rule means: "if test_editor does not exist, or -# is older than test_editor.o or editor.o, -# then link test_editor". -test_editor: test_editor.o editor.o - g++ -o test_editor test_editor.o editor.o +# Define the compiler and the linker. The linker must be defined since +# the implicit rule for linking uses CC as the linker. g++ can be +# changed to clang++. +CXX = g++ +CC = $(CXX) -# Rules to create the object files. -test_editor.o: test_editor.cc editor.h - g++ -c test_editor.cc -std=c++11 -editor.o: editor.cc editor.h - g++ -c editor.cc -std=c++11 +# Generate dependencies in *.d files +DEPFLAGS = -MT $@ -MMD -MP -MF $*.d +# Define preprocessor, compiler, and linker flags. Uncomment the # lines +# if you use clang++ and wish to use libc++ instead of GNU's libstdc++. +# -g is for debugging. +CPPFLAGS = -std=c++11 -I. +CXXFLAGS = -O2 -Wall -Wextra -pedantic-errors -Wold-style-cast +CXXFLAGS += -std=c++11 +CXXFLAGS += -g +CXXFLAGS += $(DEPFLAGS) +LDFLAGS = -g +#CPPFLAGS += -stdlib=libc++ +#CXXFLAGS += -stdlib=libc++ +#LDFLAGS += -stdlib=libc++ + +# Targets +PROGS = test_editor test_coding print_argv hello encode decode + +all: $(PROGS) + +test: test_coding test_editor + ./test_coding + ./test_editor + ./hello + +test_encode_decode: encode decode + @echo "Running encode-decode tests" + ./encode testfile.txt + ./decode testfile.txt.enc + +# Targets rely on implicit rules for compiling and linking print_argv: print_argv.o - g++ -o print_argv print_argv.o -print_argv.o: print_argv.cc - g++ -c print_argv.cc -std=c++11 +test_editor: test_editor.o editor.o +test_coding: test_coding.o coding.o +encode: encode.o coding.o +decode: decode.o coding.o +# Phony targets +.PHONY: all test test_encode_decode clean distclean + +# Standard clean +clean: + rm -f *.o $(PROGS) + +distclean: clean + rm *.d + + +# Include the *.d files +SRC = $(wildcard *.cc) +-include $(SRC:.cc=.d) diff --git a/lab1/Makefile1 b/lab1/Makefile1 new file mode 100644 index 0000000..aa95b23 --- /dev/null +++ b/lab1/Makefile1 @@ -0,0 +1,17 @@ +# The following rule means: "if test_editor does not exist, or +# is older than test_editor.o or editor.o, +# then link test_editor". +test_editor: test_editor.o editor.o + g++ -o test_editor test_editor.o editor.o + +# Rules to create the object files. +test_editor.o: test_editor.cc editor.h + g++ -c test_editor.cc -std=c++11 +editor.o: editor.cc editor.h + g++ -c editor.cc -std=c++11 + +print_argv: print_argv.o + g++ -o print_argv print_argv.o +print_argv.o: print_argv.cc + g++ -c print_argv.cc -std=c++11 + diff --git a/lab1/MakefileWithDeps b/lab1/MakefileWithDeps deleted file mode 100644 index 29fd9da..0000000 --- a/lab1/MakefileWithDeps +++ /dev/null @@ -1,50 +0,0 @@ -# Define the compiler and the linker. The linker must be defined since -# the implicit rule for linking uses CC as the linker. g++ can be -# changed to clang++. -CXX = g++ -CC = $(CXX) - -# Generate dependencies in *.d files -DEPFLAGS = -MT $@ -MMD -MP -MF $*.d - -# Define preprocessor, compiler, and linker flags. Uncomment the # lines -# if you use clang++ and wish to use libc++ instead of GNU's libstdc++. -# -g is for debugging. -CPPFLAGS = -std=c++11 -I. -CXXFLAGS = -O2 -Wall -Wextra -pedantic-errors -Wold-style-cast -CXXFLAGS += -std=c++11 -CXXFLAGS += -g -CXXFLAGS += $(DEPFLAGS) -LDFLAGS = -g -#CPPFLAGS += -stdlib=libc++ -#CXXFLAGS += -stdlib=libc++ -#LDFLAGS += -stdlib=libc++ - -# Targets -PROGS = test_editor test_coding print_argv - -all: $(PROGS) - -test: test_coding test_editor - ./test_coding - ./test_editor - -# Targets rely on implicit rules for compiling and linking -print_argv: print_argv.o -test_editor: test_editor.o editor.o -test_coding: test_coding.o coding.o - -# Phony targets -.PHONY: all test clean distclean - -# Standard clean -clean: - rm -f *.o $(PROGS) - -distclean: clean - rm *.d - - -# Include the *.d files -SRC = $(wildcard *.cc) --include $(SRC:.cc=.d) diff --git a/lab1/buggy_programs/Makefile b/lab1/buggy_programs/Makefile index 7e015e7..0f14928 100644 --- a/lab1/buggy_programs/Makefile +++ b/lab1/buggy_programs/Makefile @@ -21,20 +21,30 @@ PROGS=ub leak bounds bounds-heap dangling sum ALL: $(PROGS) leak: leak.cc + $(CXX) $(CPPFLAGS) $(CXXFLAGS) $(LDFLAGS) -o $@ $< dangling: dangling.cc + $(CXX) $(CPPFLAGS) $(CXXFLAGS) $(LDFLAGS) -o $@ $< bounds: bounds.cc + $(CXX) $(CPPFLAGS) $(CXXFLAGS) $(LDFLAGS) -o $@ $< ub: ub.cc + $(CXX) $(CPPFLAGS) $(CXXFLAGS) $(LDFLAGS) -o $@ $< sum: sum.cc + $(CXX) $(CPPFLAGS) $(CXXFLAGS) $(LDFLAGS) -o $@ $< -# Targets -# Phony targets .PHONY: all clean test -# Standard clean clean: - -rm $(PROGS) - -rm -r $(addsuffix .dSYM, $(PROGS)) + -rm -f $(PROGS) + -rm -rf $(addsuffix .dSYM, $(PROGS)) + +test: $(PROGS) + @echo "Running tests with Valgrind..." + @for prog in $(PROGS); do \ + echo "Testing $$prog with Valgrind:"; \ + valgrind ./$$prog; \ + echo ""; \ + done diff --git a/lab1/buggy_programs/Makefile.sanitizers b/lab1/buggy_programs/Makefile3 similarity index 100% rename from lab1/buggy_programs/Makefile.sanitizers rename to lab1/buggy_programs/Makefile3 diff --git a/lab1/buggy_programs/bounds b/lab1/buggy_programs/bounds new file mode 100755 index 0000000..64bbff0 Binary files /dev/null and b/lab1/buggy_programs/bounds differ diff --git a/lab1/buggy_programs/bounds-alt b/lab1/buggy_programs/bounds-alt new file mode 100755 index 0000000..2dc8faf Binary files /dev/null and b/lab1/buggy_programs/bounds-alt differ diff --git a/lab1/buggy_programs/bounds-heap b/lab1/buggy_programs/bounds-heap new file mode 100755 index 0000000..d00e208 Binary files /dev/null and b/lab1/buggy_programs/bounds-heap differ diff --git a/lab1/buggy_programs/bounds-heap-alt b/lab1/buggy_programs/bounds-heap-alt new file mode 100755 index 0000000..cfa4f56 Binary files /dev/null and b/lab1/buggy_programs/bounds-heap-alt differ diff --git a/lab1/buggy_programs/dangling b/lab1/buggy_programs/dangling new file mode 100755 index 0000000..d4a00d0 Binary files /dev/null and b/lab1/buggy_programs/dangling differ diff --git a/lab1/buggy_programs/leak b/lab1/buggy_programs/leak new file mode 100755 index 0000000..14051a8 Binary files /dev/null and b/lab1/buggy_programs/leak differ diff --git a/lab1/buggy_programs/sum b/lab1/buggy_programs/sum new file mode 100755 index 0000000..b708220 Binary files /dev/null and b/lab1/buggy_programs/sum differ diff --git a/lab1/buggy_programs/sum-alt b/lab1/buggy_programs/sum-alt new file mode 100755 index 0000000..7a4728a Binary files /dev/null and b/lab1/buggy_programs/sum-alt differ diff --git a/lab1/buggy_programs/ub b/lab1/buggy_programs/ub new file mode 100755 index 0000000..e0ad5a0 Binary files /dev/null and b/lab1/buggy_programs/ub differ diff --git a/lab1/buggy_programs/vgcore.12684 b/lab1/buggy_programs/vgcore.12684 new file mode 100644 index 0000000..81504c8 Binary files /dev/null and b/lab1/buggy_programs/vgcore.12684 differ diff --git a/lab1/buggy_programs/vgcore.13027 b/lab1/buggy_programs/vgcore.13027 new file mode 100644 index 0000000..f2ecbe6 Binary files /dev/null and b/lab1/buggy_programs/vgcore.13027 differ diff --git a/lab1/buggy_programs/vgcore.13797 b/lab1/buggy_programs/vgcore.13797 new file mode 100644 index 0000000..707a34d Binary files /dev/null and b/lab1/buggy_programs/vgcore.13797 differ diff --git a/lab1/coding.cc b/lab1/coding.cc index 6b28096..c80cd87 100644 --- a/lab1/coding.cc +++ b/lab1/coding.cc @@ -2,9 +2,9 @@ unsigned char encode(unsigned char c) { - return c; + return c + 2; } unsigned char decode(unsigned char c) { - return c; + return c - 2; } diff --git a/lab1/coding.d b/lab1/coding.d new file mode 100644 index 0000000..d7ad881 --- /dev/null +++ b/lab1/coding.d @@ -0,0 +1,2 @@ +coding.o: coding.cc coding.h +coding.h: diff --git a/lab1/coding.o b/lab1/coding.o new file mode 100644 index 0000000..988de90 Binary files /dev/null and b/lab1/coding.o differ diff --git a/lab1/decode b/lab1/decode new file mode 100755 index 0000000..8859e72 Binary files /dev/null and b/lab1/decode differ diff --git a/lab1/decode.cc b/lab1/decode.cc new file mode 100644 index 0000000..29b36d8 --- /dev/null +++ b/lab1/decode.cc @@ -0,0 +1,34 @@ +#include +#include +#include "coding.h" + +int main(int argc, char* argv[]) { + std::string filename; + + if (argc > 1) { + filename = argv[1]; + } else { + std::cout << "Enter filename to decode: "; + std::cin >> filename; + } + + std::ifstream infile(filename, std::ios::binary); + if (!infile) { + std::cerr << "Could not open file " << filename << "\n"; + return 1; + } + + std::ofstream outfile(filename + ".dec", std::ios::binary); + if (!outfile) { + std::cerr << "Could not create output file" << "\n"; + return 1; + } + + unsigned char c; + while (infile.get(reinterpret_cast(c))) { + outfile.put(decode(c)); + } + + std::cout << "Decoding complete. Output saved to " << filename << ".dec" << "\n"; + return 0; +} \ No newline at end of file diff --git a/lab1/decode.d b/lab1/decode.d new file mode 100644 index 0000000..994ccb5 --- /dev/null +++ b/lab1/decode.d @@ -0,0 +1,2 @@ +decode.o: decode.cc coding.h +coding.h: diff --git a/lab1/decode.o b/lab1/decode.o new file mode 100644 index 0000000..70f6281 Binary files /dev/null and b/lab1/decode.o differ diff --git a/lab1/editor.cc b/lab1/editor.cc index 82ec95b..e60100d 100644 --- a/lab1/editor.cc +++ b/lab1/editor.cc @@ -12,5 +12,23 @@ size_type Editor::get_size() const } size_type Editor::find_left_par(size_type pos) const { - return string::npos; + if (pos >= text.size() || (text[pos] != ')' && text[pos] != ']' && text[pos] != '}')) { + return std::string::npos; + } + + char right_par = text[pos]; + char left_par = (right_par == ')') ? '(' : (right_par == ']') ? '[' : '{'; + + int balance = 0; + + for (int i = pos; i >= 0; --i) { + if (text[i] == right_par) { + balance++; + } else if (text[i] == left_par) { + balance--; + if (balance == 0) { + return i; + } + } + } } diff --git a/lab1/editor.d b/lab1/editor.d new file mode 100644 index 0000000..b810e0b --- /dev/null +++ b/lab1/editor.d @@ -0,0 +1,2 @@ +editor.o: editor.cc editor.h +editor.h: diff --git a/lab1/editor.o b/lab1/editor.o new file mode 100644 index 0000000..2c5a26f Binary files /dev/null and b/lab1/editor.o differ diff --git a/lab1/encode b/lab1/encode new file mode 100755 index 0000000..04f0bc4 Binary files /dev/null and b/lab1/encode differ diff --git a/lab1/encode.cc b/lab1/encode.cc new file mode 100644 index 0000000..200b329 --- /dev/null +++ b/lab1/encode.cc @@ -0,0 +1,35 @@ +#include +#include +#include "coding.h" + +int main (int argc, char* argv[]) { + std::string filename; + + if (argc > 1) { + filename = argv[1]; + } else { + std::cout << "Enter filename: "; + std::cin >> filename; + } + + std::ifstream infile(filename, std::ios::binary); + if (!infile) { + std::cerr << "Could not open file " << filename << "\n"; + return 1; + } + + std::string output_filename = filename + ".enc"; + std::ofstream outfile(output_filename, std::ios::binary); + if (!outfile) { + std::cerr << "Could not create output file" << "\n"; + return 1; + } + + unsigned char c; + while (infile.get(reinterpret_cast(c))) { + outfile.put(encode(c)); + } + + std::cout << "Encoding complete. Output saved to " << filename << "\n"; + return 0; +} \ No newline at end of file diff --git a/lab1/encode.d b/lab1/encode.d new file mode 100644 index 0000000..66f2ed5 --- /dev/null +++ b/lab1/encode.d @@ -0,0 +1,2 @@ +encode.o: encode.cc coding.h +coding.h: diff --git a/lab1/encode.o b/lab1/encode.o new file mode 100644 index 0000000..824425d Binary files /dev/null and b/lab1/encode.o differ diff --git a/lab1/hello b/lab1/hello new file mode 100755 index 0000000..2529b03 Binary files /dev/null and b/lab1/hello differ diff --git a/lab1/hello.cc b/lab1/hello.cc new file mode 100644 index 0000000..96f5da4 --- /dev/null +++ b/lab1/hello.cc @@ -0,0 +1,7 @@ +#include + +int main() { + std::cout << "Hello, World!" << "\n"; + std::cout << "Hello, World!" << "\n"; + return 0; +} \ No newline at end of file diff --git a/lab1/hello.d b/lab1/hello.d new file mode 100644 index 0000000..a3f4bad --- /dev/null +++ b/lab1/hello.d @@ -0,0 +1 @@ +hello: hello.cc diff --git a/lab1/print_argv b/lab1/print_argv new file mode 100755 index 0000000..048b643 Binary files /dev/null and b/lab1/print_argv differ diff --git a/lab1/print_argv.d b/lab1/print_argv.d new file mode 100644 index 0000000..363ab24 --- /dev/null +++ b/lab1/print_argv.d @@ -0,0 +1 @@ +print_argv.o: print_argv.cc diff --git a/lab1/print_argv.o b/lab1/print_argv.o new file mode 100644 index 0000000..da01df8 Binary files /dev/null and b/lab1/print_argv.o differ diff --git a/lab1/reflektion.txt b/lab1/reflektion.txt new file mode 100644 index 0000000..c676a3e --- /dev/null +++ b/lab1/reflektion.txt @@ -0,0 +1,49 @@ +1. What is the difference between a declaration and a definition? + +A declaration tells the compiler about the existence of a variable or function, +while a definition allocates storage or provides the function code. + + +2. How does an include guard prevent multiple definitions? + +An include guard (using #ifndef, #define, #endif) +prevents a header file from being included multiple times, which avoids redefinitions. + + +3. How can you tell if an error comes from the compiler or the linker? Does a linker error +mean that you have an error in your source code? How do you (typically) fix a linker error? + +Compiler errors are syntax or semantic issues in code, while linker errors occur when symbols +are missing or duplicated across files. A linker error often means a missing definition or library; it’s fixed by ensuring all functions and variables are correctly defined and linked. + + +4. Do you have to make any changes to MakefileWithDeps to build your hello world program? + +You may need to adjust MakefileWithDeps +if dependencies or file paths change for your hello world program. + + +5. In encode and decode, the type unsigned char is used. Would your code work the same +way if that type is changed to char or signed char? + +Changing unsigned char to char or signed char +may alter the behavior due to differences in handling negative values. + + +6. In the coding problem, reading the file with char ch; while (infile >> ch) ... doesn’t +work. Why? + +infile >> ch reads formatted input, skipping whitespace; +using infile.get(ch) reads every character, including whitespace. + + +7. If your program crashes, how can you use the debugger to get a stack trace similar to that +of Exception.printStackTrace() in Java? + +lldb ./progrma -> run -> bt + + +8. What is the difference between a debugger breakpoint and a watchpoint? + +A breakpoint pauses execution at a specific line, +while a watchpoint stops execution when a specific variable changes. \ No newline at end of file diff --git a/lab1/stream-examples/Makefile b/lab1/stream-examples/Makefile index 3221e68..7c657b9 100644 --- a/lab1/stream-examples/Makefile +++ b/lab1/stream-examples/Makefile @@ -25,6 +25,8 @@ PROGS = read-words example-out all: $(PROGS) +test: ./example-out ./read-words + # Phony targets .PHONY: all test clean distclean diff --git a/lab1/stream-examples/err.txt b/lab1/stream-examples/err.txt new file mode 100644 index 0000000..9476609 --- /dev/null +++ b/lab1/stream-examples/err.txt @@ -0,0 +1,2 @@ +And this is written to stderr +And some more to stderr diff --git a/lab1/stream-examples/example-out b/lab1/stream-examples/example-out new file mode 100755 index 0000000..b5bb6c8 Binary files /dev/null and b/lab1/stream-examples/example-out differ diff --git a/lab1/stream-examples/example-out.d b/lab1/stream-examples/example-out.d new file mode 100644 index 0000000..c912c8a --- /dev/null +++ b/lab1/stream-examples/example-out.d @@ -0,0 +1 @@ +example-out: example-out.cc diff --git a/lab1/stream-examples/out.txt b/lab1/stream-examples/out.txt new file mode 100644 index 0000000..2b53dce --- /dev/null +++ b/lab1/stream-examples/out.txt @@ -0,0 +1,2 @@ +This text is written to stdout +More text to stdout diff --git a/lab1/stream-examples/read-words b/lab1/stream-examples/read-words new file mode 100755 index 0000000..d281fe4 Binary files /dev/null and b/lab1/stream-examples/read-words differ diff --git a/lab1/stream-examples/read-words.d b/lab1/stream-examples/read-words.d new file mode 100644 index 0000000..176523b --- /dev/null +++ b/lab1/stream-examples/read-words.d @@ -0,0 +1 @@ +read-words: read-words.cc diff --git a/lab1/test_coding b/lab1/test_coding new file mode 100755 index 0000000..7e09ccb Binary files /dev/null and b/lab1/test_coding differ diff --git a/lab1/test_coding.d b/lab1/test_coding.d new file mode 100644 index 0000000..cbbbb42 --- /dev/null +++ b/lab1/test_coding.d @@ -0,0 +1,2 @@ +test_coding.o: test_coding.cc coding.h +coding.h: diff --git a/lab1/test_coding.o b/lab1/test_coding.o new file mode 100644 index 0000000..a588974 Binary files /dev/null and b/lab1/test_coding.o differ diff --git a/lab1/test_editor b/lab1/test_editor new file mode 100755 index 0000000..d141ad4 Binary files /dev/null and b/lab1/test_editor differ diff --git a/lab1/test_editor.d b/lab1/test_editor.d new file mode 100644 index 0000000..edfdcf1 --- /dev/null +++ b/lab1/test_editor.d @@ -0,0 +1,2 @@ +test_editor.o: test_editor.cc editor.h +editor.h: diff --git a/lab1/test_editor.o b/lab1/test_editor.o new file mode 100644 index 0000000..b367fc2 Binary files /dev/null and b/lab1/test_editor.o differ diff --git a/lab1/testfile.txt b/lab1/testfile.txt new file mode 100644 index 0000000..0032b0f --- /dev/null +++ b/lab1/testfile.txt @@ -0,0 +1 @@ +Hello my name is Douglas \ No newline at end of file diff --git a/lab1/testfile.txt.dec b/lab1/testfile.txt.dec new file mode 100644 index 0000000..b975562 --- /dev/null +++ b/lab1/testfile.txt.dec @@ -0,0 +1 @@ +Fcjjmkwl_kcgqBmsej_q \ No newline at end of file diff --git a/lab1/testfile.txt.enc b/lab1/testfile.txt.enc new file mode 100644 index 0000000..e7211eb --- /dev/null +++ b/lab1/testfile.txt.enc @@ -0,0 +1 @@ +Jgnnq"o{"pcog"ku"Fqwincu \ No newline at end of file diff --git a/lab1/testfile.txt.enc.dec b/lab1/testfile.txt.enc.dec new file mode 100644 index 0000000..0032b0f --- /dev/null +++ b/lab1/testfile.txt.enc.dec @@ -0,0 +1 @@ +Hello my name is Douglas \ No newline at end of file