MPLAB: XC8: Cannot assign struct variable - mplab

I am trying to compile the following code in MPLab v5.10 using XC8 for a PIC18.
The code is as follows:
struct vec2i {
int x;
int y;
};
void main(void) {
static struct vec2i array[10];
int i;
for(i = 0; i < 10; ++i) {
array[i] = {0, 0};
}
return;
}
This yields the following error:
newmain.c:11:20: error: expected expression
array[i] = {0, 0};
This code compiles just fine on my native gcc compiler.
If I change the code to the following, the error goes away.
struct vec2i {
int x;
int y;
};
void main(void) {
static struct vec2i array[10];
int i;
for(i = 0; i < 10; ++i) {
// array[i] = {0, 0};
array[i].x = 0;
array[i].y = 0;
}
return;
}
I am using the free version of XC8, version 2.05. Is this a bug, or am I overlooking something with regards to the PIC architecture?

Looks like I don't know C as well as I thought; The following post clarifies the problem I was facing: Struct initialization in C with error: expected expression
The corrected code reads as follows:
struct vec2i {
int x;
int y;
};
void main(void) {
static struct vec2i array[10];
int i;
for(i = 0; i < 10; ++i) {
array[i] = (struct vec2i){0, 0};
}
return;
}
Note that this kind of workaround is only available under C99. Selecting C90 from MPLAB verifies this (the above code fails to compile under C90).
As for the code working on my machine, I was actually using g++, which has support for extended initialier lists since c++11, which is enabled by default.

Related

What is the union of bitfield equivalent in Dart?

I'd like to achieve the following C code in Dart:
union AuxiliaryHardwares {
uint16_t all = 0;
struct {
hasRs485: 1;
hasCanbus: 1;
hasRelay0: 1;
hasRelay1: 1;
};
};
AuxialiaryHardwares ah;
ah.all = 123; // Or any value I read from the network
if (ah.hasCanbus) {
// blah blah
}
if (ah.hasRelay0) {
// blah blah
}
There is no language support for accessing individual bits of an integer as an integer or boolean variable.
Traditionally, you'd write it yourself.
class AuxiliaryHardwares {
static const int _rs485Flag = 1;
static const int _canbusFlag = 2;
static const int _relay0Flag = 4;
static const int _relay1Flag = 8;
int all = 0;
bool get hasRs45 => all & _rs45Flag != 0;
bool get hasCanbus => all & _canbusFlag != 0;
bool get hasRelay0 => all & _relay0Flag != 0;
bool get hasRelay1 => all & _relay1Flag != 0;
// Setters too if you want them, e.g.
void set hasRs45(bool value) {
all = value ? (all | _rs45Flag) : (all & ~_rs45Flag);
}
}
You can probably find a bit-set class somewhere which abstracts over accessing individual bits of an integer, but it'll be extra overhead for very little gain.
I expect that "inline classes" will be the future way to encapsulate integers like this.

iOS inject code when code is on compile by Xcode or clang

Can I inject some special code when iOS project code is compiling,not on runtime. I want insert a function into project any functions!
Just like this:
//the original code
//the filename is user_code.cpp
int f1(){ return 1; }
int f2(){ return 2; }
int f3(){ return 3; }
int main(){
for(int i=0;i<1000;i++)f1();
for(int i=0;i<10000;i++)f2();
for(int i=0;i<100000;i++)f3();
return 0;
}
//the injected code
int function_counter[3];
int f1(){ function_counter[0]++; return 1; }
int f2(){ function_counter[1]++; return 2; }
int f3(){ function_counter[2]++; return 3; }

Binding multiple pthreads, each to the same member function of a different object from the same class

