do variables die in function

This is a discussion on do variables die in function within the C++ Programming forums, part of the General Programming Boards category; if varianbles are declared within a function, do they die when the function finish?...

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    30

    do variables die in function

    if varianbles are declared within a function, do they die when the function finish?

  2. #2
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    unless they are static then yes.

  3. #3
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    If you allocate memory for them with new, then the memory is still valid after the function ends and doesn't die until you delete it.

  4. #4
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    The memory space will still be avialable until you delete it, however any local pointers you have in the function will be destroyed when the function ends, so unless you are saving the pointer somehow (return value or static) then that memory space is effectivaly lost.

  5. #5
    Registered User
    Join Date
    Mar 2004
    Posts
    30
    ok, i see how it works. i was just working with random numbers and i came up with another question. lkook at my code:

    Code:
     #include <iostream>
    #include <conio.h>
    
    using namespace std;
    
    int generate_numbers(int low, int high)
    {
        time_t seconds;
        time (&seconds);
        return (rand() % (high - low + 1) + low);
    }
    
    int main()
    {
        int number[10];
        int i;
        for (i = 0; i < 10; i++)
            number[i] = generate_numbers(1, 100);
        for (i = 0; i < 10; i++)
            cout << number[i] << endl;
        getch();
        return 0;
    }
    i have a function thats supposed to generate random numbers and reutnr them, but everytime i run the program, i get the same result. why isn't this random?

  6. #6
    essence of digital xddxogm3's Avatar
    Join Date
    Sep 2003
    Posts
    589
    I have run experiments on an xp machine.
    I ran a program with initialization and declaration of a variable inside a function.
    Local scope without being declared as static.
    I monitored the memory usage per the system log.
    The memory was still being used by the system after the exit of the function.
    It didn't release the memory back to the system resource pool until after the program itself was dead. I think the adress that keeps track of the memory is dead after the function ends, but the memory itself is still concidered active in the program until the program itself dies. This could all be system dependent though. Windows does some weird things sometimes.
    "Hence to fight and conquer in all your battles is not supreme excellence;
    supreme excellence consists in breaking the enemy's resistance without fighting."
    Art of War Sun Tzu

  7. #7
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Ok are you getting different numbers for each call?

    The reason you would get the same set of numbers each time you run the program is because you aren't initalizing the random number generator.

    Search the board and you'll find about a million posts about it.

  8. #8
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie class question
    By vastgoten in forum C++ Programming
    Replies: 4
    Last Post: 07-31-2008, 02:43 AM
  2. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 12:45 PM
  3. dllimport function not allowed
    By steve1_rm in forum C++ Programming
    Replies: 5
    Last Post: 03-11-2008, 04:33 AM
  4. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM
  5. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21