diff options
| author | Charles Cabergs <me@cacharle.xyz> | 2020-12-14 11:18:40 +0100 |
|---|---|---|
| committer | Charles Cabergs <me@cacharle.xyz> | 2020-12-14 11:18:40 +0100 |
| commit | b13cd117a7957f9d86ec2986b47349979325a854 (patch) | |
| tree | fcdb302deb45f95bdd8495c5c03ea0e45ce77275 /cpp06/ex01/main.cpp | |
| parent | 2ee90625e793175c5fcde47564326a7eb8a1b3ee (diff) | |
| download | piscine_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/main.cpp')
| -rw-r--r-- | cpp06/ex01/main.cpp | 41 |
1 files changed, 10 insertions, 31 deletions
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; } |
