Thread: Too many variables

  1. #1
    Wannabe Geek
    Join Date
    Aug 2004
    Posts
    19

    Too many variables

    How do i delete a variable.

    If I have a variable declared:

    Code:
    <data type> <variable name>;
    and if my prog is long and i have used the variable and dont require it any longer.
    How can i delete it, so as to free up memory and ensure that my prog is not a memmory hog.

    -Cheers.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You don't. You could use pointers I suppose, but you can't remove the pointer itself, only what it points to if you've actually allocated one. You're only going to get a savings there if you're allocating in blocks of N variables at a time (ie: arrays).

    The only other option, which isn't really "deleting" anything, is to limit the scope of the variable to as small a scope as possible. Example:
    Code:
    int main( void )
    {
        int i, j;
        for( i = 0; i < foo; i++ )
        {
            j = i + bar;
            ...stuff...
        }
        i = baz;
        stuffwithi( i );
    
        return 0;
    }
    If in the above code, j was only needed inside the loop, you could create it there instead of above. Crude example, but it illustrates the point:
    Code:
    int main( void )
    {
        int i;
        for( i = 0; i < foo; i++ )
        {
            int j = i + bar;
            ...stuff...
        }
        ...stuff...
    Thus, j only exists inside the loop. Be sure that what you need it for would work in said scenario though. But basicly that's the only way you can "delete" variables.

    Recap:
    1) Dynamic allocation, only useful for blocks of data rather than single instances.
    2) Scope limitation.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Watch for flying houses. Nessarose's Avatar
    Join Date
    Sep 2004
    Posts
    46
    And if you're using C99 you could include the int i in the for loop itself:

    Code:
    for (int i = 0 i < n; i++)

  4. #4
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Quote Originally Posted by quzah

    ...
    The only other option, which isn't really "deleting" anything, is to limit the scope of the variable to as small a scope as possible.
    ...

    There may be good stylistic reasons to limit the scope of variables, but I can't see how it would affect memory usage in a program.

    After all, the size of the code to use the variable and the memory requirements to store the variable are the same, regardless of whether it is declared inside a code block or outside of the code block (in the same function). When a variable goes out of scope, its name is no longer recognized by the compiler, but the run-time memory requirements are not changed.

    Regards,

    Dave

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I beg to differ. I may be wrong, but I'll still beg to differ. It's my understanding, that when a variable comes into scope, it's pushed on the stack, correct? Then when it goes out of scope, it's popped back off. Is this right?

    If it is, then when a variable goes out of scope, since that portion of the stack is now "free", you've just "freed" up some memory. Right? Otherwise recursion wouldn't overflow your stack. Because otherwise, if it just hung around, there would be no point at all in declaring items in a narrower scope. Everything may as well be at the top of every function. There would be no advantage, other than name reuse, which really isn't that great, to declaring variables right when you need them.

    If it isn't, well you'll see why I'm wrong.

    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    It depends on the compiler quzah. Many will allocate memory for all needed variables at the beginning of the function. However using limited scope still does help in limiting the amount of memory used. Example:
    Code:
    void func(void)
    {
      int i;
      /* ... */
      {
        int j;
        /* ... */
      }
      {
        int k;
        /* ... */
      }
    }
    In that sample function it only has to allocate for 2 ints since it can use the same memory location for both j and k.

  7. #7
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Quote Originally Posted by quzah
    I beg to differ. I may be wrong, but I'll still beg to differ. It's my understanding, that when a variable comes into scope, it's pushed on the stack, correct? Then when it goes out of scope, it's popped back off. Is this right?

    If it is, then when a variable goes out of scope, since that portion of the stack is now "free", you've just "freed" up some memory. Right? Otherwise recursion wouldn't overflow your stack. Because otherwise, if it just hung around, there would be no point at all in declaring items in a narrower scope. Everything may as well be at the top of every function. There would be no advantage, other than name reuse, which really isn't that great, to declaring variables right when you need them.

    If it isn't, well you'll see why I'm wrong.

    Quzah.
    Instead of proclaiming myself right (or wrong) and you wrong (or right), I would like to say that I believe that in a given function storage for a variable will exist regardless of whether the variable is delcared inside a given block or outside it. Storage for the variable is not allocated every time the block is entered and deallocate whenever the block is exited. Now, I haven't tried all of the C compilers in the world, but I have done it on a couple. I invite you to look at the assembly language output for calc() and calc2() in the following. For Borland bcc32 and GNU gcc, I got identical stack allocation sizes (at the beginning of the function, not within the block) and idendical code size.

    But maybe I'm wrong.

    Regards,

    Dave
    Code:
    #include <stdio.h>
    int main()
    {
      double calc(void);
      double calc2(void);
     
      double prod;
    
      prod = calc();
      printf("prod from calc  = %f\n", prod);
    
      prod = calc2();
      printf("prod from calc2 = %f\n", prod);
    
      return 0;
    }
    
    
    double calc(void)
    {
      double i;
      double retval = 0;
      for (i = 1; i < 5; i++) {
        double j;
        for (j = 1; j < 5; j++) {
          retval += i*j;
        }
      }
      return retval;
    }
    
    double calc2(void)
    {
      double i;
      double j;
      double retval = 0;
      for (i = 1; i < 5; i++) {
        for (j = 1; j < 5; j++) {
          retval += i*j;
        }
      }
      return retval;
    }
    Last edited by Dave Evans; 09-30-2004 at 11:28 AM.

  8. #8
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Quote Originally Posted by Thantos
    It depends on the compiler quzah. Many will allocate memory for all needed variables at the beginning of the function. However using limited scope still does help in limiting the amount of memory used. Example:
    Code:
    void func(void)
    {
      int i;
      /* ... */
      {
        int j;
        /* ... */
      }
      {
        int k;
        /* ... */
      }
    }
    In that sample function it only has to allocate for 2 ints since it can use the same memory location for both j and k.

    You are correct, of course. My example was too simple to show how compilers can reuse storage for variables declared inside local blocks. (Not the first time I have been wrong; not the first time I have learned something here.)

    I still hold my belief that storage is not allocated when entering a block and deallocated when leaving. (At least not for the compilers that I have access to right now.)

    Regards,

    Dave
    Last edited by Dave Evans; 09-30-2004 at 12:31 PM.

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Good info. Thanks all. So in theory, it could be like a union: Find the "minimum biggest" block that we need, and use it. Like Thantos suggested. If k was a char instead of an int, it would allocate 2 ints worth and just reuse the space, ala a union.

    But I suppose there's nothing "standard" on the way compilers do it. So one could do as Dave suggested, and allocate everything at once (which is probably the best way to do it), or it could, in theory, just allocate it when it needed it. Probably never happens that way. I was thinking for some reason that it just allocated it when it appeard in scope and disposed of it afterward.

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. basic question about global variables
    By radeberger in forum C++ Programming
    Replies: 0
    Last Post: 04-06-2009, 12:54 AM
  2. Replies: 15
    Last Post: 09-30-2008, 02:12 AM
  3. esbo's data sharing example
    By esbo in forum C Programming
    Replies: 49
    Last Post: 01-08-2008, 11:07 PM
  4. Replies: 6
    Last Post: 01-02-2004, 01:01 PM
  5. functions to return 2 variables?
    By tim in forum C Programming
    Replies: 5
    Last Post: 02-18-2002, 02:39 PM