Another Dynamic Memory Question

This is a discussion on Another Dynamic Memory Question within the C++ Programming forums, part of the General Programming Boards category; According to the book I read on C++, variables in dynamic memory are availiable to functions other than the one ...

  1. #1
    Rabite SirCrono6's Avatar
    Join Date
    Nov 2003
    Location
    California, US
    Posts
    269

    Red face Another Dynamic Memory Question

    According to the book I read on C++, variables in dynamic memory are availiable to functions other than the one it was defined in... take this code:
    Code:
    #include <iostream>
    
    class Hello
    {
      public:
        int ello;
        Hello() { Hello = 10; }
        ~Hello() {}
    };
    
    void Func()
    {
      std::cout << h->ello;
    }
    
    int main( void )
    {
      Hello* h = new Hello;
      Func();
      delete h;
      return 0;
    }
    Dev-C++ gives me the error that in Func() h is undeclared... so I'm thinking my understanding of dynamic memory (my book) is wrong, and I would like to know what is wrong about it.

    - SirCrono6

    P.S. Googled and board searched
    From C to shining C++!

    Great graphics, sounds, algorithms, AI, pathfinding, visual effects, cutscenes, etc., etc. do NOT make a good game.
    - Bubba

    IDE and Compiler - Code::Blocks with MinGW
    Operating System - Windows XP Professional x64 Edition

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    According to the book I read on C++, variables in dynamic memory are availiable to functions other than the one it was defined in
    Yes, that's true, however your application of that principle is faulty. Normally, an object that is declared inside a function is destroyed when that function ends. The implication is: if you return a pointer to an object that was declared inside a function, the object will have been destroyed, and the pointer won't be able to access that object.

    If instead, you use the new operator to create the object inside the function, the object won't be destroyed when the function finishes executing, so if you return a pointer to that object, you will still be able to access the object. However, that does not mean some other function knows anything about the object. The only way another function can refer to the object is if you pass it the pointer. Then, it can use the pointer to access the object.

    You also have have another error in your code: a constructor initializes the members of an object, and "Hello" is not a member variable.

    To demonstrate the difference when using 'new', call a function from main(), and inside the function create an object using new and return a pointer to that object. Then, call another function and send it the pointer, and use the pointer to display a member variable.

    After that, try the same thing without using the new operator. Instead, just declare an object, declare a pointer, assign the address of the object to the pointer, and return the pointer.
    Last edited by 7stud; 03-01-2005 at 11:37 PM.

  3. #3
    Rabite SirCrono6's Avatar
    Join Date
    Nov 2003
    Location
    California, US
    Posts
    269
    Ah... thank you.

    >"Hello" is not a member variable.
    I did not notice that... um that isn't the code I was using.

    - SirCrono6
    From C to shining C++!

    Great graphics, sounds, algorithms, AI, pathfinding, visual effects, cutscenes, etc., etc. do NOT make a good game.
    - Bubba

    IDE and Compiler - Code::Blocks with MinGW
    Operating System - Windows XP Professional x64 Edition

  4. #4
    Registered User
    Join Date
    Sep 2004
    Posts
    197
    What they guy above said is correct. here is an example

    Code:
    #include <iostream>
    int func(int *ptr)
    {
        delete ptr;
        ptr = new int;
        *int = 1;
        return 1; // so we can check what its been set to in main
    }
    
    int main()
    {
        int *blah;
        int test = func(blah);
        if(test = *blah)
        {
          std::cout << "You should see this\n";
        }
        else
        {
          std::cerr << "Not this\n";
        }
        return 0;
    }
    If any part of my post is incorrect, please correct me.

    This post is not guarantied to be correct, and is not to be taken as a matter of fact, but of opinion or a guess, unless otherwise noted.

  5. #5
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,702
    >> here is an example
    ...of code that doesn't compile and wouldn't work even if it did.

    • blah is uninitialized so delete is being passed an uninitialized value.
    • ptr is not a reference paramereter and thus the new'd memory is leaked and blah's value never changes.
    • "*int = 1;"
    • "if(test = *blah)" If you get away with the dereferencing of uninitialized memory, then you will "see this".


    Code:
    #include <iostream>
    using namespace std;
    
    int func(int *&ptr)
    {
        delete ptr;
        ptr = new int(1);
        return *ptr;
    }//func
    
    int main()
    {
        int *blah = 0;
        int test = func(blah);
    
        if (test == *blah)
        {
            cout << "You should see this" << endl;
        }
        else
        {
            cerr << "Not this" << endl;
        }
    
        delete blah;
    
        return 0;
    }//main
    gg

  6. #6
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    The dynamically allocated memory is accessible from outside, however your actual pointer that points at that memory is not!
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  7. #7
    Registered User
    Join Date
    Sep 2004
    Posts
    197
    Quote Originally Posted by Codeplug
    >> here is an example
    ...of code that doesn't compile and wouldn't work even if it did.

    • blah is uninitialized so delete is being passed an uninitialized value.
    • ptr is not a reference paramereter and thus the new'd memory is leaked and blah's value never changes.
    • "*int = 1;"
    • "if(test = *blah)" If you get away with the dereferencing of uninitialized memory, then you will "see this".

    gg
    Thanks for the fix. My head was a little light at the time (needed sleep, but couldn't get to sleep), and I didn't try to compile it at all, just typed it out.
    If any part of my post is incorrect, please correct me.

    This post is not guarantied to be correct, and is not to be taken as a matter of fact, but of opinion or a guess, unless otherwise noted.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Assignment Operator, Memory and Scope
    By SevenThunders in forum C++ Programming
    Replies: 47
    Last Post: 03-31-2008, 07:22 AM
  2. Question regarding Memory Leak
    By clegs in forum C++ Programming
    Replies: 29
    Last Post: 12-07-2007, 01:57 AM
  3. Newbie question: pointers, other program's memory
    By xxxxme in forum C++ Programming
    Replies: 23
    Last Post: 11-25-2006, 01:00 PM
  4. 2D array in dynamic memory
    By VanJay011379 in forum C++ Programming
    Replies: 3
    Last Post: 03-16-2003, 08:34 PM

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