Given long double
, I need to serialize it to char[12]
(optimally) and deserialize.
Is it even possible?
Asked
Active
Viewed 208 times
1

phuclv
- 37,963
- 15
- 156
- 475

Commander Tvis
- 2,244
- 2
- 15
- 41
-
1Presumably sizeof(long double) is 12 on your platform? And you need to assume that both "sender" and "receiver" use the same scheme for representing the long double. – Bathsheba Apr 14 '20 at 13:35
-
@Bathsheba I am sure it is 12 bits, because it is both stored and retrieved on one PC – Commander Tvis Apr 14 '20 at 13:43
-
1You mean bytes. It would not be a very useful system with 12 bit long double. – drescherjm Apr 14 '20 at 13:44
-
sizeof long double is 16 using 64-bit gcc 4.8.5, so no. – stark Apr 14 '20 at 13:52
-
@stark What's that "no" for? – Ted Lyngmo Apr 14 '20 at 14:06
-
@stark it is 12 for my platform – Commander Tvis Apr 14 '20 at 16:06
-
1Looks like on a 64-bit system it is 10 bytes of precision (80-bit format) and 6 bytes of padding to align. – stark Apr 14 '20 at 16:08
-
@stark but among that 16 bytes there are 4 padding bytes, only 12 bytes contain the actual value (unless you're using [`-mlong-double-64/128`](https://gcc.gnu.org/onlinedocs/gcc/x86-Options.html) to change the `long double`'s format. You can easily check that with `std::numeric_limits
::digits` or `LDBL_MANT_DIG` – phuclv Jul 23 '22 at 03:44
2 Answers
2
From what I understand of your question, I would do:
double in=2.132;
char arr[12] = {};
memcpy(arr,&in,sizeof(in));
char arr2[12] = ...;
double out;
memcpy(&out,arr2,sizeof(out));
assuming endianness and size of double are the same on both sides of the de/serialization.

Ted Lyngmo
- 93,841
- 5
- 60
- 108

log0
- 10,489
- 4
- 28
- 62
-
It is both stored and retrieved on one PC and I assert that `sizeof(long double)` = 12 – Commander Tvis Apr 14 '20 at 13:44
-
1ok then should be good. Could possibly be done by casting then. since there is no alignment and/or endianess issues. – log0 Apr 14 '20 at 13:45
-
1The answer that @log0 gave you should work. You can also use [sprintf](https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/sprintf-sprintf-l-swprintf-swprintf-l-swprintf-l?view=vs-2019) and [sscanf](https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/sscanf-sscanf-l-swscanf-swscanf-l?view=vs-2019) to convert it to readable text, but you will loose some accuracy. – Sam Apr 14 '20 at 13:52
-2
I would advise you to stay away from memcpy as it might result in memory overlapping as stated below.
Link http://www.cplusplus.com/reference/cstring/memcpy/
void * memcpy ( void * destination, const void * source, size_t num );
Copy block of memory
The function does not check for any terminating null character in source -
it always copies exactly num bytes.
To avoid overflows, the size of the arrays pointed to by both the
destination and source parameters, shall be at least num bytes,
and should not overlap (for overlapping memory blocks, memmove is a safer approach).
You can follow a different approach as C library particularly has functions for these kind of similar requirements as shown below.
I guess this is what you want to achieve.
int main(){
long double var1 = 123.23;
char str1[12];
memset(str1, 0x00, sizeof(str1));
sprintf(str1, "%-12.12Lf", var1);
printf(" Value : %-12.12s\n", str1);
long double var2 = 0;
sscanf(str1, "%12Lf", &var2);
printf(" Value : %Lf\n", var2);
return 0;
}
NOTE: Here you can adjust for accuracy too.

D.Malim
- 86
- 6
-
1"_it might result in memory overlapping_" - There's absolutely no risk of memory overlapping just happening. In OP:s case, `memcpy` is perfectly secure. – Ted Lyngmo Apr 14 '20 at 14:19
-
It just that in this case it might not as long as you manage it properly. If you try to run in any code analysis tool memcpy comes first highlighted for same reasons. Any way its upto the programmers preference. – D.Malim Apr 14 '20 at 14:27
-
I ran it in a code analysis tool and it didn't complain about the use of `memcpy` for this case. I'd turn that off if it showed warnings when the code is safe. Using `memcpy` to/from a `char[]` is btw the prefered to do binary serialization which is what OP seems to ask about. – Ted Lyngmo Apr 14 '20 at 14:49
-
Well it depends upon the tool you are using we got a report highlights for all memcpy usage in code while using a tool called "checkmarx" .Like I said its the way you use it .We were working on large volume of data where we had contagious memory segments for holding structured object . Now memcpy work fine here no problem but if you aren't too careful it will corrupt your data by overlapping. Only reason I suggested otherwise. – D.Malim Apr 14 '20 at 15:03
-
cplusplus.com is absolutely not reliable, there have been lots of incorrect information on it. And `memcpy` is the only reliable way here. Serialization with fixed precision like that will fail miserably – phuclv Jul 23 '22 at 03:38