Thread: Function creates memory space and returns reference to class

  1. #1
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681

    Function creates memory space and returns reference to class

    What I want to do is have a function create the memory space outside of the stack for a class and then return the object via a reference. Is there any problem using the below method?

    Code:
    #include <iostream>
    
    class test {
      public:
      test *ptr;
      int x;
      test(test *x){ ptr = x; }
      ~test() { delete ptr; }
    };
    
    test & func1(void);
    
    int main(void)
    {
      test &x = func1();
    
      std::cout<<x.x<<std::endl;
      std::cout<<x.ptr<<std::endl;
    }
    
    test & func1(void)
    {
      test *x = new test(x);
    
      x->x = 12;
    
      return *x;
    }
    It compiles and runs fine. Just wondering if there is any potential problems I should be aware of. Thanks

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Code:
    test *x = new test(x);
    You can't do that. You are trying to use x before it has been defined. Why are you trying to do this? What are you trying to accomplish, maybe there is a better way.
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  3. #3
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Basically what I'm trying to do is to have a function create the space, load the data into the class (it will be a friend function), and return it as a reference to the caller. Since it will be by reference I don't think I can use delete on it so I need to declare a destructor that will do it for me.

    As to the example above: Doesn't it depend on the order in which things are invoked? I would think the new would be done first, assigned to x, and then passed into the constructor.

  4. #4
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    I still don't understand why you would want to do what you are trying to do. As for the above example, you call new and it will construct the object with the parameter to the constructor and then assign that to x.
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  5. #5
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Look at the first post. I don't want the memory space to be removed until main()'s x variable dies. I want to pass it by reference so that I don't have to keep dereferencing a pointer each time I want to use x. I don't want to return it by copy because thats wasteful.

    As for the order, the asm code shows different but that could just be g++.

  6. #6
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Well, I know on VC++ .NET 2003 you will get a run-time error saying you are trying to use the variable before it's been defined. I also know it's pretty compliant as far as the standard goes, but who knows. I always thought it was the other way but I could be wrong.

    What about using an auto_ptr ? You would have to dereference still but it would automatically be deallocated when it goes out of scope.

    The problem with yours, and it may or may not be a problem for what you need it for, is you can't dynamically allocate an array the way you have it.
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  7. #7
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Ok did another test
    test *x=NULL;
    x= new test(x);
    and sure enough its doing what you say. thanks

    In fact the destructor is never called. when x dies. I'll have to think of another method to do this. Thanks for the help

  8. #8
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Ah dang. Looked at my book and as far as assigning the pointer goes it couldn't be easier.

    Code:
    test() { ptr = this; }

  9. #9
    Registered User
    Join Date
    May 2003
    Posts
    161
    To add on to what MrWizard mentioned, std::auto_ptr would probably best suit your needs.

    Code:
    #include <iostream>
    #include <memory>
    
    class test {
      public:
        int x;
    };
    
    std::auto_ptr<test> func1(void);
    
    int main(void)
    {
      std::auto_ptr<test> x = func1();
    
      std::cout<<x->x<<std::endl;
    
      // test object is properly deleted at end of scope
    }
    
    std::auto_ptr<test> func1(void)
    {
      std::auto_ptr<test> x(new test);
      x->x = 12;
      return x;
    }

  10. #10
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    No I reference would best suit my wants, an auto_ptr might just be want I have to use though

    Also took it a step further and realized that there was no need to store the address and this is always avilable to me and can be used on the delete.
    Last edited by Thantos; 06-05-2004 at 07:25 PM.

  11. #11
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Ok I went with auto_ptr. While its not exactly what I wanted it will do just fine. Thanks for the help you two.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  2. Manipulating the Windows Clipboard
    By Johno in forum Windows Programming
    Replies: 2
    Last Post: 10-01-2002, 09:37 AM
  3. Memory handler
    By Dr. Bebop in forum C Programming
    Replies: 7
    Last Post: 09-15-2002, 04:14 PM
  4. Tab Controls - API
    By -KEN- in forum Windows Programming
    Replies: 7
    Last Post: 06-02-2002, 09:44 AM
  5. pointers, functions, parameters
    By sballew in forum C Programming
    Replies: 3
    Last Post: 11-11-2001, 10:33 PM