Intel® Quartus® Prime Software
Intel® Quartus® Prime Design Software, Design Entry, Synthesis, Simulation, Verification, Timing Analysis, System Design (Platform Designer, formerly Qsys)
Announcements
Intel Support hours are Monday-Fridays, 8am-5pm PST, except Holidays. Thanks to our community members who provide support during our down time or before we get to your questions. We appreciate you!

Need Forum Guidance? Click here
Search our FPGA Knowledge Articles here.
15336 Discussions

Variable declaration place question

Altera_Forum
Honored Contributor II
948 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
85 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".

Altera_Forum
Honored Contributor II
85 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.
Reply