diff --git a/20230413/smart-pointers-01.cpp b/20230413/smart-pointers-01.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ab07005528a3039e9b43a44038d2070ae94d25fb --- /dev/null +++ b/20230413/smart-pointers-01.cpp @@ -0,0 +1,13 @@ +#include <iostream> +#include <string> + +using std::cout; +using std::endl; +using std::string; + +int main () +{ + string *hello = new string ("Hello, world!"); + cout << *hello << endl; + return 0; +} diff --git a/20230413/smart-pointers-02.cpp b/20230413/smart-pointers-02.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7fedf98b8e0474bee64563c39884d302cfe8cbb7 --- /dev/null +++ b/20230413/smart-pointers-02.cpp @@ -0,0 +1,15 @@ +#include <iostream> +#include <string> + +using std::cout; +using std::endl; +using std::string; + +int main () +{ + string *hello = new string ("Hello, world!"); + cout << *hello << endl; + delete hello; + cout << *hello << endl; + return 0; +} diff --git a/20230413/smart-pointers-03.cpp b/20230413/smart-pointers-03.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8c7313dcfe63fc28202ba6de4eac1a4c9297051e --- /dev/null +++ b/20230413/smart-pointers-03.cpp @@ -0,0 +1,18 @@ +#include <iostream> +#include <string> + +using std::cout; +using std::endl; +using std::string; + +int main () +{ + string *hello = new string ("Hello, world!"); + cout << *hello << endl; + delete hello; + if (hello) + cout << *hello << endl; + else + cout << "Variable wurde bereits freigegeben." << endl; + return 0; +} diff --git a/20230413/smart-pointers-04.cpp b/20230413/smart-pointers-04.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4184ff5e3862b702efe3fe4a1dc197cdc02b839e --- /dev/null +++ b/20230413/smart-pointers-04.cpp @@ -0,0 +1,21 @@ +#include <stdio.h> +#include <iostream> +#include <string> + +using std::cout; +using std::endl; +using std::string; + +int main () +{ + string *hello = new string ("Hello, world!"); + printf ("hello = 0x%016zx\n", (size_t) hello); + cout << *hello << endl; + delete hello; + printf ("hello = 0x%016zx\n", (size_t) hello); + if (hello) + cout << *hello << endl; + else + cout << "Variable wurde bereits freigegeben." << endl; + return 0; +} diff --git a/20230413/smart-pointers-05.cpp b/20230413/smart-pointers-05.cpp new file mode 100644 index 0000000000000000000000000000000000000000..188c175b6903e5b79cb43c2d7fd070b36b73eda7 --- /dev/null +++ b/20230413/smart-pointers-05.cpp @@ -0,0 +1,22 @@ +#include <stdio.h> +#include <iostream> +#include <string> + +using std::cout; +using std::endl; +using std::string; + +int main () +{ + string *hello = new string ("Hello, world!"); + printf ("hello = 0x%016zx\n", (size_t) hello); + cout << *hello << endl; + delete hello; + hello = NULL; + printf ("hello = 0x%016zx\n", (size_t) hello); + if (hello) + cout << *hello << endl; + else + cout << "Variable wurde bereits freigegeben." << endl; + return 0; +} diff --git a/20230413/smart-pointers-06.cpp b/20230413/smart-pointers-06.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e924ba3a32354ed5a106377701fa5a4d9d55379c --- /dev/null +++ b/20230413/smart-pointers-06.cpp @@ -0,0 +1,17 @@ +#include <stdio.h> +#include <iostream> +#include <string> + +using std::cout; +using std::endl; +using std::string; + +int main () +{ + string *hello; + if (hello) + cout << *hello << endl; + else + cout << "Kein gültiger Zeiger." << endl; + return 0; +} diff --git a/20230413/smart-pointers-07.cpp b/20230413/smart-pointers-07.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f70a5b327c1e05dc5e64413c1f31122f4e76c3dc --- /dev/null +++ b/20230413/smart-pointers-07.cpp @@ -0,0 +1,18 @@ +#include <iostream> +#include <string> +#include <memory> + +using std::cout; +using std::endl; +using std::string; +using std::shared_ptr; + +int main () +{ + shared_ptr <string> hello = new string ("Hello, world!"); + if (hello) + cout << *hello << endl; + else + cout << "Kein gültiger Zeiger." << endl; + return 0; +} diff --git a/20230413/smart-pointers-08.cpp b/20230413/smart-pointers-08.cpp new file mode 100644 index 0000000000000000000000000000000000000000..46bd15c76946227dadb6d267b3985f30bdbb4b92 --- /dev/null +++ b/20230413/smart-pointers-08.cpp @@ -0,0 +1,18 @@ +#include <iostream> +#include <string> +#include <memory> + +using std::cout; +using std::endl; +using std::string; +using std::shared_ptr; + +int main () +{ + shared_ptr <string> hello (new string ("Hello, world!")); + if (hello) + cout << *hello << endl; + else + cout << "Kein gültiger Zeiger." << endl; + return 0; +} diff --git a/20230413/smart-pointers-09.cpp b/20230413/smart-pointers-09.cpp new file mode 100644 index 0000000000000000000000000000000000000000..83b34a215c329db45dd03138ad0f2bdf81ce2f1d --- /dev/null +++ b/20230413/smart-pointers-09.cpp @@ -0,0 +1,18 @@ +#include <iostream> +#include <string> +#include <memory> + +using std::cout; +using std::endl; +using std::string; +using std::shared_ptr; + +int main () +{ + shared_ptr <string> hello ("Hello, world!"); + if (hello) + cout << *hello << endl; + else + cout << "Kein gültiger Zeiger." << endl; + return 0; +} diff --git a/20230413/smart-pointers-10.cpp b/20230413/smart-pointers-10.cpp new file mode 100644 index 0000000000000000000000000000000000000000..33ef969fdaf5026bbaaff1bc7c474e3547536eb9 --- /dev/null +++ b/20230413/smart-pointers-10.cpp @@ -0,0 +1,25 @@ +#include <iostream> +#include <string> +#include <memory> + +using std::cout; +using std::endl; +using std::string; +using std::shared_ptr; + +int main () +{ + shared_ptr <string> hello2; + if (hello2) + cout << *hello2 << endl; + else + cout << "(String noch nicht vorhanden)" << endl; + if (1) + { + shared_ptr <string> hello (new string ("Hello, world!")); + cout << *hello << endl; + hello2 = hello; + } + cout << *hello2 << endl; + return 0; +} diff --git a/20230413/smart-pointers-11.cpp b/20230413/smart-pointers-11.cpp new file mode 100644 index 0000000000000000000000000000000000000000..803168363150d322ae248e70d15ffc955c54a332 --- /dev/null +++ b/20230413/smart-pointers-11.cpp @@ -0,0 +1,26 @@ +#include <iostream> +#include <string> +#include <memory> + +using std::cout; +using std::endl; +using std::string; +using std::shared_ptr; + +struct container +{ + string content; + container (const char *s) + { + content = s; + } + shared_ptr <container> next; +}; + +int main () +{ + shared_ptr <container> hello1 (new container ("Hello,")); + shared_ptr <container> hello2 (new container ("world!")); + cout << hello1->content << " " << hello2->content << endl; + return 0; +} diff --git a/20230413/smart-pointers-12.cpp b/20230413/smart-pointers-12.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b80cff7570ae19f6d3b843480afaad4fbd1b7f63 --- /dev/null +++ b/20230413/smart-pointers-12.cpp @@ -0,0 +1,28 @@ +#include <iostream> +#include <string> +#include <memory> + +using std::cout; +using std::endl; +using std::string; +using std::shared_ptr; + +struct container +{ + string content; + container (const char *s) + { + content = s; + } + shared_ptr <container> next; +}; + +int main () +{ + shared_ptr <container> hello1 (new container ("Hello,")); + shared_ptr <container> hello2 (new container ("world!")); + hello1->next = hello2; + hello2->next = hello1; + cout << hello1->content << " " << hello2->content << endl; + return 0; +} diff --git a/20230413/smart-pointers-13.cpp b/20230413/smart-pointers-13.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f044de4d01b6ee14cdec6fae8ffbfc105f9f7be1 --- /dev/null +++ b/20230413/smart-pointers-13.cpp @@ -0,0 +1,32 @@ +#include <iostream> +#include <string> +#include <memory> + +using std::cout; +using std::endl; +using std::string; +using std::shared_ptr; + +struct container +{ + string content; + container (const char *s) + { + content = s; + } + ~container () + { + cout << "Bye: " << content << endl; + } + shared_ptr <container> next; +}; + +int main () +{ + shared_ptr <container> hello1 (new container ("Hello,")); + shared_ptr <container> hello2 (new container ("world!")); +// hello1->next = hello2; +// hello2->next = hello1; + cout << hello1->content << " " << hello2->content << endl; + return 0; +} diff --git a/20230413/smart-pointers-14.cpp b/20230413/smart-pointers-14.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8d11a2d3f7d5b3af210692aa7a82ac207d648430 --- /dev/null +++ b/20230413/smart-pointers-14.cpp @@ -0,0 +1,32 @@ +#include <iostream> +#include <string> +#include <memory> + +using std::cout; +using std::endl; +using std::string; +using std::shared_ptr; + +struct container +{ + string content; + container (const char *s) + { + content = s; + } + ~container () + { + cout << "Bye: " << content << endl; + } + shared_ptr <container> next; +}; + +int main () +{ + shared_ptr <container> hello1 (new container ("Hello,")); + shared_ptr <container> hello2 (new container ("world!")); + hello1->next = hello2; + hello2->next = hello1; + cout << hello1->content << " " << hello2->content << endl; + return 0; +} diff --git a/20230413/smart-pointers-15.cpp b/20230413/smart-pointers-15.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8a91989ed071820ff6163078837acdeae53626f7 --- /dev/null +++ b/20230413/smart-pointers-15.cpp @@ -0,0 +1,33 @@ +#include <iostream> +#include <string> +#include <memory> + +using std::cout; +using std::endl; +using std::string; +using std::shared_ptr; +using std::weak_ptr; + +struct container +{ + string content; + container (const char *s) + { + content = s; + } + ~container () + { + cout << "Bye: " << content << endl; + } + weak_ptr <container> next; +}; + +int main () +{ + shared_ptr <container> hello1 (new container ("Hello,")); + shared_ptr <container> hello2 (new container ("world!")); + hello1->next = hello2; + hello2->next = hello1; + cout << hello1->content << " " << hello2->content << endl; + return 0; +} diff --git a/20230413/smart-pointers-16.cpp b/20230413/smart-pointers-16.cpp new file mode 100644 index 0000000000000000000000000000000000000000..dcf942e1ba0baf0b4784d4d790a224bfed24753d --- /dev/null +++ b/20230413/smart-pointers-16.cpp @@ -0,0 +1,18 @@ +#include <iostream> +#include <string> +#include <memory> + +using std::cout; +using std::endl; +using std::string; +using std::shared_ptr; +using std::weak_ptr; + +int main () +{ + weak_ptr <string> p; + shared_ptr <string> hello (new string ("Hello, world!")); + p = hello; + cout << *p << endl; + return 0; +} diff --git a/20230413/smart-pointers-17.cpp b/20230413/smart-pointers-17.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f9d845c11f6d20f1ab81f2b032637142a8af1a0a --- /dev/null +++ b/20230413/smart-pointers-17.cpp @@ -0,0 +1,19 @@ +#include <iostream> +#include <string> +#include <memory> + +using std::cout; +using std::endl; +using std::string; +using std::shared_ptr; +using std::weak_ptr; + +int main () +{ + weak_ptr <string> p; + shared_ptr <string> hello (new string ("Hello, world!")); + p = hello; + shared_ptr <string> q = p.lock (); + cout << *q << endl; + return 0; +} diff --git a/20230413/smart-pointers-18.cpp b/20230413/smart-pointers-18.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d9f3327c183db5f3fa7684aaf86f7ea4605f2020 --- /dev/null +++ b/20230413/smart-pointers-18.cpp @@ -0,0 +1,16 @@ +#include <iostream> +#include <string> + +using std::cout; +using std::endl; +using std::string; + +int main () +{ + string *p; + string *hello = new string ("Hello, world!"); + p = hello; + string *q = p; + cout << *q << endl; + return 0; +} diff --git a/20230413/smart-pointers-19.cpp b/20230413/smart-pointers-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..27c4e636d72163088293d3106bf2b28a502bfc19 --- /dev/null +++ b/20230413/smart-pointers-19.cpp @@ -0,0 +1,22 @@ +#include <iostream> +#include <string> +#include <memory> + +using std::cout; +using std::endl; +using std::string; +using std::shared_ptr; +using std::weak_ptr; + +int main () +{ + weak_ptr <string> p; + shared_ptr <string> hello (new string ("Hello, world!")); + p = hello; + shared_ptr <string> q = p.lock (); + if (q) + cout << *q << endl; + else + cout << "Zeiger wurde bereits freigegeben." << endl; + return 0; +} diff --git a/20230413/smart-pointers-20.cpp b/20230413/smart-pointers-20.cpp new file mode 100644 index 0000000000000000000000000000000000000000..50e95cf6374133061ec4534acb17b3c2a2c3a71e --- /dev/null +++ b/20230413/smart-pointers-20.cpp @@ -0,0 +1,25 @@ +#include <iostream> +#include <string> +#include <memory> + +using std::cout; +using std::endl; +using std::string; +using std::shared_ptr; +using std::weak_ptr; + +int main () +{ + weak_ptr <string> p; + if (1) + { + shared_ptr <string> hello (new string ("Hello, world!")); + p = hello; + } + shared_ptr <string> q = p.lock (); + if (q) + cout << *q << endl; + else + cout << "Zeiger wurde bereits freigegeben." << endl; + return 0; +} diff --git a/20230413/smart-pointers-21.cpp b/20230413/smart-pointers-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5699eaedb0e8be001d28c5a2e8965c652653f059 --- /dev/null +++ b/20230413/smart-pointers-21.cpp @@ -0,0 +1,26 @@ +#include <iostream> +#include <string> +#include <memory> + +using std::cout; +using std::endl; +using std::string; +using std::shared_ptr; +using std::weak_ptr; + +int main () +{ + weak_ptr <string> p; + shared_ptr <string> q; + if (1) + { + shared_ptr <string> hello (new string ("Hello, world!")); + p = hello; + q = p.lock (); + } + if (q) + cout << *q << endl; + else + cout << "Zeiger wurde bereits freigegeben." << endl; + return 0; +} diff --git a/20230413/smart-pointers-22.cpp b/20230413/smart-pointers-22.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c8688a6fbc492b0fd16b1bb69d4672adf0dc9881 --- /dev/null +++ b/20230413/smart-pointers-22.cpp @@ -0,0 +1,19 @@ +#include <iostream> +#include <string> + +using std::cout; +using std::endl; +using std::string; + +int main () +{ + string *hello = new string ("Hello, world!"); + string *storage = NULL; + + storage = hello; + hello = NULL; + hello = new string ("Hallo, Welt!"); + + cout << *storage << endl; + return 0; +} diff --git a/20230413/smart-pointers-23.cpp b/20230413/smart-pointers-23.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a9f8fd8bdae9c742bad60b5012a78a6e7c3c1628 --- /dev/null +++ b/20230413/smart-pointers-23.cpp @@ -0,0 +1,24 @@ +#include <iostream> +#include <string> + +using std::cout; +using std::endl; +using std::string; + +int main () +{ + string *hello = new string ("Hello, world!"); + string *storage = NULL; + + storage = hello; + hello = NULL; + + cout << *storage << endl; + + if (hello) + delete hello; + if (storage) + delete storage; + + return 0; +} diff --git a/20230413/smart-pointers-24.cpp b/20230413/smart-pointers-24.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a3000f8f99fef966c9c692e5d06b7f2cdda38edf --- /dev/null +++ b/20230413/smart-pointers-24.cpp @@ -0,0 +1,24 @@ +#include <iostream> +#include <string> + +using std::cout; +using std::endl; +using std::string; + +int main () +{ + string *hello = new string ("Hello, world!"); + string *storage = NULL; + + storage = hello; +// hello = NULL; + + cout << *storage << endl; + + if (hello) + delete hello; + if (storage) + delete storage; + + return 0; +} diff --git a/20230413/smart-pointers-25.cpp b/20230413/smart-pointers-25.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5ade0eb09b9d181ca4b0df5d292617e1f150ba7e --- /dev/null +++ b/20230413/smart-pointers-25.cpp @@ -0,0 +1,30 @@ +#include <iostream> +#include <string> + +using std::cout; +using std::endl; +using std::string; + +int main () +{ + string *hello = new string ("Hello, world!"); + string *storage = NULL; + + storage = hello; + + // Jetzt kommt ein anderer Thread und macht: + + if (hello) + delete hello; + + // Jetzt sind wir wieder im ursprünglichen Thread. + + hello = NULL; + + cout << *storage << endl; + + if (storage) + delete storage; + + return 0; +} diff --git a/20230413/smart-pointers-26.cpp b/20230413/smart-pointers-26.cpp new file mode 100644 index 0000000000000000000000000000000000000000..db13d3667419c0c014843f6d6ef9a9b90be42d00 --- /dev/null +++ b/20230413/smart-pointers-26.cpp @@ -0,0 +1,32 @@ +#include <iostream> +#include <string> +#include <memory> + +using std::cout; +using std::endl; +using std::string; +using std::unique_ptr; + +int main () +{ + unique_ptr <string> hello (new string ("Hello, world!")); + unique_ptr <string> storage; + + storage = hello; + + // Jetzt kommt ein anderer Thread und macht: + + if (hello) + delete hello; + + // Jetzt sind wir wieder im ursprünglichen Thread. + + hello = NULL; + + cout << *storage << endl; + + if (storage) + delete storage; + + return 0; +} diff --git a/20230413/smart-pointers-27.cpp b/20230413/smart-pointers-27.cpp new file mode 100644 index 0000000000000000000000000000000000000000..85d4d7d22ec5da6bbb0d1939b6be4ae1c22a13d9 --- /dev/null +++ b/20230413/smart-pointers-27.cpp @@ -0,0 +1,30 @@ +#include <iostream> +#include <string> +#include <memory> + +using std::cout; +using std::endl; +using std::string; +using std::unique_ptr; + +int main () +{ + unique_ptr <string> hello (new string ("Hello, world!")); + unique_ptr <string> storage; + + storage = move (hello); // setzt gleichzeitig auf NULL + + // Jetzt kommt ein anderer Thread und macht: + + if (hello) + delete hello; + + // Jetzt sind wir wieder im ursprünglichen Thread. + + cout << *storage << endl; + + if (storage) + delete storage; + + return 0; +} diff --git a/20230413/smart-pointers-28.cpp b/20230413/smart-pointers-28.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4abf8d4ad82af564cef0d90152f19c340a850525 --- /dev/null +++ b/20230413/smart-pointers-28.cpp @@ -0,0 +1,26 @@ +#include <iostream> +#include <string> +#include <memory> + +using std::cout; +using std::endl; +using std::string; +using std::unique_ptr; + +int main () +{ + unique_ptr <string> hello (new string ("Hello, world!")); + unique_ptr <string> storage; + + storage = move (hello); // setzt gleichzeitig auf NULL + + // Ein anderer Thread hat keine Chance mehr, sich zwischen die Zeiger-Zuweisung + // und das Auf-NULL-Setzen zu schieben. + + cout << *storage << endl; + + // if (storage) + // delete storage; --> geschieht automatisch + + return 0; +} diff --git a/20230413/variables-01.c b/20230413/variables-01.c new file mode 100644 index 0000000000000000000000000000000000000000..7e1ee2d66160e307129048f02e27cd169972e0c1 --- /dev/null +++ b/20230413/variables-01.c @@ -0,0 +1,13 @@ +#include <stdio.h> + +void foo (void) +{ + int a = 42; + printf ("%d\n", a); +} + +int main (void) +{ + foo (); + return 0; +} diff --git a/20230413/variables-01.s b/20230413/variables-01.s new file mode 100644 index 0000000000000000000000000000000000000000..cd658d68d9d583e96db336af0fa62b70a5868afd --- /dev/null +++ b/20230413/variables-01.s @@ -0,0 +1,50 @@ + .file "variables-01.c" + .text + .section .rodata +.LC0: + .string "%d\n" + .text + .globl foo + .type foo, @function +foo: +.LFB0: + .cfi_startproc + pushq %rbp + .cfi_def_cfa_offset 16 + .cfi_offset 6, -16 + movq %rsp, %rbp + .cfi_def_cfa_register 6 + subq $16, %rsp + movl $42, -4(%rbp) + movl -4(%rbp), %eax + movl %eax, %esi + leaq .LC0(%rip), %rdi + movl $0, %eax + call printf@PLT + nop + leave + .cfi_def_cfa 7, 8 + ret + .cfi_endproc +.LFE0: + .size foo, .-foo + .globl main + .type main, @function +main: +.LFB1: + .cfi_startproc + pushq %rbp + .cfi_def_cfa_offset 16 + .cfi_offset 6, -16 + movq %rsp, %rbp + .cfi_def_cfa_register 6 + call foo + movl $0, %eax + popq %rbp + .cfi_def_cfa 7, 8 + ret + .cfi_endproc +.LFE1: + .size main, .-main + .ident "GCC: (Debian 8.3.0-6) 8.3.0" + .section .note.GNU-stack,"",@progbits