Crazy behavior when using compiler optimization - compiler-optimization

Execution of the following code runs to "!!!" when the compiler is optimized:
int test()
{
volatile uint32_t flag = 0; /* volatile doesnt matter */
flag = 3;
if (flag == 0 )
{
return 0; // !!!
}
else
{
return 1;
}
}
Compiler: IAR Studio C compiler; Platform: SAM4C microcontroller; medium level optimization
Of course, this is already a code, where the original problem has been boiled down.
I cannot understand what the compiler is doing here ...
On the other hand, this works as expected:
int test()
{
volatile uint32_t flag = 0; /* volatile doesnt matter */
int result = 0;
flag = 3;
if (flag == 0 )
{
result = 0;
}
else
{
result = 1; // !!!
}
return result;
}

I spent some more time and observed, that the function indeed returns 1, although the debugger stops at the line with "return 0;".
The problem I ran into was rather related to an uninitialized variable (outside the function), which is not automatically set to zero during optimization.
Apart from the strange behavior in the debugger, the function is correct. Unfortunately, the observation resulted in a misinterpretation.

Related

how to combine ADRESH and ADRESL on 12 bit ADC

MICRO: PIC18LF47K42
compiler: XC8
application: MPLABX
I am trying to combine the values in of 12 bit ADC. They go into ADRESH and ADRESL. My ADC is set up for right-justify which does formating as so:
ADRESH:(----MSB,x,x,x) ADRESL: (X,X,X,X,X,X,X,LSB)
From inspecting the value in my result register i can tell i dont have a great resolution. Im pretty sure its becasue of how im combining ADRESH and ADRESL. How could i do this?
#include "myIncludes.h"
volatile unsigned char ZCDSoftwareFlag = 0;
volatile unsigned char switchValue = 0;
void main(void)
{
portInit();
triac = 0;
unsigned char result;
adcInit();
while(1)
{
__delay_us(4);
ADCON0bits.GO = 1; //Start conversion
while (ADCON0bits.GO); //Wait for conversion done
result = ADRESH;
result = result << 8;
result = result |ADRESL;
}
}
And heres the ADC init function
void adcInit(void)
{
ADCON0bits.FM = 1; //right-justify
ADCON0bits.CS = 1; //ADCRC Clock
ADPCH = 0x00; //RA0 is Analog channel
ADCON0bits.ON = 1; //Turn ADC On
ADCON0bits.GO = 1; //Start conversion
}
You try to put an 12Bit result in an 8 Bit variable. Switch it to 16Bit.
uint16_t result;
Then you can combine the values:
result = ADRESH;
result = result << 8;
result = result |ADRESL;

Sending strings to BPF Map Space and printing them out

I have a small txt file that I would like to write to BPF here. Here is what my python code looks like for BPF but I am unable to print out anything as of now. I keep ending up with a Failed to load program: Invalid argument with a bunch of register errors. As of now my string basically says hello, world, hi
BPF_ARRAY(lookupTable, char, 512);
int helloworld2(void *ctx)
{
//print the values in the lookup table
#pragma clang loop unroll(full)
for (int i = 0; i < 512; i++) {
char *key = lookupTable.lookup(&i);
if (key) {
bpf_trace_printk("%s\n", key);
}
}
return 0;
}
Here is the Python code:
b = BPF(src_file="hello.c")
lookupTable = b["lookupTable"]
#add hello.csv to the lookupTable array
f = open("hello.csv","r")
file_contents = f.read()
#append file contents to the lookupTable array
b_string1 = file_contents.encode('utf-8')
b_string1 = ctypes.create_string_buffer(b_string1)
lookupTable[0] = b_string1
f.close()
b.attach_kprobe(event=b.get_syscall_fnname("clone"), fn_name="helloworld2")
b.trace_print()
I have the error linked in this pastebin since it's so long:
BPF Error
One notable error is the mention of infinite loop detected which is something I would need to check out.
The issue is that i is passed by pointer in bpf_map_lookup_elem, so the compiler can't actually unroll the loop (from its point of view, i may not linearly increase).
Using an intermediate variable is enough to fix this:
BPF_ARRAY(lookupTable, char, 512);
#define MAX_LENGTH 1
int helloworld2(void *ctx)
{
//print the values in the lookup table
#pragma clang loop unroll(full)
for (int i = 0; i < 1; i++) {
int k = i;
char *key = lookupTable.lookup(&k);
if (key) {
bpf_trace_printk("%s\n", key);
}
}
return 0;
}

