Thread: overloaded <<

  1. #1
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696

    overloaded <<

    Code:
    // Object definition
    ...
    int     name;
    void* hook;  //the variable that holds the info
    ...
    std::istream &operator>>(std::istream &stream, Object &o)
    {
      char temp[100];
    
      stream >> name;
      stream.getline(temp, 100);
      o.hook = &temp;
      // the following gives the output I want 
      std::cout << reinterpret_cast<const char *>(o.hook);
      return stream;
    }
    
    std::ostream &operator<<(std::ostream &stream, const Object &o)
    {
      stream << name;
      stream << reinterpret_cast<const char *>(o.hook);
    
      return stream;
    }
    
    // Main program
    ...
    Object object;
    ...
    //the following gives garbage for "hook" only
    std::cout << object;
    What causes the problem above?
    Last edited by alphaoide; 03-15-2004 at 06:08 PM.
    source: compsci textbooks, cboard.cprogramming.com, world wide web, common sense

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Code:
    char temp[100];
    
    stream.getline(temp, 100);
    o.hook = &temp;
    o.hook is assigned the address of a local variable that goes out of scope at the end of the function, so when you call the overload << function, o.hook points to nothing in particular.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    Originally posted by Hammer
    Code:
    char temp[100];
    
    stream.getline(temp, 100);
    o.hook = &temp;
    o.hook is assigned the address of a local variable that goes out of scope at the end of the function, so when you call the overload << function, o.hook points to nothing in particular.
    So, do you suggest any solution?
    source: compsci textbooks, cboard.cprogramming.com, world wide web, common sense

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    A couple (at least):

    Make the char array a member variable of the class that o is a type of.

    or

    Use dynamic memory allocation (new/delete).


    Anyway, why is hook a void pointer? Why not a char pointer?
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    Yoohoo, got it. Thanks!
    I use void* because it's given by my prof; can't change it.
    source: compsci textbooks, cboard.cprogramming.com, world wide web, common sense

  6. #6
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    I've been doing research on deleting void pointer but couldn't find comprehensive answer on it.
    So, Can I or can I not delete void pointer?
    If I can, is there specific way to do it?
    If I can't, will I run out of heap space at some point?
    Could you give me some general insight on this issue or give me any refence on the web? (I did google but unsatisfied)

    EDIT: is it just like this
    Code:
    void* hook;
    {
        char* temp = new char[80];
        
        hook = temp;
    }
    
    delete reinterpret_cast<char *> hook;
    Last edited by alphaoide; 03-15-2004 at 07:30 PM.
    source: compsci textbooks, cboard.cprogramming.com, world wide web, common sense

  7. #7
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Yes, I believe you can call delete on a void*, but be warned, if the thing you're deleting is an object, it's constructor will not be called.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  8. #8
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >So, Can I or can I not delete void pointer?
    You cannot. For details, see section 5.3.5 of the C++ standard (don't forget the footnotes).

    >If I can't, will I run out of heap space at some point?
    Well, if you don't release the memory you allocate then eventually the program will leak it all away. But the real question is why are you using a void pointer in the first place?
    My best code is written with the delete key.

  9. #9
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    Originally posted by Prelude
    >So, Can I or can I not delete void pointer?
    You cannot. For details, see section 5.3.5 of the C++ standard (don't forget the footnotes).

    >If I can't, will I run out of heap space at some point?
    Well, if you don't release the memory you allocate then eventually the program will leak it all away. But the real question is why are you using a void pointer in the first place?
    Where you go to look up that section of C++ standard?
    So if you ever allocate something with a void pointer you could never deallocate it?
    This method don't work?
    Code:
    void* hook;
    {
        char* temp = new char[80];
        
        hook = temp;
    }
    
    delete reinterpret_cast<char *> hook;
    And as I said, the void pointer is already given in the assignment from my prof to be used. I'll make sure to mention this issue to my prof.
    source: compsci textbooks, cboard.cprogramming.com, world wide web, common sense

  10. #10
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Where you go to look up that section of C++ standard?
    In the standard probably. It's a small but rewarding purchase from www.ansi.org. Or you can get a hardcopy in book form from your bookstore. I believe that it should be out by now.

    >delete reinterpret_cast<char *> hook;
    As long as the static type of the pointer matches the type of the object. In other words, if the object is a char, you delete a char *. If the object is an array of char, you delete a char * with delete[]. As you cannot have objects of type void, deleting a void * doesn't make much sense. So that method would work (once you fix the syntax error of the cast and undefined behavior of not deleting an array), even though it's incredibly ugly and suggests a poor design.

    >I'll make sure to mention this issue to my prof.
    You do that. Void pointers are rarely needed in C++.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  2. Problem using overloaded << operator
    By Zildjian in forum C++ Programming
    Replies: 5
    Last Post: 09-19-2004, 01:09 PM
  3. setw() type of overloaded <<
    By Thantos in forum C++ Programming
    Replies: 7
    Last Post: 06-13-2004, 10:25 AM
  4. overloaded << AP classes
    By lithium in forum C++ Programming
    Replies: 12
    Last Post: 02-11-2003, 01:42 AM
  5. overloaded << operator doesnt work
    By Unregistered in forum C++ Programming
    Replies: 6
    Last Post: 04-21-2002, 04:20 AM