Quick Search for:  in language:    
flaw,concerning,static,arrays,return,values,f
   Code/Articles » |  Newest/Best » |  Community » |  Jobs » |  Other » |  Goto » | 
CategoriesSearch Newest CodeCoding ContestCode of the DayAsk A ProJobsUpload
Visual Basic Stats

 Code: 3,011,557. lines
 Jobs: 115. postings

 How to support the site

 
Sponsored by:

 

You are in:

 
Login



Latest Code Ticker for Visual Basic.
Locate Database
By Erica Ziegler-Roberts on 6/30


Organize the errors of your programs
By Toni on 6/30


how to open and close access database
By Freebug on 6/29


Click here to see a screenshot of this code!PSC-Browser
By Ralph LONG Metz on 6/29

(Screen Shot)

Click here to see a screenshot of this code!Quadratic Solver 2
By Guillaume Couture-Levesqu e on 6/29

(Screen Shot)

Click here to see a screenshot of this code!Array Example
By Cold Fire on 6/29

(Screen Shot)

Click here to see a screenshot of this code!Reconstructor 3.0
By Peter Scale on 6/29

(Screen Shot)

Click here to see a screenshot of this code!Subtitles Manager
By KarahaNa on 6/29

(Screen Shot)

XPlorer
By ZProse on 6/29


Click here to put this ticker on your site!


Add this ticker to your desktop!


Daily Code Email
To join the 'Code of the Day' Mailing List click here!





Affiliate Sites



 
 
   

Static Arrays as Return Values (Reformatted)

Print
Email
 

Submitted on: 6/16/2003 11:44:01 PM
By: Sudif 
Level: Intermediate
User Rating: Unrated
Compatibility:VB 6.0

Users have accessed this article 522 times.
 
(About the author)
 
     <P> Tip about a flaw in VB concerning static arrays as return values from function calls. </P> <P> It turns out that the original submission was almost illegible. Actually, it was nicely formatted when I pasted it into the submission form; I didn't know it would look that ghastly in a web page. Hope it'll be better this time around. </P>

 
 
Terms of Agreement:   
By using this article, you agree to the following terms...   
1) You may use this article in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.   
2) You MAY NOT redistribute this article (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
3) You may link to this article from another website, but ONLY if it is not wrapped in a frame. 
4) You will abide by any additional copyright restrictions which the author may have placed in the article or article's description.

Static Arrays as Return Values.


There's a common trick when returning an array from a function: to assign the array to the function name in the very last statement before leaving the function (i.e. just before "Exit Function" or "End Function"). This makes VB switch pointers rather than copy the local array to the return value. The local copy is left empty but that doesn't matter because it's guaranteed not to be used (or even exist) afterwards since we immediately exit the function after the statement. Thus this function



Public Function MyArrayFunc() As Long()

Dim lstRet() As Long

lstRet = InitArray

DoSomething

MyArrayFunc = lstRet ' Last statement: DOESN'T COPY local array.
End Function

will be faster than



Public Function MyArrayFunc() As Long()

Dim lstRet() As Long

lstRet = InitArray

MyArrayFunc = lstRet ' Not last statement: DOES COPY local array.

DoSomething

End Function

So that's a good thing, then. It's not exactly intuitive but you get a speed increase by the simple measure of assigning the return array in the last statement, which in most cases makes perfect sense anyway.



Now for a not so good thing: VB TREATS STATIC ARRAYS THE SAME WAY.
This function will work the first time around but return an empty array from all subsequent calls:



Public Function MyStaticArrayFunc() As Long()

Static lstRet() As Long
Static bInit As Byte

If bInit = 0 Then
lstRet = InitArray
bInit = 1
End If

MyStaticArrayFunc = lstRet ' Last statement: DOESN'T COPY local array EVEN THOUGH IT'S STATIC!
End Function

The "static" array is initialized and then emptied in the first call.
To avoid this we need to ensure that there's at least one statement, any statement, between "MyArrayFunc = lstRet" and the function exit. In the above example there are no other calls to be made so instead "bInit = 1" could be placed after the return value assignment:



Public Function MyStaticArrayFunc() As Long()

Static lstRet() As Long
Static bInit As Byte

If bInit = 0 Then
lstRet = InitArray
End If

MyStaticArrayFunc = lstRet ' Not last statement: DOESN'T COPY local array.

bInit = 1 ' Looks unnecessary, should be unnecessary, but isn't.
End Function

This is another annoying VB hack (like we didn't have enough of those). Assigning 1 to bInit should of course not be necessary except in the first call but that superfluous assignment is a small price to pay for keeping the static array truly static.


Other 1 submission(s) by this author

 

 
Report Bad Submission
Use this form to notify us if this entry should be deleted (i.e contains no code, is a virus, etc.).
Reason:
 
Your Vote!

What do you think of this article(in the Intermediate category)?
(The article with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor See Voting Log
 
Other User Comments
6/17/2003 7:58:49 AM:Coding Genius
My eyes started hurting as soon as I tried to read the code. Just looks like a big rabble. Try to format it so that it's actually half readable.
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
6/17/2003 9:16:32 AM:Sudif
It doesn't look very nice now either but at least it's better than before.
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
Add Your Feedback!
Note:Not only will your feedback be posted, but an email will be sent to the code's author in your name.

NOTICE: The author of this article has been kind enough to share it with you.  If you have a criticism, please state it politely or it will be deleted.

For feedback not related to this particular article, please click here.
 
Name:
Comment:

 

Categories | Articles and Tutorials | Advanced Search | Recommended Reading | Upload | Newest Code | Code of the Month | Code of the Day | All Time Hall of Fame | Coding Contest | Search for a job | Post a Job | Ask a Pro Discussion Forum | Live Chat | Feedback | Customize | Visual Basic Home | Site Home | Other Sites | About the Site | Feedback | Link to the Site | Awards | Advertising | Privacy

Copyright© 1997 by Exhedra Solutions, Inc. All Rights Reserved.  By using this site you agree to its Terms and Conditions.  Planet Source Code (tm) and the phrase "Dream It. Code It" (tm) are trademarks of Exhedra Solutions, Inc.