Thread: Question about memory space

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    147

    Question about memory space

    Look at this code

    Code:
    my_tmp_function() {
       int array1[4096]
       long array2[other big size]
       ... a few more big arrays...
    }
    I have a similar code in my program. At runtime, memory is a factor because the program uses lots of hash tables, precomputed values and so on. The above function only run from time to time (I can call many times the program and it does not run it). My question is: if at runtime above code is not invoked, does the function take so a big space at memory, or is only filled when call the function?

    thanks.

  2. #2
    Registered User
    Join Date
    May 2014
    Posts
    121
    Non-static local variables only occupy memory when the function is running since they are allocated on the stack. You should be concerned about stack overflow though if you're allocating huge arrays.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    If your local variables have automatic storage duration (i.e., none of them are declared static), then memory for them is not actually allocated until the function is called.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Max Out Virtual Memory Space
    By newbe in forum Windows Programming
    Replies: 0
    Last Post: 05-06-2010, 10:46 AM
  2. How to Map a Memory Space
    By jacob12 in forum C Programming
    Replies: 6
    Last Post: 10-04-2008, 10:19 PM
  3. Process memory space
    By barboris in forum C++ Programming
    Replies: 6
    Last Post: 04-25-2008, 11:35 PM
  4. Ideal memory space?
    By franziss in forum C++ Programming
    Replies: 5
    Last Post: 10-30-2005, 11:15 PM
  5. Amount of memory space need?
    By jnwk888hwq in forum C Programming
    Replies: 8
    Last Post: 10-25-2003, 02:55 PM