• AVR Freaks

AnsweredHot!xc8 Compiler "Error 712 Cant Generate Code" When using Pointers to Const 2D Array

Page: 12 > Showing page 1 of 2
Author
beb
New Member
  • Total Posts : 30
  • Reward points : 0
  • Joined: 2019/11/12 13:38:42
  • Location: 0
  • Status: offline
2020/06/23 11:39:39 (permalink)
0

xc8 Compiler "Error 712 Cant Generate Code" When using Pointers to Const 2D Array

Hello, 
 
I have been attempting to update a const 2D array using pointers and functions, but the method I am using is yielding the following error in MPLABX using the xc8 compiler:
 
"error: (712) can't generate code for this expression"
 
I am initializing the array and the pointer to it as follows:
 
const unsigned long anim_seq[frames][scene];
unsigned long *access_anim_seq = &anim_seq[0][0];

 
Then I am updating "anim_seq" like so:
 
access_anim_seq = &anim_seq[i][j]; //error happens here
*(access_anim_seq + i * scene + j) = on;

 
The "i" and "j" are just index variables in a nested for loop, and "on" is a 32 bit value "0x200000FF". The values of "frame" and "scene" are definitions, which are 15 and 15 respectively. 
 
I can upload more of my code if that helps, but I thought I would keep it simple for the question.
 
Does anyone have any ideas what the issue is here? This is what a lot of online resources have said is how you implement something like this. 
#1
Ivan Cenov
Super Member
  • Total Posts : 291
  • Reward points : 0
  • Joined: 2003/11/07 12:36:51
  • Location: Bulgaria
  • Status: offline
Re: xc8 Compiler "Error 712 Cant Generate Code" When using Pointers to Const 2D Array 2020/06/23 11:54:50 (permalink)
0 (2)
beb
Hello, 
 
I have been attempting to update a const 2D array using pointers and functions, but the method I am using is yielding the following error in MPLABX using the xc8 compiler:
 
"error: (712) can't generate code for this expression"
 
I am initializing the array and the pointer to it as follows:
 
const unsigned long anim_seq[frames][scene];
unsigned long *access_anim_seq = &anim_seq[0][0];

 
Then I am updating "anim_seq" like so:
 
access_anim_seq = &anim_seq[i][j]; //error happens here
*(access_anim_seq + i * scene + j) = on;

 
The "i" and "j" are just index variables in a nested for loop, and "on" is a 32 bit value "0x200000FF". The values of "frame" and "scene" are definitions, which are 15 and 15 respectively. 
 
I can upload more of my code if that helps, but I thought I would keep it simple for the question.
 
Does anyone have any ideas what the issue is here? This is what a lot of online resources have said is how you implement something like this. 


Try
const unsigned long anim_seq[frames][scene];
unsigned long * const access_anim_seq = &anim_seq[0][0];

#2
beb
New Member
  • Total Posts : 30
  • Reward points : 0
  • Joined: 2019/11/12 13:38:42
  • Location: 0
  • Status: offline
Re: xc8 Compiler "Error 712 Cant Generate Code" When using Pointers to Const 2D Array 2020/06/23 12:04:27 (permalink)
0
When I try that I get the following error:
 
error: cannot assign to variable 'access_anim_seq' with const-qualified type 'unsigned long *const'
 
For this line of code


access_anim_seq = &anim_seq[i][j];

 
#3
beb
New Member
  • Total Posts : 30
  • Reward points : 0
  • Joined: 2019/11/12 13:38:42
  • Location: 0
  • Status: offline
Re: xc8 Compiler "Error 712 Cant Generate Code" When using Pointers to Const 2D Array 2020/06/23 12:19:33 (permalink)
0
I even tried using double pointers, and I got the same error:
 
unsigned long const anim_seq[frames][scene];
unsigned long const * access_anim_seq = &anim_seq[0][0];
unsigned long **pt2pt; //double pointer