I have bound multiple pthreads to independent member function of independent objects from the same class.
I had to use of a static member function as a helper since it is not possible to bind a member function directly to a pthread in C++; however, my application behaves strangely and I am suspicious to the use of this static function, since this static function is shared between all objects of the same class.
Is this type of usage right? Is there any alternative solution?
I appreciate to hear any guidance.
class Region
{
public:
Region();
void Init();
void Push_Tuple(int int_value, float float_value, bool tuple_source);
static void *Read_Tuple_R_Process_S_Update_helper(void *context)
{
return ((Region *)context)->Read_Tuple_R_Process_S_Update();
}
void* Read_Tuple_R_Process_S_Update();
static void *Read_Tuple_S_Process_R_Update_helper(void *context)
{
return ((Region *)context)->Read_Tuple_S_Process_R_Update();
}
void* Read_Tuple_S_Process_R_Update();
};
int main(){
Region regions[THREAD_COUNT*2];
for(int i=0; i < THREAD_COUNT*2; i++){
regions[i].Init();
}
pthread_t thread_ID[THREAD_COUNT*2];
void* exit_status;
for(int i=0; i < THREAD_COUNT; i++){
pthread_create(&thread_ID[i], NULL, &Region::Read_Tuple_R_Process_S_Update_helper, &regions[i]);
}
for(int i=THREAD_COUNT; i < THREAD_COUNT*2; i++){
pthread_create(&thread_ID[i], NULL, &Region::Read_Tuple_S_Process_R_Update_helper, &regions[i]);
}
for(int i=0; i < THREAD_COUNT*2; i++){
pthread_join(thread_ID[i], &exit_status);
}
return 0;
}
Although I was suspected to the binding that I described, it was not the source of my problem and it works correctly.
The problem was waiting while loops which were affected by optimizer! Using volatile keyword solved my problem.

dmd can't infer type even when type is given

Here I want to lockstep iterate over two arrays of size_t
import std.stdio;
import std.range;
import std.exception;
import std.conv;
struct zip(R,Q)
if(isInputRange!(R) && isInputRange!(Q))
{
R r;
Q q;
#property
const auto front() {
return tuple(r.front, q.front);
}
void popFront() {
r.popFront();
q.popFront();
}
#property
const bool empty() {
bool re = r.empty;
enforce(re == q.empty);
return re;
}
}
void main() {
size_t[] a = [0,1,2,3,4,5];
size_t[] b = [2,3,4,5,6,7];
foreach(size_t i, size_t j; zip!(size_t[],size_t[])(a,b)) {
writeln(to!string(i) ~ " " ~ to!string(j));
}
}
But this fails to compile with
src/Interpreter.d(30): Error: cannot infer argument types
However when I change the foreach line to use uint instead of size_t (I'm on a 32-bit laptop)
foreach(uint i, uint j; zip!(size_t[],size_t[])(a,b)) {
It compiles and runs just fine. What's going on?
It might be a bug. In v2.065.0 it doesn't work, but it does work in the latest git-head development version.

aparapi start index of getGlobalId()

i use aparapi for parallelize and i wante to convert this java code:
public static void main(String[] args) {
float res = 0;
for (int i = 2; i < 5; i++) {
for (int j = 3; j < 5; j++) {
res += i * j;
}
}
System.out.println(res);
}
to its equivalent in aparapi:
Kernel kernel = new Kernel() {
#Override
public void run() {
int i = getGlobalId();
...
}
};
kernel.execute();
kernel.dispose();
There are a few issues here.
First your code is not data parallel. You have a 'race' condition on 'res' so this code cannot be computed on the GPU.
Secondly the range of execution is way too small. You are trying to execute 6 threads (x [2,3,4] * y [ 3,4]). This will not really gain any benefit from the GPU.
To answer the question regarding how you might implement over the 2 dim grid above.
Range range = Range.create2D(3, 2) ; // A two dimension grid 3x2
Kernel kernel = new Kernel() {
#Override
public void run() {
int x = getGlobalId(0)+2; // x starts at 2
int y = getGlobalId(1)+3; // y starts at 3
...
}
};
kernel.execute(range);
kernel.dispose();

Resources