|
-
Jul 14th, 2003, 06:29 PM
#1
Thread Starter
Hyperactive Member
variables and loops...
if i have a loop and an if...then statement inside that loop with a variable being dimmed inside the if...then, will that waste memory?
-
Jul 14th, 2003, 07:42 PM
#2
Sleep mode
If this is similar to what you are trying to do , then Yes , it's waste of memory . Here , in below example , the variable 'aa' will be dimmed 5 times if the first condition was met otherwise nothing happen .
VB Code:
Dim i As Integer
Dim a As String = "a", b As String = "b"
For i = 0 To 5
If a = "a" Then
Dim aa As String = "a"
'do other stuff
ElseIf b = "b" Then
'do something
End If
Next
-
Jul 14th, 2003, 08:01 PM
#3
Thread Starter
Hyperactive Member
differnet in vb.net?
i thought it was different in .net where the variable was destroyed once it's container finished, for..loop, if..then, procedure... guess not. thanks though
-
Jul 14th, 2003, 08:57 PM
#4
Actual gmatteson you are correct the variable scope is only valid inside the If statement. Although it will be initialized 5 times or with each loop, but it will also be destroyed in that loop. So if you only need it for something inside that if..then then that is the proper way to do it.
-
Jul 14th, 2003, 10:02 PM
#5
Sleep mode
Hmm , still wasting memory .
-
Jul 15th, 2003, 12:28 AM
#6
Thread Starter
Hyperactive Member
-
Jul 15th, 2003, 12:52 AM
#7
Well it doesn't waste memory because it only creates the the variable or takes the memory if the IF statement is true and then it uses it and then destroys it. It is most efficent since no memory is used if not needed and instead of 5 there is one 1 at a time. If you created it outside of the then it would be used regardless of whether or not it was needed.
-
Jul 15th, 2003, 07:02 AM
#8
Fanatic Member
But say the GC doesn't collect the objects until after the looping has completed, then you would still have the memory allocated until that point. Or at least, the GC has more work to do.
Hope I don't start another GC debate
-
Jul 15th, 2003, 07:17 AM
#9
Sleep mode
Originally posted by VBCrazyCoder
But say the GC doesn't collect the objects until after the looping has completed, then you would still have the memory allocated until that point. Or at least, the GC has more work to do.
Hope I don't start another GC debate
And say loop is long , still wasting memory in my view point . This doesn't mean you guys are wrong but as I know , it's bad practice to dim variables within loops and IFs with some exceptions of course .
[i]
Hope I don't start another GC debate  [/B]
lol.......
-
Jul 15th, 2003, 02:47 PM
#10
Wouldn't a locally declared variable like that be created on the stack? If that is the case, there is no waste of memory, since the memory is already allocated (this might be true even without the stack), and the object goes away when the function or the loop exits.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|