summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorOskar <[email protected]>2024-08-21 20:55:10 +0200
committerOskar <[email protected]>2024-08-21 20:55:10 +0200
commit4c8377e5711e2c864c8754d1d6373c09da2ce7e1 (patch)
treed3c8703a39cb8bd2f1a3b22c868141f8eba35e95
parente747f69eb9b34f2c99472ca1cd17e049c1b74c9c (diff)
more exercises
-rw-r--r--.gitignore1
-rw-r--r--4p28.cpp33
-rw-r--r--4p29.cpp20
-rw-r--r--4p30.cpp32
-rw-r--r--sizeof-tests.cpp59
5 files changed, 145 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore
index 59e6131..c175108 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,3 +1,4 @@
bin/
redirection/
TODO
+gede.ini
diff --git a/4p28.cpp b/4p28.cpp
new file mode 100644
index 0000000..0f519e1
--- /dev/null
+++ b/4p28.cpp
@@ -0,0 +1,33 @@
+#include <iostream>
+
+/*
+ *
+ * 4.28
+ *
+ *
+ */
+
+int main () {
+
+ std::cout << "void " << sizeof(void*) << std::endl;
+ std::cout << "nullptr_t " << sizeof(std::nullptr_t) << std::endl;
+ std::cout << "bool " << sizeof(bool) << std::endl;
+ std::cout << "char " << sizeof(char) << std::endl;
+ std::cout << "uchar " << sizeof(unsigned char) << std::endl;
+ std::cout << "schar " << sizeof(signed char) << std::endl;
+ std::cout << "si " << sizeof(short int) << std::endl;
+ std::cout << "i " << sizeof(int) << std::endl;
+ std::cout << "li " << sizeof(long int) << std::endl;
+ std::cout << "lli " << sizeof(long long int) << std::endl;
+ std::cout << "usi " << sizeof(unsigned short int) << std::endl;
+ std::cout << "ui " << sizeof(unsigned int) << std::endl;
+ std::cout << "uli " << sizeof(unsigned long int) << std::endl;
+ std::cout << "ulli " << sizeof(unsigned long long int) << std::endl;
+ std::cout << "wchar " << sizeof(wchar_t) << std::endl;
+ std::cout << "char16 " << sizeof(char16_t) << std::endl;
+ std::cout << "char32 " << sizeof(char32_t) << std::endl;
+ std::cout << "float " << sizeof(float) << std::endl;
+ std::cout << "double " << sizeof(double) << std::endl;
+ std::cout << "long double " << sizeof(long double) << std::endl;
+ return 0;
+}
diff --git a/4p29.cpp b/4p29.cpp
new file mode 100644
index 0000000..44a0245
--- /dev/null
+++ b/4p29.cpp
@@ -0,0 +1,20 @@
+#include <iostream>
+
+/*
+ *
+ * 4.29
+ *
+ *
+ */
+
+int main () {
+
+ int x[10];
+ int *p = x;
+ auto xx = sizeof(x) / sizeof(*x);
+ auto pp1 = sizeof(p);
+ auto pp2 = sizeof(*p);
+ std::cout << xx << std::endl; // int is 32bit on my machine therefore 40 / 4 = 10
+ std::cout << pp1 / pp2 << std::endl; // we get the size of a pointer(8) then the size of an int(4) = 2
+ return 0;
+}
diff --git a/4p30.cpp b/4p30.cpp
new file mode 100644
index 0000000..8a93455
--- /dev/null
+++ b/4p30.cpp
@@ -0,0 +1,32 @@
+#include <iostream>
+
+/*
+ *
+ * 4.30
+ *
+ * Wow! This was a tricky question.
+ */
+
+int f() {
+ return 1;
+}
+
+struct ddd {
+ int mem[10];
+};
+
+int main () {
+
+ struct ddd ggg;
+ struct ddd *p = &ggg;
+ int i = 0;
+ int x = 0;
+ int y = 0;
+ unsigned int a = 0;
+ unsigned int b = 0;
+ std::cout << sizeof (x) + y << std::endl;
+ std::cout << sizeof (p->mem[i]) << std::endl;
+ std::cout << (sizeof (a) < b) << std::endl;
+ std::cout << sizeof (f()) << std::endl;
+ return 0;
+}
diff --git a/sizeof-tests.cpp b/sizeof-tests.cpp
new file mode 100644
index 0000000..96a8323
--- /dev/null
+++ b/sizeof-tests.cpp
@@ -0,0 +1,59 @@
+#include <iostream>
+#include <vector>
+
+/*
+ *
+ *
+ *
+ *
+ */
+
+int main () {
+
+ std::vector<int> iv1;
+ std::vector<int> iv2 = {1,2,3,4,5,6,7,8,9,10};
+ std::vector<std::string> sv1;
+ std::vector<std::string> sv2;
+ std::string ss0;
+ std::string ss1 = "Hello, World!";
+ std::string ss2 = "Hello, World!";
+ std::string ss3 = "Hello, World!";
+ std::string ss4 = "Hello, World!";
+ std::string ss5 = "Hello, World!";
+ sv2.push_back(ss1);
+ sv2.push_back(ss2);
+ sv2.push_back(ss3);
+ sv2.push_back(ss4);
+ sv2.push_back(ss5);
+ char *p = nullptr;
+ char aa[10];
+ std::cout << "ss0 " << sizeof(ss0) << std::endl;
+ std::cout << "ss1 " << sizeof(ss1) << std::endl;
+ std::cout << "sv1 " << sizeof(sv1) << std::endl;
+ std::cout << "sv2 " << sizeof(sv2[0]) << std::endl;
+ std::cout << "iv1 " << sizeof(iv1) << std::endl;
+ std::cout << "iv2[0] " << sizeof(iv2[0]) << std::endl;
+ std::cout << "char " << sizeof(char) << std::endl;
+ std::cout << "int " << sizeof(int) << std::endl;
+ std::cout << "int32_t " << sizeof(int32_t) << std::endl;
+ std::cout << "long " << sizeof(long) << std::endl;
+ std::cout << "long long " << sizeof(long long) << std::endl;
+ std::cout << "int64_t " << sizeof(int64_t) << std::endl;
+ std::cout << "uint64_t " << sizeof(uint64_t) << std::endl;
+ std::cout << "*aa " << sizeof(*aa) << std::endl;
+ std::cout << "aa " << sizeof(aa) << std::endl;
+ std::cout << "*p " << sizeof(*p) << std::endl;
+ std::cout << "p " << sizeof(p) << std::endl;
+ int iii[100];
+ int *ip = iii;
+ std::cout << "iii " << sizeof(iii) << std::endl;
+ std::cout << "ip " << sizeof(ip) << std::endl;
+ std::cout << "*ip " << sizeof(*ip) << std::endl;
+ std::cout << "iii[0] " << sizeof(iii[0]) << std::endl;
+ std::cout << "&iii " << &iii << std::endl;
+ std::cout << "&iii[0] " << &iii[0] << std::endl;
+ std::cout << "ip " << ip << std::endl;
+ std::cout << "&(*ip) " << &(*ip) << std::endl;
+ std::cout << "&ip[0] " << &ip[0] << std::endl;
+ return 0;
+}