//update anim_seq
access_anim_seq = &anim_seq[i][j];
pt2pt = &access_anim_seq;
**pt2pt = on;

 
But, when I get rid of the const for anim_seq, the code compiles and works!
unsigned long anim_seq[frames][scene]; //change here, now compiles!
unsigned long const * access_anim_seq = &anim_seq[0][0];
unsigned long **pt2pt; //double pointer

//update anim_seq
access_anim_seq = &anim_seq[i][j];
pt2pt = &access_anim_seq;
**pt2pt = on;

 
#4
Ivan Cenov
Super Member
  • Total Posts : 291
  • Reward points : 0
  • Joined: 2003/11/07 12:36:51
  • Location: Bulgaria
  • Status: offline
Re: xc8 Compiler "Error 712 Cant Generate Code" When using Pointers to Const 2D Array 2020/06/23 12:29:57 (permalink)
0
unsigned long anim_seq[frames][scene];

 
May be 15x15x4=900 is a too big array ... you may try with smaller frames and scene to see what will happen ...
#5
beb
New Member
  • Total Posts : 30
  • Reward points : 0
  • Joined: 2019/11/12 13:38:42
  • Location: 0
  • Status: offline
Re: xc8 Compiler "Error 712 Cant Generate Code" When using Pointers to Const 2D Array 2020/06/23 12:33:49 (permalink)
0
I made frames and scene both 5, and I got the same error. 
 
The trouble is I need "anim_seq" to be large, the reason I made it const was to store it in program memory vs RAM. Is there a better way to store a large 2D array in program memory and change it in real time?
#6
jtemples
عُضْوٌ جَدِيد
  • Total Posts : 11891
  • Reward points : 0
  • Joined: 2004/02/13 12:31:19
  • Location: Southern California
  • Status: offline
Re: xc8 Compiler "Error 712 Cant Generate Code" When using Pointers to Const 2D Array 2020/06/23 12:40:34 (permalink)
+1 (1)
The pointer should be
 
const unsigned long (*access_anim_seq)[scene] = anim_seq;

 
I'm assuming "frame" and "scene" are #defines?  Always show a complete example.
#7
jtemples
عُضْوٌ جَدِيد
  • Total Posts : 11891
  • Reward points : 0
  • Joined: 2004/02/13 12:31:19
  • Location: Southern California
  • Status: offline
Re: xc8 Compiler "Error 712 Cant Generate Code" When using Pointers to Const 2D Array 2020/06/23 12:44:03 (permalink)
+2 (2)
i_cenov
unsigned long * const access_anim_seq = &anim_seq[0][0];

 
This declares a const pointer, not a pointer to const.
#8
beb
New Member
  • Total Posts : 30
  • Reward points : 0
  • Joined: 2019/11/12 13:38:42
  • Location: 0
  • Status: offline
Re: xc8 Compiler "Error 712 Cant Generate Code" When using Pointers to Const 2D Array 2020/06/23 12:51:10 (permalink)
0
When I try that line of code, I get the following error:
 
error: read-only variable is not assignable
 
For the line:
*access_anim_seq[j] = on;

 
And for reference here is the initialization and update method:
const unsigned long anim_seq[frames][scene];
const unsigned long (*access_anim_seq)[scene] = anim_seq; //updated

access_anim_seq = &anim_seq[i][j];
*access_anim_seq[j] = on; //error here

#9
jtemples
عُضْوٌ جَدِيد
  • Total Posts : 11891
  • Reward points : 0
  • Joined: 2004/02/13 12:31:19
  • Location: Southern California
  • Status: offline
Re: xc8 Compiler "Error 712 Cant Generate Code" When using Pointers to Const 2D Array 2020/06/23 12:54:39 (permalink)
+2 (2)
You can't write to a const array.  That's the whole point of const.
#10
beb
New Member
  • Total Posts : 30
  • Reward points : 0
  • Joined: 2019/11/12 13:38:42
  • Location: 0
  • Status: offline
