Warning: Function ereg() is deprecated in ..../includes/class_postbit.php(345) : eval()'d code on line 4

Warning: Function split() is deprecated in ..../includes/class_postbit.php(345) : eval()'d code on line 19

Warning: Function ereg() is deprecated in ..../includes/class_postbit.php(345) : eval()'d code on line 4

Warning: Function split() is deprecated in ..../includes/class_postbit.php(345) : eval()'d code on line 19

Warning: Function ereg() is deprecated in ..../includes/class_postbit.php(345) : eval()'d code on line 4

Warning: Function split() is deprecated in ..../includes/class_postbit.php(345) : eval()'d code on line 19

Warning: Function ereg() is deprecated in ..../includes/class_postbit.php(345) : eval()'d code on line 4

Warning: Function split() is deprecated in ..../includes/class_postbit.php(345) : eval()'d code on line 19

Warning: Function ereg() is deprecated in ..../includes/class_postbit.php(345) : eval()'d code on line 4

Warning: Function split() is deprecated in ..../includes/class_postbit.php(345) : eval()'d code on line 19
c++ variable scope
Results 1 to 5 of 5

Thread: c++ variable scope

  1. #1
    Senior Member
    Join Date
    Jul 2001
    Posts
    280

    c++ variable scope

    If you declare a variable in a function
    Code:
    void myfunc (){
     * * *int a = 33;
    }
    that variable (in this case [tt]a[/tt]) only exists throughout the duration of the function and is then wiped off the stack.

    I assume it works the same way with objects
    Code:
    void myfunc(){
     * * *MyObject obj;
    }
    [tt]obj[/tt] only exists on the stack throughout the function.

    My question is what happens here:
    Code:
    void myfunc(){
     * * *MyObject *obj = new MyObject;
    }
    The [tt]new[/tt] operator creates the object on the heap, so the object remains after the function is over, right? *But wouldn't the pointer to that object be removed along with all other local variables when the function ends? *So this object would be a memory leak, right? *

  2. #2

    Re: c++ variable scope



    The [tt]new[/tt] operator creates the object on the heap, so the object remains after the function is over, right? But wouldn't the pointer to that object be removed along with all other local variables when the function ends? So this object would be a memory leak, right?
    That is correct.

  3. #3
    Senior Member
    Join Date
    Jul 2001
    Posts
    280

    Re: c++ variable scope

    Ok, so this should work:

    Code:
    void myfunc(){
    
     * * *MyObject *obj = getObj ();
    }
    
    MyObject *getObj (){
     * * *
     * * *MyObject *anotherObj = new MyObject;
     * * *return anotherObj;
    }
    The object is created in the [tt]getObj[/tt] function and continues to exist when the function is over and the pointer is returned to the [tt]myfunc[/tt] function. *This wouldn't cause any errors, right?

  4. #4

    Re: c++ variable scope



    The object is created in the [tt]getObj[/tt] function and continues to exist when the function is over and the pointer is returned to the [tt]myfunc[/tt] function. This wouldn't cause any errors, right?
    Well, when myfunc() returns, all pointers to the object will have expired, and there will be a leak, but other than that it looks good.

  5. #5
    Senior Member
    Join Date
    Jul 2001
    Posts
    280

    Re: c++ variable scope


    Well, when myfunc() returns, all pointers to the object will have expired, and there will be a leak, but other than that it looks good.
    Oh, yes, of course. I was referring to the getobj function.

Similar Threads

  1. set mysql variable using command
    By svar in forum Windows - General Topics
    Replies: 0
    Last Post: 01-02-2009, 05:53 PM
  2. setting PATH environment variable
    By omidkamangar in forum Linux - General Topics
    Replies: 2
    Last Post: 04-20-2007, 11:10 PM
  3. Pass a variable for one script to another in python
    By AljoshaNL in forum Linux - Software, Applications & Programming
    Replies: 3
    Last Post: 02-24-2006, 01:12 PM
  4. errno variable problem
    By BadDudes in forum Linux - General Topics
    Replies: 2
    Last Post: 02-18-2005, 10:12 AM
  5. Add to Evniron Variable?
    By WarLockLord in forum Linux - Software, Applications & Programming
    Replies: 9
    Last Post: 07-14-2002, 09:28 AM

Bookmarks

Posting Permissions

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