Intel® Quartus® Prime Software
Intel® Quartus® Prime Design Software, Design Entry, Synthesis, Simulation, Verification, Timing Analysis, System Design (Platform Designer, formerly Qsys)
16637 Discussions

Variable declaration place question

Altera_Forum
Honored Contributor II
1,189 Views

the FPGA resource allocation will be different if the variable declaration in different place? 

 

 

//declare the variable i, j, k not in the loop 

int i; 

int j; 

int k; 

 

 

for (...) 

for (...) 

//declare the variable i, j, k in loop 

int i; 

int j; 

int k; 

 

 

 

 

Thanks
0 Kudos
2 Replies
Altera_Forum
Honored Contributor II
326 Views

It certainly does, please read "Intel FPGA SDK for OpenCL Best Practices Guide, Section 1.6.3, Declare Variables in the Deepest Scope Possible".

0 Kudos
Altera_Forum
Honored Contributor II
326 Views

Variables declared outside the loops become Global and are visible to the entire code/project. This will mean the memory allocated for the variable will not be released once it is not used. Global variables will tend to keep their memory allocations as long as the project / program is active in memory this taking up a lot of memory space. This is bad design for Embedded designs as Embedded systems have limited RAM.  

 

Its always best to declare variables locally where they tend to be used and then free them once they are out of scope. Only use global variables if needed. This way your code will take up the least memory footprint and can fit into the program memory or the on-chip ROM/flash.
0 Kudos
Reply