Re: xc8 Compiler "Error 712 Cant Generate Code" When using Pointers to Const 2D Array 2020/06/23 12:56:39 (permalink)
0
jtemples
You can't write to a const array.  That's the whole point of const.




Not even indirectly? Online it says you can update a constant value using pointers.
 
Example: https://www.geeksforgeeks.org/how-to-modify-a-const-variable-in-c/
 
Is there another way to store a large array and write to it in real time?
#11
crosland
Super Member
  • Total Posts : 1989
  • Reward points : 0
  • Joined: 2005/05/10 10:55:05
  • Location: Warks, UK
  • Status: offline
Re: xc8 Compiler "Error 712 Cant Generate Code" When using Pointers to Const 2D Array 2020/06/23 12:57:38 (permalink)
0
beb
Is there a better way to store a large 2D array in program memory and change it in real time?


You can't simply assign a value to a variable in program memory. Look up the Flash programming section of the data sheet for your device.
 
Define "real time".
#12
andersm
Super Member
  • Total Posts : 2822
  • Reward points : 0
  • Joined: 2012/10/07 14:57:44
  • Location: 0
  • Status: online
Re: xc8 Compiler "Error 712 Cant Generate Code" When using Pointers to Const 2D Array 2020/06/23 12:58:17 (permalink)
+2 (2)
Modifying a const object through a non-const pointer is not legal C, and is unlikely to work anyway if the compiler places constants in flash.
 
Edit: C99 §6.7.3, Type qualifiers:
5. If an attempt is made to modify an object defined with a const-qualified type through use of an lvalue with non-const-qualified type, the behavior is undefined.

post edited by andersm - 2020/06/23 13:01:08
#13
du00000001
Just Some Member
  • Total Posts : 3787
  • Reward points : 0
  • Joined: 2016/05/03 13:52:42
  • Location: Germany
  • Status: offline
Re: xc8 Compiler "Error 712 Cant Generate Code" When using Pointers to Const 2D Array 2020/06/23 12:59:16 (permalink)
+1 (1)
beb
I even tried using double pointers, and I got the same error:
 
unsigned long const anim_seq[frames][scene];
unsigned long const * access_anim_seq = &anim_seq[0][0];
unsigned long **pt2pt; //double pointer

//update anim_seq
access_anim_seq = &anim_seq[i][j];
pt2pt = &access_anim_seq;
**pt2pt = on;

 
But, when I get rid of the const for anim_seq, the code compiles and works!
unsigned long anim_seq[frames][scene]; //change here, now compiles!
unsigned long const * access_anim_seq = &anim_seq[0][0];
unsigned long **pt2pt; //double pointer

//update anim_seq
access_anim_seq = &anim_seq[i][j];
pt2pt = &access_anim_seq;
**pt2pt = on;




What's all this trolling here today?
You just cannot write to anything const (beyond once - the initialization value).
So when anim_seq[][] is const, there's no writing to it. Neither diretly nor via some fancy pointer. Shouldn't work with a double pointer as well. I'd expect the compiler to throw some errors about type conflicts (ptr-to-const vs. "normal" pointers).
THUS it worked once anim_seq[][] got the const removed.

PEBKAC / EBKAC / POBCAK / PICNIC (eventually see en.wikipedia.org)
#14
beb
New Member
  • Total Posts : 30
  • Reward points : 0
  • Joined: 2019/11/12 13:38:42
  • Location: 0
  • Status: offline
Re: xc8 Compiler "Error 712 Cant Generate Code" When using Pointers to Const 2D Array 2020/06/23 13:00:00 (permalink)
0
crosland
beb
Is there a better way to store a large 2D array in program memory and change it in real time?


You can't simply assign a value to a variable in program memory. Look up the Flash programming section of the data sheet for your device.
 
Define "real time".




