casting

Reading a value from raw memory (MISRA compliant)

落花浮王杯 提交于 2019-12-13 01:23:44
问题 I'm trying to read the value of a variable previously write on NVM flash. My code is: uintptr_t address = getAddress(); //[MISRA C++ Rule 5-2-8] cast from unsigned int to pointer uint16_t value = *(reinterpret_cast<uint16_t*>(address)); The problem is the cast from uintptr_t to pointer is not allowed in MISRA. Do anyone knows a way to access this memory? I'm breaking one of the big rules of MISRA. Using dynamic memory (the contents of flash is dynamic so the address of data is variable). Only

When casting an int array to a short*, why does assigning a value to an element overwrite the entire integer?

江枫思渺然 提交于 2019-12-13 01:14:07
问题 I am watching Jerry Cain's Programming Paradigms Lecture 3 video where the effect of an element assignment after casting between an int array and short array is demonstrated. Essentially the argument is that if you were to assign an int array element arr[3] = 128 , then temporarily cast the int array to a short* and assign arr[6] = 2 , then arr[3] should become 128 + 512 = 640 because the 2 would be interpreted as being in the 2^9th position. Code to demonstrate: #include <stdio.h> int main()

.net float rounding errors at compile-time vs runtime

半腔热情 提交于 2019-12-13 01:12:05
问题 I was recently setting up some data for a test case checking rounding errors on the float data type, and ran into some unexpected results. I expected that cases t2 and t3 would produce the same result as t1, but that is not the case on my machine. Can anyone tell me why? I suspect the reason for the difference is that t2 and t3 are evaluated at compilation, but I'm surprised that the compiler completely ignores my attempts to force it to use an intermediate float data type during evaluation.

In C#, is it possible to cast a List<Child> to List<Parent>?

安稳与你 提交于 2019-12-13 00:49:16
问题 I want to do something like this: List<Child> childList = new List<Child>(); ... List<Parent> parentList = childList; However, because parentList is a List of Child's ancestor, rather than a direct ancestor, I am unable to do this. Is there a workaround (other than adding each element individually)? 回答1: Casting directly is not allowed because there's no way to make it typesafe. If you have a list of giraffes, and you cast it to a list of animals, you could then put a tiger into a list of

Why does a pointer to array need to be cast before being passed as parameter to a function with array type argument?

浪尽此生 提交于 2019-12-13 00:43:20
问题 In C99, why is it that declaring a variable p as pointer to array needs to be cast before it is passed as a parameter to a function with array type argument, but declaring a variable p as void pointer then casting it to a pointer to array can be passed as pointer to array to the same function? #include <stdio.h> int arreglo(int locArr[]) { locArr[0]=1; printf("el arreglo es : %i\n",locArr[0]); return 0; } int main() { /* Declare a pointer p to array */ int (*p)[]; int manArr[10]; p=&manArr; /

Sql Server. Why field value of [almost] any type may be treated as quoted string in Query/DML?

余生长醉 提交于 2019-12-13 00:24:31
问题 I discovered SUDDENLY that in SQL Server (2000) any field type value may be treated as quoted string (in query or DML). Question: Is it normal behavior or accidentally successful result? Example: CREATE TABLE [Test_table] ( [int_field] [int] NULL , [float_field] [float] NULL , [date_field] [datetime] NULL , [id] [int] NOT NULL ) ON [PRIMARY] GO update test_table set int_field = 100, float_field = 10.01, date_field = CAST('2013-11-10' AS DATETIME) where id = 1 update test_table set int_field =

Problems casting a null object with Spock

谁说我不能喝 提交于 2019-12-13 00:17:56
问题 I have a Spock test that fails over two Mac OS X Lion machines, but works over other Linux machines and the Spock Web Console. I found another related question: Why I get a cannot cast object 'null' error, when testing my controller? I'm using Grails 2.0.0 and Spock 0.6. Any suggestions? Seems to be a bug def "casting null object"() { expect: null as BigDecimal == null } | Failure: casting null object(com.arturoherrero.MySpec) | org.codehaus.groovy.runtime.typehandling.GroovyCastException:

Is the member field order of a class “stable”?

喜你入骨 提交于 2019-12-12 20:51:48
问题 Considering c++ (or c++11), where I have some array of data with 2*N integers which represent N pairs. For each even i=0,2,4,6,...,2*N it holds that (data[i],data[i+1]) forms such a pair. Now I want to have a simple way to access these pairs without the need to write loops like: for(int i=0; i<2*N; i+=2) { ... data[i] ... data[i+1] ... } So I wrote this: #include <iostream> struct Pair { int first; int second; }; int main() { int N=5; int data[10]= {1,2,4,5,7,8,10,11,13,14}; Pair *pairs =

How to pass an int as “void *” to thread start function?

纵然是瞬间 提交于 2019-12-12 20:42:58
问题 I originally had a global variable for my fibonacci variable array, but found out that is not allowed. I need to do elementary multithreading and handle race conditions, but I can't get past feeding an int as a void argument in pthread create. I've tried using a constant pointer with no luck. For some strange reason the void* gets past the first boolean test but not the else if: $ gcc -o fibonacci fibonacci.c fibonacci.c:22:16: warning: comparison between pointer and integer ('void *' and

Casting a string to a generic

只愿长相守 提交于 2019-12-12 20:25:08
问题 I have this method: public T GetInput<T>() { T result; if( (typeof)T == Type.GetType("string")) { result = GetStringInput(); // returns a string } // Etc for a bunch of different types } The error I'm getting is that I can't implicitly cast a string to a "T". The point of the function is to be able to get input of any specified type and make sure to do type validation on the input before returning it. Ideas? 回答1: You cannot simply assign variable of undetermined on compile time type T with