swift 3 variable used before begin initialized

I have an issue with my n variable. I cannot use n in for loop. Why? n was initialized before for loop. Please, help.
import Foundation
var n: Int
var t: Int
while(true){
var tt = readLine()
t = Int(tt!)!
if (t==0){
break
}
else if ( t < 0){
n = t*(-1)
}
else if(t > 0){
n = t
}
var arr : [[String]] = []
for i in 0..<n*2{
for y in 0..<n*2{
arr[i][y] = "."
}
}
}
A variable may be declared and not immediately initialized, as long as initialization is guaranteed before first use
The error is more subtle than at first glance. You may actually declare a property without initializing it, as long as all program flows leading to its first use ascertain initialization of it.
The issue is with the if, else if and else if block:
var n: Int // declaration
// ...
if (t == 0) {
break
}
else if (t < 0) {
n = t*(-1)
}
else if (t > 0){
n = t
}
// first use
for i in 0..<n*2 { /* ... */ }
Swift cannot not infer that this block is in fact exhaustive, and believes that there is a possibility that none of the above if statements holds, which, in the eyes of the compiler, would lead to the following program state:
program flow has not been broken (break)
and n has not been instantiated
As humans, however, we know that the if - else if - else if block above is indeed exhaustive, and can help the compiler out by simply changing the last if else statement to a simple else statement.
if (t == 0) {
break
}
else if (t < 0) {
n = t*(-1)
}
// if none of the above, t > 0
else {
n = t
}
On another note, the nested array access of non-existing array elements, arr[i][y] = "." will lead to a runtime exception, but this is another issue. In its current form, it looks as if the intent with the nested loops could be replaced with a nested array instantiation:
var arr = [[String]](repeating: [String](repeating: ".", count: 2*n), count: 2*n)
or,
var arr = (0..<2*n).map { _ in [String](repeating: ".", count: 2*n) }
The variable n is only declared, not initialized.
To initialize the variables:
var n: Int = 0
var t: Int = 0

Netbeans 7.3 misreports an "Unused assignment" hint

Netbeans 7.3.1 IDE says that "The assigned value is never used" in the indicated line of the following program:
public class JavaTest {
static int f() {
return Math.random() < 0.9 ? 0 : 1;
}
static int g() {
return Math.random() < 0.2 ? 0 : 1;
}
public static void main(String[] args) {
int ret;
while ((ret = f()) == 0) { // Unused assignment???
ret = g();
if (ret != 0)
System.out.println(ret);
}
System.out.println(ret);
}
}
I guess this is a bug in Netbeans, but can someone confirm if they have seen it before?
Edit:
Excellent point and sorry I didn't see it earlier. I agree with you now and I can confirm with your exact code in Eclipse Juno SR2 that there is no warning about unused assignment. Netbeans is in error!
Original:
Netbeans is correct... you immediately assign ret a new value after that line so you might as well just compare f() to 0 e.g. while(f() == 0)

Is this initialisation of a primitive variable correct?

I'm calling something like this kind of function here I have a variable named index I'm initiating it to 0 initially because I have a static analyzer leak in that initialization line. Is this the correct way of resolving that leak? Since I'm initiating to 0 and after my first if condition becomes true then again assigning to 0. Would this cause any problems?
-(NSString *)loadSelected:(NSString*)selectedOptn{
int index = 0;
if ([selectedOptn isEqualToString:#"A"]) {
index = 0;
}
else if([selectedOptn isEqualToString:#"B"]){
index = 1;
}
else if([selectedOptn isEqualToString:#"C"]){
index = 2;
}
else if([selectedOptn isEqualToString:#"D"]){
index = 3;
}
return [[array.options objectAtIndex:index] objectForKey:#"xyz"];
}
No, this won't cause any problems, I'm curious to know what the error was though - are you sure it wasn't just warning you that you hadn't initialised it or something?