"Real time" as in I initialize a large, empty array at the beginning of the program, then a function runs once to populate that array. Then that array can be called later in the program for various uses.
#15
jtemples
عُضْوٌ جَدِيد
  • Total Posts : 11891
  • Reward points : 0
  • Joined: 2004/02/13 12:31:19
  • Location: Southern California
  • Status: offline
Re: xc8 Compiler "Error 712 Cant Generate Code" When using Pointers to Const 2D Array 2020/06/23 13:06:08 (permalink)
+1 (1)
"Real time" as in I initialize a large, empty array at the beginning of the program, then a function runs once to populate that array. Then that array can be called later in the program for various uses.

 
No one here except you knows what "large" means or which PIC you're using, so we can only guess at solutions.  You still haven't explained why you made the array const.
#16
crosland
Super Member
  • Total Posts : 1989
  • Reward points : 0
  • Joined: 2005/05/10 10:55:05
  • Location: Warks, UK
  • Status: offline
Re: xc8 Compiler "Error 712 Cant Generate Code" When using Pointers to Const 2D Array 2020/06/23 13:10:19 (permalink)
+1 (1)
beb
"Real time" as in I initialize a large, empty array at the beginning of the program, then a function runs once to populate that array. Then that array can be called later in the program for various uses.



That doesn't really answer the question and you didn't address my point about program memory.
 
Use a part with enough RAM or learn how to use the FLASH, with all it's limitations.
#17
beb
New Member
  • Total Posts : 30
  • Reward points : 0
  • Joined: 2019/11/12 13:38:42
  • Location: 0
  • Status: offline
Re: xc8 Compiler "Error 712 Cant Generate Code" When using Pointers to Const 2D Array 2020/06/23 13:10:52 (permalink)
0
jtemples
"Real time" as in I initialize a large, empty array at the beginning of the program, then a function runs once to populate that array. Then that array can be called later in the program for various uses.

 
No one here except you knows what "large" means or which PIC you're using, so we can only guess at solutions.  You still haven't explained why you made the array const.




Sorry, using the PIC18F4580, and the reason I made it const was because an array of say size 20 x 20 would be too small for the application. 
 
The idea is that this array holds values for LED animations, and instead of calculating the LEDs as on or off one by one, the idea is that an array holding all the possible LED color and brightness combinations could just be called whenever. 
 
The code would flow like: initialize array -> populate it with one function call -> use the filled array in the animation function
#18
beb
New Member
  • Total Posts : 30
  • Reward points : 0
  • Joined: 2019/11/12 13:38:42
  • Location: 0
  • Status: offline
Re: xc8 Compiler "Error 712 Cant Generate Code" When using Pointers to Const 2D Array 2020/06/23 13:13:23 (permalink)
+1 (1)
I do realize now that I need to rethink the design of my code, thank you everyone for your feedback
#19
du00000001
Just Some Member
  • Total Posts : 3787
  • Reward points : 0
  • Joined: 2016/05/03 13:52:42
  • Location: Germany
  • Status: offline
Re: xc8 Compiler "Error 712 Cant Generate Code" When using Pointers to Const 2D Array 2020/06/23 13:13:47 (permalink)
+1 (1)
beb
<...snipped part of the quoting...>
 
"Real time" as in I initialize a large, empty array at the beginning of the program, then a function runs once to populate that array. Then that array can be called later in the program for various uses.



Just write
const unsigned long anim_seq[frames][scene] = { list of initialization values };

 
Unfortunately I currently cannot recall how the initialization list has to be sorted. IIRC, bracket the values for frames0 with '{' resp. '}', individual initialization values separated by commas. Where another opening bracket '{' follows, add a comma to the closing bracket '},'.
So for each framsX you'll have a sub-list { scene0, scene1, ... }  All exempt the last one have tol be closed by '},'
post edited by du00000001 - 2020/06/23 13:14:49

PEBKAC / EBKAC / POBCAK / PICNIC (eventually see en.wikipedia.org)
#20
Page: 12 > Showing page 1 of 2
Jump to:
© 2020 APG vNext Commercial Version 4.5