CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Dec 2007
    Location
    Brazil
    Posts
    37

    [RESOLVED] Local arrays in methods

    A variable declared in a method gives me a place to store a value. If it is an array, it must be instanciated with new and is initialized to 0 by the compiler. Does the compiler do that at each call?
    The alternative is global declaration at class level. But a lot of those variables, only used in some small method somewhere, not even used to keep values between calls, would impair the readability of the program.

    So what would be good programming style for small, local arrays in methods?

  2. #2
    Join Date
    Mar 2005
    Location
    Vienna, Austria
    Posts
    4,538

    Re: Local arrays in methods

    Quote Originally Posted by hreba View Post
    A variable declared in a method gives me a place to store a value. If it is an array, it must be instanciated with new and is initialized to 0 by the compiler. Does the compiler do that at each call?
    The alternative is global declaration at class level. But a lot of those variables, only used in some small method somewhere, not even used to keep values between calls, would impair the readability of the program.

    So what would be good programming style for small, local arrays in methods?
    There is nothing against to create them at every method call as long as you do not need them outside this method.
    In contradiction I would say.: Never create any field for arrays, structures, values, classes outside of a method, when you only need them in the method. This can only bring up confusion and unreadable code
    Jonny Poet

    To be Alive is depending on the willingsness to help others and also to permit others to help you. So lets be alive. !
    Using Code Tags makes the difference: Code is easier to read, so its easier to help. Do it like this: [CODE] Put Your Code here [/code]
    If anyone felt he has got help, show it in rating the post.
    Also dont forget to set a post which is fully answered to 'resolved'. For more details look to FAQ's about Forum Usage. BTW I'm using Framework 3.5 and you ?
    My latest articles :
    Creating a Dockable Panel-Controlmanager Using C#, Part 1 | Part 2 | Part 3 | Part 4 | Part 5 | Part 6 | Part 7

  3. #3
    Join Date
    Mar 2004
    Location
    Prague, Czech Republic, EU
    Posts
    1,701

    Re: Local arrays in methods

    I back Johny's opinion.

    In addition to your question: if the variable (the value it holds) is local to the method, you have to always initialize it regardless it is defined as temporary in method, or "global" in class. Unless absolutely neccessary, e.g. profiler points you to specific rountine, always preferre maintability before performance. And what's more, if you declare the variable on class level, you need to take care about synchronization if multithreading comes to play. Method level temporary variables are easier to handle with in multihreading.
    • Make it run.
    • Make it right.
    • Make it fast.

    Don't hesitate to rate my post.

  4. #4
    Join Date
    Dec 2007
    Location
    Brazil
    Posts
    37

    Re: Local arrays in methods

    My original question referred to small arrays, that is a few bytes, and that case is clear now, thanks for the hints. On some of my projects however I work with images which are typically byte[1024*768] and I declare them globally. How would you handle these?

  5. #5
    Join Date
    Mar 2005
    Location
    Vienna, Austria
    Posts
    4,538

    Re: Local arrays in methods

    Quote Originally Posted by hreba View Post
    My original question referred to small arrays, that is a few bytes, and that case is clear now, thanks for the hints. On some of my projects however I work with images which are typically byte[1024*768] and I declare them globally. How would you handle these?
    This doen't depend on size. If that are Images they are classes and classes are reference Types. so your array never will cotain the pictures but only pointers to the real picture anywhere in the heap. Simple do it in an array which is declared at method level. If they need lots of loading time and you need to have them on screen very quick then you need to decide if you want to have a collection anywhere in the datalayer and you are maybe only accessing your datalayer then. So there is no 'general wrong' or 'always corrrect' solution. It depends on your design, how often the data needs to be accessed Loading time and all that. Maybe you are starting your program and while starting loading resources to a datalayer class in the background.

    I think, there are sme books on the market regarding C# design patterns. Maybe take a look in such stuff which will always be helpful when doing design decisions before starting to code a program. If you invest some time in these sort of knowledge you will spare lots of time which sometimes are wasted when you find out a design is not workable after investedsome days into it.
    Jonny Poet

    To be Alive is depending on the willingsness to help others and also to permit others to help you. So lets be alive. !
    Using Code Tags makes the difference: Code is easier to read, so its easier to help. Do it like this: [CODE] Put Your Code here [/code]
    If anyone felt he has got help, show it in rating the post.
    Also dont forget to set a post which is fully answered to 'resolved'. For more details look to FAQ's about Forum Usage. BTW I'm using Framework 3.5 and you ?
    My latest articles :
    Creating a Dockable Panel-Controlmanager Using C#, Part 1 | Part 2 | Part 3 | Part 4 | Part 5 | Part 6 | Part 7

  6. #6
    Join Date
    Dec 2007
    Location
    Brazil
    Posts
    37

    Re: Local arrays in methods

    My question is whether an array declared in a method is allocated and initialized at each call anew and whether this overhead might justify a global declaration for large arrays.

  7. #7
    Join Date
    Jun 2008
    Posts
    2,477

    Re: Local arrays in methods

    Quote Originally Posted by hreba View Post
    My question is whether an array declared in a method is allocated and initialized at each call anew and whether this overhead might justify a global declaration for large arrays.
    And the answer is no. Write code for clarity first. *If* you find you have a performance problem, then optimize,m but in this case that would not even be an issue.

  8. #8
    Join Date
    Mar 2005
    Location
    Vienna, Austria
    Posts
    4,538

    Re: Local arrays in methods

    Quote Originally Posted by BigEd781 View Post
    And the answer is no. Write code for clarity first. *If* you find you have a performance problem, then optimize,m but in this case that would not even be an issue.
    I simple have to confirm BigED781
    Jonny Poet

    To be Alive is depending on the willingsness to help others and also to permit others to help you. So lets be alive. !
    Using Code Tags makes the difference: Code is easier to read, so its easier to help. Do it like this: [CODE] Put Your Code here [/code]
    If anyone felt he has got help, show it in rating the post.
    Also dont forget to set a post which is fully answered to 'resolved'. For more details look to FAQ's about Forum Usage. BTW I'm using Framework 3.5 and you ?
    My latest articles :
    Creating a Dockable Panel-Controlmanager Using C#, Part 1 | Part 2 | Part 3 | Part 4 | Part 5 | Part 6 | Part 7

  9. #9
    Join Date
    Dec 2007
    Location
    Brazil
    Posts
    37

    Re: Local arrays in methods

    Ok, thanks guys for your hints.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured