20,065 questions
2
votes
0
answers
77
views
Is this C++11 implementation of type erasure safe or UB?
I came up with a C++11 implementation of an std::any-like thing.
It works according to my testing, but I'm concerned that I might be invoking undefined behavior somewhere without realizing.
I'm ...
4
votes
3
answers
146
views
What does exempting (unsigned char *) data type from strict aliasing rule achieve?
I was trying to calculate square root of numbers without including math.h. I thought I could cast a double as a long as they both are 64 bits (in the function magnitude()) :
#define NUMBER -2
#include ...
1
vote
1
answer
118
views
Create a custom cast (that also accepts $null)
(Continuing from: Cast to custom class in PowerShell) I would like to build a custom cast like:
class CheckBox {
[Nullable[Bool]]$NullableBool
CheckBox([Nullable[Bool]]$NullableBool) { $this....
Best practices
0
votes
0
replies
81
views
Shall type-conversion be the responsibility of a function/method?
I got the following Android UI-code:
OutlinedTextField(value = mainVM.srcAmount.doubleValue.toString(),
onValueChange = { // Implicit generated argument 'it' is a String.
mainVM.setAmount(...
3
votes
4
answers
342
views
How can I take the raw bits of an int32 and create a float32 from them?
I am currently trying to generate a random float number in C.
And apparently that is a hard thing to do as there seems to be no real library for it, only workarounds with rand() from stdlib,
but those ...
1
vote
4
answers
174
views
Casting in comparator functions for qsort
I'm new to pointers and trying to wrap my head around how casting and dereferencing should function in comparator functions that qsort takes. My understanding is these always have the signature int ...
Advice
1
vote
4
replies
51
views
How to conditionally constrain Union types in Python?
Type Checking Challenge for AmiNode
I have an interesting challenge when parsing *.ami files.
I use the following recursive type, which has arbitrary hierarchical depth:
AmiName = NewType("...
1
vote
2
answers
179
views
C implicit const casting (in dereferencing order) should be permitted but isn't
For context, I have a function to print some text, which naturally takes in a const char *const * as one of its arguments (meaning the provided text will not be modified in any way by the function). ...
3
votes
2
answers
114
views
GDB typecasting failure in C
I was setting up gdb on vscode on windows and it wasn't working. I soon found out that it was erroring giving me a SIGSEGV segfault.
I tried gdb in powershell with the same problem.
I run the program ...
0
votes
1
answer
205
views
What is the practical reason to declare `const Uint32 x = ((Uint32)1 << 12)`, rather then simply `.. = 4096` or `.. = (Uint32)4096`?
This is a real example from 7z latest source code; it is in the file C/LzmaEnc.c, inside a function LzmaEncProps_Normalize(), at lines 70-110, namely, on line 86:
void LzmaEncProps_Normalize(...
1
vote
2
answers
167
views
How can I use two pointers to an interface to invoke the correct function that takes implementing classes as arguments?
I would like to know the proper way to get the following code to work.
With inheritance, you can call the function of a parent pointer and execute the child function. But, if there are two parent ...
2
votes
1
answer
167
views
How to assign a std::string from std::put_time in C++?
I need to set the result from std::put_time from ctime into a std::string, how do I do this ?
Here is my code:
BBToolkit::LogManager::LogManager() {
auto t = std::time(nullptr);
auto tm = *std:...
1
vote
2
answers
216
views
Getting address of non-static member function for locating its shared library
I have a situation similar to described in a blog post: a visible inline member function is called from several shared libraries which are built potentially with different compilers or compiler ...
2
votes
3
answers
261
views
Display contents of allocated memory byte by byte? [closed]
I'm trying to display the contents of some allocated memory byte by byte:
#include <stdio.h>
#include <stdlib.h>
typedef struct {
int len;
int* vec[];
} vec_t;
int main(void) {
int ...
1
vote
1
answer
162
views
Cause of AlignmentMismatch error while using bytemuck?
I'm facing an AlignmentMismatch error while trying to cast a Vec<u8> to Vec<T>, where T is a struct that uses repr(C) and Pod + Zeroable.
I've also tried the slice road &[u8] -> &...