casting

Casting const void* to const int*

﹥>﹥吖頭↗ 提交于 2021-02-10 15:53:40
问题 I haven't used void* and const_correctness before so I am not understanding what I am doing wrong in the below code. All I want is to cast a void* returned by a member function of a const object to int*. Please suggest better approaches. Thank you. I get the following error passing 'const MyClass' as 'this' argument of 'void* MyClass::getArr()' discards qualifiers So here's the actual program that I had problem with class MyClassImpl{ CvMat* arr; public: MyClassImpl(){arr = new CvMat[10];}

Is casting uint8_t to signed int at least sometimes incorrect?

落爺英雄遲暮 提交于 2021-02-10 14:19:30
问题 While reading the answer to the following question Getting a buffer into a stringstream in hex representation: I did not understand why there is a necessity to cast uint8_t to unsigned (or, as written in comments, even to unsigned char before that), while casting just to int is incorrect. As I understand, no conversions at all would lead to interpreting uint8_t as an underlying type which can (must?) be some of 3 char variations and thus printing it as a character. But what's wrong with

Why movzbl is used in assembly when casting unsigned char to signed data types?

风流意气都作罢 提交于 2021-02-10 11:37:04
问题 I'm learning data movement( MOV ) in assembly. I tried to compile some code to see the assembly in a x86_64 Ubuntu 18.04 machine: typedef unsigned char src_t; typedef xxx dst_t; dst_t cast(src_t *sp, dst_t *dp) { *dp = (dst_t)*sp; return *dp; } where src_t is unsigned char . As for the dst_t , I tried char , short , int and long . The result is shown below: // typedef unsigned char src_t; // typedef char dst_t; // movzbl (%rdi), %eax // movb %al, (%rsi) // typedef unsigned char src_t; //

Why movzbl is used in assembly when casting unsigned char to signed data types?

回眸只為那壹抹淺笑 提交于 2021-02-10 11:36:30
问题 I'm learning data movement( MOV ) in assembly. I tried to compile some code to see the assembly in a x86_64 Ubuntu 18.04 machine: typedef unsigned char src_t; typedef xxx dst_t; dst_t cast(src_t *sp, dst_t *dp) { *dp = (dst_t)*sp; return *dp; } where src_t is unsigned char . As for the dst_t , I tried char , short , int and long . The result is shown below: // typedef unsigned char src_t; // typedef char dst_t; // movzbl (%rdi), %eax // movb %al, (%rsi) // typedef unsigned char src_t; //

Type casting: double to char: multiple questions

核能气质少年 提交于 2021-02-10 10:41:47
问题 Consider this code: #include <stdio.h> int main(void) { /* TEST 1 */ double d = 128; char ch = (char)d; printf("%d\n", ch); /* TEST 2 */ printf("%d\n", (char)128.0); /* TEST 3 */ char ch1 = (char)128.0; printf("%d\n", ch1); return 0; } Results: gcc* clang* cl* TEST 1 -128 -128 -128 TEST 2 127 0 -128 TEST 3 127 -2 -128 * latest version Questions: Why the results differ between tests (excluding cl )? Why the results differ between compilers (excluding TEST 1 )? In case of UB/IB, where is the UB

Type casting: double to char: multiple questions

南笙酒味 提交于 2021-02-10 10:41:26
问题 Consider this code: #include <stdio.h> int main(void) { /* TEST 1 */ double d = 128; char ch = (char)d; printf("%d\n", ch); /* TEST 2 */ printf("%d\n", (char)128.0); /* TEST 3 */ char ch1 = (char)128.0; printf("%d\n", ch1); return 0; } Results: gcc* clang* cl* TEST 1 -128 -128 -128 TEST 2 127 0 -128 TEST 3 127 -2 -128 * latest version Questions: Why the results differ between tests (excluding cl )? Why the results differ between compilers (excluding TEST 1 )? In case of UB/IB, where is the UB

Segmentation fault when casting int to char in C

跟風遠走 提交于 2021-02-10 10:23:00
问题 I have a very simple C program. In main, I have this operation: int main(){ int theNumber = 9009; printf("%s", (char *)theNumber); } And when I run it, it gives me a seg fault. Any idea why? Am I doing this wrong? Expected Output This code should convert theNumber to a string and then print it. So the output would be: 9009 Actual Output A segmentation fault. 回答1: This is trying to print whatever is found at the address 9009 . Seeing as the operating system is not giving your program access to

Dynamic type casting in C

南笙酒味 提交于 2021-02-10 06:32:33
问题 I'm trying to write a function taking as a parameter a buffer (void *), the type size, the type name and the number of elements. The buffer may contain values of a limited number of basic types (int, float, double, ...). Within this function, I would like to be able to, let's say, increment each value. It looks like that : void increment (void *buffer, int type_size, char *type_name, int n_elements) { for (int i=0; i<n_elements; i++) ((MACRO(type_name))(buffer))[i]++; // ??? return; } My

Dynamic type casting in C

和自甴很熟 提交于 2021-02-10 06:31:09
问题 I'm trying to write a function taking as a parameter a buffer (void *), the type size, the type name and the number of elements. The buffer may contain values of a limited number of basic types (int, float, double, ...). Within this function, I would like to be able to, let's say, increment each value. It looks like that : void increment (void *buffer, int type_size, char *type_name, int n_elements) { for (int i=0; i<n_elements; i++) ((MACRO(type_name))(buffer))[i]++; // ??? return; } My

Can casting in safe Rust ever lead to a runtime error?

我是研究僧i 提交于 2021-02-10 04:13:50
问题 I'm fiddling a bit with Any and casting just to get a deeper understanding of Rust. From C# I'm used to the fact that casting can lead to runtime exceptions because casting in C# basically means Dear compiler, trust me, I know what I'm doing please cast this into an int32 because I know it will work. However, if you're doing an invalid cast the program will blow up with an Exception at runtime. So I was wondering if casting in (safe) Rust can equally lead to a runtime exception. So, I came up