aboutsummaryrefslogtreecommitdiff
path: root/cpp06/ex01
diff options
context:
space:
mode:
authorCharles Cabergs <me@cacharle.xyz>2020-12-14 11:18:40 +0100
committerCharles Cabergs <me@cacharle.xyz>2020-12-14 11:18:40 +0100
commitb13cd117a7957f9d86ec2986b47349979325a854 (patch)
treefcdb302deb45f95bdd8495c5c03ea0e45ce77275 /cpp06/ex01
parent2ee90625e793175c5fcde47564326a7eb8a1b3ee (diff)
downloadpiscine_cpp-b13cd117a7957f9d86ec2986b47349979325a854.tar.gz
piscine_cpp-b13cd117a7957f9d86ec2986b47349979325a854.tar.bz2
piscine_cpp-b13cd117a7957f9d86ec2986b47349979325a854.zip
Fixing nanf not recognized in cpp06/00, Fixing serialization size to 52 bytes
Diffstat (limited to 'cpp06/ex01')
-rw-r--r--cpp06/ex01/Makefile4
-rw-r--r--cpp06/ex01/main.cpp41
-rw-r--r--cpp06/ex01/serialization.hpp11
3 files changed, 14 insertions, 42 deletions
diff --git a/cpp06/ex01/Makefile b/cpp06/ex01/Makefile
index fff9a9f..8702cc9 100644
--- a/cpp06/ex01/Makefile
+++ b/cpp06/ex01/Makefile
@@ -6,14 +6,14 @@
# By: charles <me@cacharle.xyz> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2020/11/18 09:31:16 by charles #+# #+# #
-# Updated: 2020/11/18 10:17:03 by charles ### ########.fr #
+# Updated: 2020/12/14 11:11:02 by cacharle ### ########.fr #
# #
# ############################################################################ #
NAME = serialization
CXX = clang++
-CXXFLAGS = -std=c++98 -Wall -Wextra -Werror
+CXXFLAGS = -Wall -Wextra -Werror
SRC = main.cpp
OBJ = $(SRC:.cpp=.o)
diff --git a/cpp06/ex01/main.cpp b/cpp06/ex01/main.cpp
index 3786106..616935d 100644
--- a/cpp06/ex01/main.cpp
+++ b/cpp06/ex01/main.cpp
@@ -6,7 +6,7 @@
/* By: charles <me@cacharle.xyz> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/11/18 09:32:02 by charles #+# #+# */
-/* Updated: 2020/11/18 10:22:26 by charles ### ########.fr */
+/* Updated: 2020/12/14 11:14:11 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
@@ -20,23 +20,9 @@
#include "serialization.hpp"
-void generate_chars(char c[8])
-{
- char choices[] =
- {
- 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
- 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
- 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
- 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
- '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'
- };
- for (int i = 0; i < 8; i++)
- c[i] = choices[rand() % (sizeof(choices) / sizeof(char))];
-}
-
void* serialize(void)
{
- RawData *d = new RawData;
+ Data *data = new Data;
static const char choices[] =
{
@@ -47,26 +33,18 @@ void* serialize(void)
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'
};
size_t choices_len = sizeof(choices) / sizeof(char);
- for (int i = 0; i < 8; i++)
+ for (size_t i = 0; i < 8; i++)
{
- d->c1[i] = choices[rand() % choices_len];
- d->c2[i] = choices[rand() % choices_len];
+ data->s1.push_back(choices[rand() % choices_len]);
+ data->s2.push_back(choices[rand() % choices_len]);
}
- d->n = rand() % INT_MAX;
- return reinterpret_cast<void*>(d);
+ data->n = rand() % INT_MAX;
+ return reinterpret_cast<void*>(data);
}
Data* deserialize(void* raw)
{
- RawData *raw_data = reinterpret_cast<RawData*>(raw);
- Data *data = new Data;
-
- char tmp[9] = {'\0'};
- memcpy(tmp, raw_data->c1, 8 * sizeof(char));
- data->s1 = tmp;
- memcpy(tmp, raw_data->c2, 8 * sizeof(char));
- data->s2 = tmp;
- data->n = raw_data->n;
+ Data *data = reinterpret_cast<Data*>(raw);
return data;
}
@@ -93,8 +71,9 @@ int main()
<< ", n: " << std::setw(10) << data->n
<< ", s2: " << data->s2
<< std::endl;
- delete reinterpret_cast<RawData*>(raw);
delete data;
}
+
+ std::cout << "sizeof(Data) = " << sizeof(Data) << std::endl;
return 0;
}
diff --git a/cpp06/ex01/serialization.hpp b/cpp06/ex01/serialization.hpp
index 36a5434..a04d476 100644
--- a/cpp06/ex01/serialization.hpp
+++ b/cpp06/ex01/serialization.hpp
@@ -6,7 +6,7 @@
/* By: charles <me@cacharle.xyz> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/11/18 10:22:17 by charles #+# #+# */
-/* Updated: 2020/11/18 10:22:35 by charles ### ########.fr */
+/* Updated: 2020/12/14 11:10:42 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
@@ -15,18 +15,11 @@
# include <string>
-struct RawData
-{
- char c1[8];
- int n;
- char c2[8];
-};
-
struct Data
{
std::string s1;
int n;
std::string s2;
-};
+} __attribute__((packed));
#endif