Results 1 to 5 of 5
  1. #1

    Declaring variables

    Does declaring variable inside a function give an extra overhead on an application? Would it be better to declare the variable globally and just reuse it?

    Example
    Code:
    #include <blah>
    
    char mybuffer[2048];
    
    int main()
    {
    
     while true
     {
       myfunction();
       //call my function many times
     }
    
    }
    
    int myfunction()
    {
      //do something with mybuffer
    }

    VS this

    Code:
    #include <blah>
    
    int main()
    {
    
     while true
     {
       myfunction();
       //call my function many times
     }
    
    }
    
    int myfunction()
    {
      
      char mybuffer[2048];
    
      //do something with mybuffer
    }
    The only difference is the declaration of my variable. Since myfunction() will be called many times will it add an additional overhead if it will create mybuffer[2048] over and over?

  2. #2
    Join Date
    Jul 2008
    Location
    Atlanta GA
    Posts
    293
    This much less about overhead than scope. If can declare the variable as global, but then any function can see the variable and it will not die until the program dies. This might seem better, but then if the variable is in use by one function, you can't call other function that also wants to use that variable without discarding the value and corrupting the data of the other function.

    If you declare a variable in the function, then it's local to function only and can't be accessed by any other function. This is considered the better way and more secure.

    If you're worried about it, try compiling the program both ways and timing it to see which is faster. But I think most modern compilers take this into account when building the symbol table. You might find they preallocate space for variables they determine will see a lot of use.

  3. #3
    Join Date
    Aug 2009
    Posts
    36
    Quote Originally Posted by VPSForge-Ray View Post
    The only difference is the declaration of my variable. Since myfunction() will be called many times will it add an additional overhead if it will create mybuffer[2048] over and over?
    Local variables are allocated on the stack, global variables are allocated on the heap. Allocating on the stack is (almost) always much faster than the heap as it's basically just one instruction to move the stack pointer.

    But unless you are calling this function more than anything else in your code, you probably don't have to worry about performance as a consideration for where you allocate your variables. You can instead worry about security and bugs, in which case you should follow best practices and (almost) never use a global variable unless you have an explicit need to do so. Keep your variables to the most restricted scope you need and you'll save yourself a lot of headache.

  4. #4
    thanks it helps

  5. #5
    This is good programming about declaration variable. Declare your variables near the beginning of your program, just after the line with the initial curly bracket. Cluster them all up right there. Each variable needs an identifier that distinguishes it from the others.

Similar Threads

  1. javascript variables
    By hopesfall in forum Programming Discussion
    Replies: 4
    Last Post: 06-20-2008, 03:47 PM
  2. Passing variables as variables
    By raulgonzalez in forum Programming Discussion
    Replies: 15
    Last Post: 04-26-2006, 09:13 AM
  3. Help with PHP variables
    By TablesandChairs4 in forum Programming Discussion
    Replies: 9
    Last Post: 11-20-2004, 07:52 PM
  4. Problems with declaring a function
    By Icheb in forum Programming Discussion
    Replies: 8
    Last Post: 03-11-2003, 04:39 PM

Posting Permissions

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