lab1 done

This commit is contained in:
dDogge 2024-11-12 12:53:50 +01:00
parent 8b87ead3b9
commit 4eedfb85f5
56 changed files with 316 additions and 71 deletions

58
lab1/.vscode/settings.json vendored Normal file
View file

@ -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"
}
}

View file

@ -1,17 +1,58 @@
# The following rule means: "if test_editor does not exist, or # Define the compiler and the linker. The linker must be defined since
# is older than test_editor.o or editor.o, # the implicit rule for linking uses CC as the linker. g++ can be
# then link test_editor". # changed to clang++.
test_editor: test_editor.o editor.o CXX = g++
g++ -o test_editor test_editor.o editor.o CC = $(CXX)
# Rules to create the object files. # Generate dependencies in *.d files
test_editor.o: test_editor.cc editor.h DEPFLAGS = -MT $@ -MMD -MP -MF $*.d
g++ -c test_editor.cc -std=c++11
editor.o: editor.cc editor.h
g++ -c editor.cc -std=c++11
# 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 print_argv: print_argv.o
g++ -o print_argv print_argv.o test_editor: test_editor.o editor.o
print_argv.o: print_argv.cc test_coding: test_coding.o coding.o
g++ -c print_argv.cc -std=c++11 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)

17
lab1/Makefile1 Normal file
View file

@ -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

View file

@ -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)

View file

@ -21,20 +21,30 @@ PROGS=ub leak bounds bounds-heap dangling sum
ALL: $(PROGS) ALL: $(PROGS)
leak: leak.cc leak: leak.cc
$(CXX) $(CPPFLAGS) $(CXXFLAGS) $(LDFLAGS) -o $@ $<
dangling: dangling.cc dangling: dangling.cc
$(CXX) $(CPPFLAGS) $(CXXFLAGS) $(LDFLAGS) -o $@ $<
bounds: bounds.cc bounds: bounds.cc
$(CXX) $(CPPFLAGS) $(CXXFLAGS) $(LDFLAGS) -o $@ $<
ub: ub.cc ub: ub.cc
$(CXX) $(CPPFLAGS) $(CXXFLAGS) $(LDFLAGS) -o $@ $<
sum: sum.cc sum: sum.cc
$(CXX) $(CPPFLAGS) $(CXXFLAGS) $(LDFLAGS) -o $@ $<
# Targets
# Phony targets
.PHONY: all clean test .PHONY: all clean test
# Standard clean
clean: clean:
-rm $(PROGS) -rm -f $(PROGS)
-rm -r $(addsuffix .dSYM, $(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

BIN
lab1/buggy_programs/bounds Executable file

Binary file not shown.

BIN
lab1/buggy_programs/bounds-alt Executable file

Binary file not shown.

BIN
lab1/buggy_programs/bounds-heap Executable file

Binary file not shown.

Binary file not shown.

BIN
lab1/buggy_programs/dangling Executable file

Binary file not shown.

BIN
lab1/buggy_programs/leak Executable file

Binary file not shown.

BIN
lab1/buggy_programs/sum Executable file

Binary file not shown.

BIN
lab1/buggy_programs/sum-alt Executable file

Binary file not shown.

BIN
lab1/buggy_programs/ub Executable file

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View file

@ -2,9 +2,9 @@
unsigned char encode(unsigned char c) unsigned char encode(unsigned char c)
{ {
return c; return c + 2;
} }
unsigned char decode(unsigned char c) unsigned char decode(unsigned char c)
{ {
return c; return c - 2;
} }

2
lab1/coding.d Normal file
View file

@ -0,0 +1,2 @@
coding.o: coding.cc coding.h
coding.h:

BIN
lab1/coding.o Normal file

Binary file not shown.

BIN
lab1/decode Executable file

Binary file not shown.

34
lab1/decode.cc Normal file
View file

@ -0,0 +1,34 @@
#include <iostream>
#include <fstream>
#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<char&>(c))) {
outfile.put(decode(c));
}
std::cout << "Decoding complete. Output saved to " << filename << ".dec" << "\n";
return 0;
}

2
lab1/decode.d Normal file
View file

@ -0,0 +1,2 @@
decode.o: decode.cc coding.h
coding.h:

BIN
lab1/decode.o Normal file

Binary file not shown.

View file

@ -12,5 +12,23 @@ size_type Editor::get_size() const
} }
size_type Editor::find_left_par(size_type pos) 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;
}
}
}
} }

2
lab1/editor.d Normal file
View file

@ -0,0 +1,2 @@
editor.o: editor.cc editor.h
editor.h:

BIN
lab1/editor.o Normal file

Binary file not shown.

BIN
lab1/encode Executable file

Binary file not shown.

35
lab1/encode.cc Normal file
View file

@ -0,0 +1,35 @@
#include <iostream>
#include <fstream>
#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<char&>(c))) {
outfile.put(encode(c));
}
std::cout << "Encoding complete. Output saved to " << filename << "\n";
return 0;
}

2
lab1/encode.d Normal file
View file

@ -0,0 +1,2 @@
encode.o: encode.cc coding.h
coding.h:

BIN
lab1/encode.o Normal file

Binary file not shown.

BIN
lab1/hello Executable file

Binary file not shown.

7
lab1/hello.cc Normal file
View file

@ -0,0 +1,7 @@
#include <iostream>
int main() {
std::cout << "Hello, World!" << "\n";
std::cout << "Hello, World!" << "\n";
return 0;
}

1
lab1/hello.d Normal file
View file

@ -0,0 +1 @@
hello: hello.cc

BIN
lab1/print_argv Executable file

Binary file not shown.

1
lab1/print_argv.d Normal file
View file

@ -0,0 +1 @@
print_argv.o: print_argv.cc

BIN
lab1/print_argv.o Normal file

Binary file not shown.

49
lab1/reflektion.txt Normal file
View file

@ -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; its 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) ... doesnt
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.

View file

@ -25,6 +25,8 @@ PROGS = read-words example-out
all: $(PROGS) all: $(PROGS)
test: ./example-out ./read-words
# Phony targets # Phony targets
.PHONY: all test clean distclean .PHONY: all test clean distclean

View file

@ -0,0 +1,2 @@
And this is written to stderr
And some more to stderr

BIN
lab1/stream-examples/example-out Executable file

Binary file not shown.

View file

@ -0,0 +1 @@
example-out: example-out.cc

View file

@ -0,0 +1,2 @@
This text is written to stdout
More text to stdout

BIN
lab1/stream-examples/read-words Executable file

Binary file not shown.

View file

@ -0,0 +1 @@
read-words: read-words.cc

BIN
lab1/test_coding Executable file

Binary file not shown.

2
lab1/test_coding.d Normal file
View file

@ -0,0 +1,2 @@
test_coding.o: test_coding.cc coding.h
coding.h:

BIN
lab1/test_coding.o Normal file

Binary file not shown.

BIN
lab1/test_editor Executable file

Binary file not shown.

2
lab1/test_editor.d Normal file
View file

@ -0,0 +1,2 @@
test_editor.o: test_editor.cc editor.h
editor.h:

BIN
lab1/test_editor.o Normal file

Binary file not shown.

1
lab1/testfile.txt Normal file
View file

@ -0,0 +1 @@
Hello my name is Douglas

1
lab1/testfile.txt.dec Normal file
View file

@ -0,0 +1 @@
Fcjjmkwl_kcgqBmsej_q

1
lab1/testfile.txt.enc Normal file
View file

@ -0,0 +1 @@
Jgnnq"o{"pcog"ku"Fqwincu

View file

@ -0,0 +1 @@
Hello my name is Douglas