43 questions
1
vote
3
answers
189
views
What is the correct type of cast to use for non-typesafe pointer conversions for binary data reinterpret operations?
What is the correct form of a type-cast which should be used for non-typesafe binary data operations involving pointer casts which cause data to be reinterpreted as a different type?
For example, if I ...
5
votes
3
answers
392
views
Is it possible to have a 32-bit pointer on x86-64 without undefined behavior?
Generally pointers on x86-64 are defined to be 8 bytes. However, if you are certain that you have data that will only ever be in the first 4GB of the address space, then a 32-bit value is technically ...
3
votes
2
answers
200
views
Interpreting part of an array as an object by casting a pointer to an array element
Say you have an array of floats representing raw data, and several types representing 2D shapes that only have floats as members, like this:
#include <iostream>
#include <array>
struct ...
4
votes
1
answer
80
views
Is casting `the address of a pointer to a struct` to `the address of a struct whos first member is a pointer to the struct` UB?
I have a struct called Node whose first member is Node* p_next
I have a pointer to the first node called p_head
I want to cast &p_head which is of type Node** to a Node*, and store it in ...
1
vote
0
answers
80
views
How to assign a reader to TFDBatchMove::Reader in runtime?
Another question here lead me to a new problem. In C++ Builder I need to assign a reader to the TFDBatchMove::Reader property in runtime. The TFDBatchMove component is dropped on the form but then I ...
2
votes
2
answers
442
views
MISRA C:2012 11.3 violation casting (float *) to (uint32_t *)
I have the following code from a serial message encoder:
uint32_t bits;
*((float *) &bits) = some_external_variable;
pFrame->data[0x2C] = ((uint8_t)(bits >> 0x18) & 0xFFu);
pFrame->...
-2
votes
1
answer
147
views
In what cases does the standard forbid taking pointers to functions/objects? [duplicate]
This article lists all the different categories of pointers. I have tested the explicit conversion of different types of pointers to const void* in the following snippet (live):
#include <print>
...
4
votes
1
answer
235
views
void* ptr = &func; compiles with msvc without any diagnostic but both gcc and clang rejects it
I am learning C++ using the book "C++ Primer" by Stanley. In particular, the section about "pointer conversions" says:
a pointer to any nonconst type can be converted to void*
...
7
votes
1
answer
244
views
Why doesn't gcc 13 display the correct binary represenation?
While answering a question here, I made the following example:
#include <stdio.h>
#include <math.h>
int main (void)
{
float_t a = -248.75;
printf("%f\n", a);
unsigned ...
0
votes
1
answer
970
views
MISRA C:2012 Rule 11.3 A cast shall not be performed between a pointer to object type and a pointer to a different object type
#define HNULL (void*)0
BYTE *VRAM_OPTION_I_BIT = HNULL;
WORD *VRAM_OPTION_I_WORD = HNULL;
void Func1(void)
{
VRAM_OPTION_I_BIT = (BYTE *)(PLC_Data + VRAM_OPTION_I_BIT_ADDR);
...
7
votes
1
answer
239
views
Is casting to (void**) well-defined?
Suppose A is a struct and I have a function to allocate memory
f(size_t s, void **x)
I call f to allocate memory as follows.
struct A* p;
f(sizeof(struct A), (void**)&p);
I wonder if (void**)&...
0
votes
1
answer
937
views
Procedurally bind struct fields to command line flag values using reflect
I have a several configuration structures that I want to automatically parse into accepted command line flags (to allow a user to override them via CLI). Given that the structures are likely to ...
0
votes
1
answer
292
views
Should static_pointer_cast calls be std:: qualified, or relied upon ADL?
static_pointer_cast resides in std namespace. Yet it takes pointers that are also in std namespace.
So both std:: qualified calls and unqualified calls are accepted.
Which one is the right, more ...
58
votes
10
answers
11k
views
Do all pointers have the same size in C++?
Recently, I came across the following statement:
It's quite common for all pointers to have the same size, but it's technically possible for pointer types to have different sizes.
But then I came ...
0
votes
1
answer
156
views
C++ Socket recv() always returns 0
This does work:
char blockSize[4];
int r = recv(socket, blockSize, 4, 0);
This does not work. It always returns zero, why?
size_t blockSize = 0;
int r = recv(socket, (char*)blockSize, 4, 0);
It's ...
0
votes
1
answer
73
views
does implicit pointer conversion occur during assignment?
For e.g.
int x = 3;
float * ptr = (float*)&x; // here compiler does not implicitly do conversion, but we have to manually convert to float*
so my question is, why here we don't need to ...
0
votes
1
answer
200
views
Proper handling of 128..255 chars in C
I need to process some Win-1251-encoded text (8-bit encoding, uses some of 128..255 for Cyrillic). As far as I can tell, C was created with 7-bit ASCII in mind, no explicit support for single-byte ...
0
votes
1
answer
408
views
Bug casting from bool* to void* to int*
Often in C++, one has a parameter void* user_data that one can use to pass an arbitrary type.
I used this to pass an array of booleans. However, I had a bug where I cast from bool* --> void* --> ...
3
votes
3
answers
768
views
Might casting void* into unsigned long int cause undefined behaviour even where sizeof(void*)==sizeof(unsigned long int)
size_t size_int = sizeof(unsigned long int);
size_t size_ptr = sizeof(void*);
printf("sizeof(unsigned long int): %zu\n", size_int);
printf("sizeof(void*): %zu\n", size_ptr);
if(...
0
votes
2
answers
130
views
Can Anyone explain why I'm getting 1 here? [closed]
it's a pointer arithematic question
//code
#include<iostream>
using namespace std;
int main()
{
int a=20,b=50,*p,*q;
p=&a;
q=&b;
...
2
votes
2
answers
318
views
Aliasing and pointer-interconvertability
Given the following code:
struct Tag {};
struct X {
// Tag t; // if not commented it shouldn't be pointer-interconvertible
int k;
};
int fn(const X& x, int& p) {
int i = x.k;
...
2
votes
2
answers
274
views
Address location and value at address location in C
int PileInts[1024];
char *Pile = (char *)PileInts;
What do these two lines of code do? I am thinking that the char *Pile = (char *)PileInts; line creates a character named *Pile that gives the value ...
3
votes
2
answers
413
views
Is (void*) ptr == ptr always true?
I cut this question out of my last question because i thought this was rather an individual question. So i found the passages for pointer conversion in the standard from which the ones regarding my ...
3
votes
1
answer
505
views
C++: is reinterpret_cast the best choice in these scenarios?
This has been bugging me for a very long time: how to do pointer conversion from anything to char * to dump binary to disk.
In C, you don't even think about it.
double d = 3.14;
char *cp = (char *)&...
6
votes
1
answer
918
views
Passing a Swift protocol to an Objective-C pointer
Using XCode 10.1 / Swift 4.2.
I'm trying to assign an object that conforms to a Swift protocol to an Objective-C pointer. The following code is a minimal example that compiles and works as expected, ...
-3
votes
1
answer
76
views
Pointer casting:can the Pointer has value?
can the Pointer have value??
so In which case is it used
int num=100;
int* iptr=NULL;
iptr=reinterpret_cast<int*>(num);
printf("%d \n",num);
printf("%d \n",num);
result
100
100
3
votes
1
answer
2k
views
Accessing first field of struct in a union of structs
I have three structs that share the first type and name of the first field:
struct TYPEA {
char *name;
int x,y; /*or whatever*/
};
struct TYPEB {
char *name;
float a[30]; /*or whatever*/
};...
4
votes
2
answers
1k
views
Is it legal to implement inheritance in C by casting pointers between one struct that is a subset of another rather than first member?
Now I know I can implement inheritance by casting the pointer to a struct to the type of the first member of this struct.
However, purely as a learning experience, I started wondering whether it is ...
17
votes
2
answers
603
views
Proper type for a dynamic array of const strings
I was always under the impression that const char **x was the correct type to use for a dynamically allocated array of const strings, like so:
#include <stdlib.h>
int main()
{
const char **...
1
vote
1
answer
418
views
Protobuf-2.6.1 compile error on Solaris 10 SPARC 64
When trying to compile Protobuf-2.6.1 on Solaris 10 SPARC 64, I get:
./google/protobuf/stubs/once.h: In function `void google::protobuf::GoogleOnceInit(google::protobuf::ProtobufOnceType*, void (*)())...
0
votes
1
answer
1k
views
C++ polymorphism function taking void * and other pointer type as argument: is it considered ambiguous?
C++ polymorphism functions taking void * and other pointer type as their arguments: is it considered ambiguous?
I am worried that since any pointer can be cast to void*, will the 2nd call of bar ...
2
votes
1
answer
608
views
Is it safe to cast pointer to integer, increment that integer, and cast back?
Suppose I have a valid pointer p0:
T a[10];
T* p0 = &a[0];
I know that I can safely round-trip-cast it like this:
reinterpret_cast<T*>(reinterpret_cast<uintptr_t>(p0)) == p0;
But is ...
2
votes
1
answer
1k
views
C++ implicit conversion of pointer type
Consider this case:
int *ptr;
int offset;
ptr = <some_address>;
offset = 10;
Assume that offset is 32-bit variable. ptr has type int*, the target architecture is 64-bit (so ptr is 8-byte ...
2
votes
1
answer
2k
views
C function pointer type compatibility
Writing a library that works with function callbacks, I've frequently type-casted (and called) function pointers to types with the same calling convention and same signatures, but with one exception: ...
2
votes
1
answer
132
views
Why is it not allowed to cast Derived T::* to Base T::*?
Background: Many functional languages support algebraic data types, which can to a degree be emulated with virtual functions and inheritance.
The most obvious solution involves a heap allocation, ...
6
votes
1
answer
542
views
Pointer/integer arithmetic (un)defined behaviour
I have the following function template:
template <class MostDerived, class HeldAs>
HeldAs* duplicate(MostDerived *original, HeldAs *held)
{
// error checking omitted for brevity
MostDerived ...
-2
votes
2
answers
359
views
Type conversion with pointers
I'm at a loss to understand how typecasting works with pointers
double x = 0.7;
int *ptr = (int *)&x;
What is happening with *(byte )&x ? &x means the address of variable x. Then what ...
2
votes
1
answer
1k
views
Conversion of uchar array to uint8 type pointer
I want to convert a array of uchar to uint8 pointer. As both are of 8 bits and value ranges from 0 to 255 so I do not think it should cause and issue.
uchar list[100];
I have to pass above list to a ...
3
votes
1
answer
143
views
C++ conversion: have pointer to object member, calculate pointer to object
C++ has static_cast to convert base_class_pointer to derived_class_pointer.
It is very similar operation to convert object_data_member_pointer to object_pointer.
I wrote the function ...
-2
votes
1
answer
1k
views
reinterpret_cast in C++
uint32_t r,g,b;
r = (uint32_t)145;
g = (uint32_t)131;
b = (uint32_t)139;
uint32_t rgb = ((uint32_t)r << 16 | (uint32_t)g << 8 | (uint32_t)b);
float rgbf = *reinterpret_cast<float*&...
20
votes
1
answer
17k
views
Can't pass non-const T* to a function accepting reference to const T* [duplicate]
I am confused that why following code is not able to compile
int foo(const float* &a) {
return 0;
}
int main() {
float* a;
foo(a);
return 0;
}
Compiler give error as:
error: ...
4
votes
1
answer
341
views
Pointer-to-Pointer-to-Const Conversion
I'm reading a book called C++ Gotchas which explains the conversions between const pointers and I'm having some trouble understanding the following rules:
Two pointer types T1 and T2 are similar if ...
16
votes
3
answers
3k
views
Conversion from Derived** to Base**
I was reading this and unfortunately could not understand in depth why the compiler does not allow conversion from Derived** to Base**. Also I have seen this which gives no more info than the ...