Thread: pointer to a class member

  1. #1
    Just kidding.... fnoyan's Avatar
    Join Date
    Jun 2003
    Location
    Still in the egg
    Posts
    275

    pointer to a class member

    Hi

    I have a class and I declared a pointer to hostent structure in the class. The value of pointer is assigned in the constructor (upon returing from a function). The problem is that all the instances of my class use the same memory address for this structure. I examined the case with gdb and saw that all hostent* point the same memory location. That's why all values of hostent hold for one instance of class are overwritten by another instance. How can I fix this?

    Code:
    class MyClass 
    {
     public:
      .... 
     private:
      hostent *h;
    };
    
    MyClass::MyClass()
    {
    ...
     h= gethostbyname();
    ...
    }
    
    int main()
    {
       MyClass obj1("aaa");
       MyClass obj2("bbb");
    // the *h of obj1 and *h of obj2 point the
    // same memory location at this point!
       retrurn 0;
    }
    Last edited by fnoyan; 10-30-2006 at 04:04 PM.

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Don't use a pointer. Store a copy of the information in your class.

  3. #3
    Just kidding.... fnoyan's Avatar
    Join Date
    Jun 2003
    Location
    Still in the egg
    Posts
    275
    this requires definiton of a lot of variables! :{

    is this the case in C++, i mean all the class instances uses the same pointer to access a member (if it is defined as a pointer). This resembles using static variables...

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    No, it is probably because gethostbyname has its own static pointer, and it expects you to get the data before you call it again.

    You don't need a ton of variables, you can just use a class or struct. In fact, you could probably just use a hostent object.

  5. #5
    Just kidding.... fnoyan's Avatar
    Join Date
    Jun 2003
    Location
    Still in the egg
    Posts
    275
    Quote Originally Posted by Daved
    No, it is probably because gethostbyname has its own static pointer, and it expects you to get the data before you call it again.
    So, i should save the returned values as soon as the function returns, right?

    Maybe defining a hostent class is a simple way oy saving the values (instead of declaring lots of variables)

    What about defining a local hostent structure and copying the values from hostent*. Are those values copied really copied (i mean copied to another memory address), or they are just assigned addresses that points the fileds from hostent* ?

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    If you have a hostent member variable, and you assign the another hostent variable to it, it should copy over the data so you don't have to worry. I don't remember the contents of the hostet structure well enough to know whether it is copyable like that or not, but it should be.

    Edit: I looked at the hostent structure and it uses char pointers without C++ copying, so it is not safe to copy directly. You will have to copy each value to your own data. You can still use a hostent member variable, but you must copy all of the internal data over manually. This means you have to allocate memory and use strcpy to copy the strings.
    Last edited by Daved; 10-30-2006 at 04:31 PM.

  7. #7
    Just kidding.... fnoyan's Avatar
    Join Date
    Jun 2003
    Location
    Still in the egg
    Posts
    275
    Well, assiging values directly and using the values from hostent instead of hostent* did not work!

    Code:
    class MyClass 
    {
     public:
      .... 
     private:
      hostent *h, local_h;
    };
    
    MyClass::MyClass()
    {
    ...
     h= gethostbyname();
     local_h.h_name = h->h_name;
    ...
    }
    
    string MyClass::GetHostname()
    {
      // return string(h->h_name);
      return string(local_h.h_name);
    }

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Right. I don't know if you noticed my edit, but it turns out the hostent is a C structure that doesn't have proper copy semantics. You have to allocate space for the names and then copy them with strcpy.

    I personally would create my own struct, hostent_data, and use C++ strings instead of C style strings. Then you would only have to assign each member one at a time and you wouldn't have to worry about memory allocation and deallocation.

  9. #9
    Just kidding.... fnoyan's Avatar
    Join Date
    Jun 2003
    Location
    Still in the egg
    Posts
    275
    Thanks, i actually did not notice your edit!

    It seems it is better to create my own structure and use C++ strings as you suggested.

  10. #10
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Another benefit of creating your own structure with C++ strings is that if you use it as a member of MyClass, you won't have to do anything special to copy MyClass either. If you used hostent itself, you would have to add copy constructor, copy assignment operator, and destructor code in MyClass to handle everything correctly.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  2. Replies: 6
    Last Post: 04-27-2006, 10:55 AM
  3. Member function pointer within the class
    By skorman00 in forum C++ Programming
    Replies: 1
    Last Post: 07-05-2004, 06:03 AM
  4. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM
  5. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 06:51 PM