Author |
Topic: Local DIM (Read 391 times) |
|
Matt
Developer
member is offline


Gender: 
Posts: 210
|
 |
Local DIM
« Thread started on: May 25th, 2013, 06:29am » |
|
Hi,
In a procedure, what's the difference between:
1. Code:LOCAL text%
DIM text% 255
2. Code:LOCAL [none]
DIM text% LOCAL 255
Would this normally accomplish the same thing?
Matt
|
|
Logged
|
|
|
|
admin
Administrator
member is offline


Posts: 1145
|
 |
Re: Local DIM
« Reply #1 on: May 25th, 2013, 07:37am » |
|
on May 25th, 2013, 06:29am, Matt wrote: Code:LOCAL text%
DIM text% 255 Code: Would this normally accomplish the same thing? |
|
No, the two uses of LOCAL are completely different; they just happen to share the same keyword. LOCAL text% makes the variable text% local, DIM text% LOCAL 255 makes the allocated memory local, i.e. it allocates it from the stack rather than from the heap. As a rule you would want to make the variable local as well, so typically you'd have:
Code: LOCAL text% : REM Makes text% local
DIM text% LOCAL 255 : REM Allocates memory from the stack Richard.
|
|
Logged
|
|
|
|
Matt
Developer
member is offline


Gender: 
Posts: 210
|
 |
Re: Local DIM
« Reply #2 on: May 25th, 2013, 1:12pm » |
|
on May 25th, 2013, 07:37am, Richard Russell wrote:DIM text% LOCAL 255 makes the allocated memory local, i.e. it allocates it from the stack rather than from the heap. |
|
As they both work, and I would assume that they are both acceptable, why would you rather have the memory allocated from the stack? Is there a potential problem, or is just good practice?
Matt
|
|
Logged
|
|
|
|
admin
Administrator
member is offline


Posts: 1145
|
 |
Re: Local DIM
« Reply #3 on: May 25th, 2013, 5:29pm » |
|
on May 25th, 2013, 1:12pm, Matt wrote:Is there a potential problem, or is just good practice? |
|
There's a massive potential problem - a memory leak! As a rule you never have a choice of when to allocate memory from the heap or the stack; for example if you're allocating memory inside a PROC or FN it will generally be essential to allocate it from the stack. A memory leak is the very last thing you want because it's so insidious - it may not be noticed until your program eventually crashes maybe days or months later!
Richard.
|
|
Logged
|
|
|
|
|