Is a buffer created here?

This is a discussion on Is a buffer created here? within the C++ Programming forums, part of the General Programming Boards category; So I have an overloaded operator << implemented as follows: Code: ostream& operator << (ostream& os, const Position& p) { ...

  1. #1
    Registered User
    Join Date
    Jul 2011
    Posts
    6

    Is a buffer created here?

    So I have an overloaded operator << implemented as follows:

    Code:
    ostream& operator << (ostream& os, const Position& p)
    { 
    	os << "(" << p.x << ", " << p.y << ")";
    
    	return os;
    }
    The operator is a friend of the Position class.

    I found this as a solution when I was trying to find an easy way to print out a description of my objects in main() by just writing cout << object << endl; however I wasn't able to find an explanation that helped me understand what was actually happening with the streams and how everything was being processed etc.

    In regards to the stack, heap and buffers, what is happening when I call my << operator in the following way:

    Code:
    cout << PositionObject << endl;
    Is a buffer holding the "(" << p.x << ", " << p.y << ")" string I put together created in the stack when a Position object is created? or what is the sequence of events to retrieving it?

    Also, if a buffer is not created, can someone give me an example of when one is created other than an array?

    Thanks.
    Last edited by stariz; 01-17-2012 at 03:30 AM.

  2. #2
    Registered User
    Join Date
    Apr 2008
    Posts
    395
    You've overloaded the << operator, so a specific function has been defined holding your code for the Position class. Now the compiler is able to select the appropriate function depending on the operand type. There's only one syntax (<<) but many functions.
    Last edited by root4; 01-17-2012 at 04:02 AM.

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,793
    Quote Originally Posted by stariz View Post
    In regards to the stack, heap and buffers, what is happening when I call my << operator in the following way:

    Code:
    cout << PositionObject << endl;
    Is a buffer holding the "(" << p.x << ", " << p.y << ")" string I put together created in the stack when a Position object is created? or what is the sequence of events to retrieving it?
    There is no buffer - as such - for the above created at instantiation of a Position object. When you call the function, cout is passed by reference as os (os in effect becomes an alias for cout in this particular case). Then, the particular sequence of stream insertion statements you've indicated is run on the os/cout stream. The stream itself has a buffer it manages but I don't think this is what you are referring to. The code for the overloaded stream insertion function is loaded into memory at time of execution of the program (I don't know if you would call this a buffer, I don't). When the function is called, several things take place, among them objects and variable used by the function are pushed onto the stack and the code pointer jumps to the start of the code for the function and execution of code takes place from that point on. When the function ends, any objects on the stack used by the function are popped off and the code pointer returns to the point where the function was called from and execution of the program can continue from where things left off last.

    Don't know if that explains things for you.




    Quote Originally Posted by stariz View Post
    Also, if a buffer is not created, can someone give me an example of when one is created other than an array?
    Seems to me you are unclear of the meaning of the word "buffer", at least in the context of programming. In some regards any area of memory can be considered a buffer but in programming it typically refers to a scratch/temporary holding area where data is stored until further processing can be done on said stored data. Whether an array constitutes a "buffer" or not depends on the purpose of the array.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  4. #4
    Registered User NeonBlack's Avatar
    Join Date
    Nov 2007
    Posts
    435
    Depending on what type your ostream object is, it *probably* has an internal buffer, which is *probably* allocated on the heap. In the case of cout, yes there is a buffer somewhere, and each call to operator<< converts its argument to a string and adds it to the end of the buffer.

    A buffer is not always required however. For example, you might write an ostream class which writes data directly to some hardware.
    I copied it from the last program in which I passed a parameter, which would have been pre-1989 I guess. - esbo

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Class for both dynamic allocated buffer and static buffer
    By TotalTurd in forum C++ Programming
    Replies: 3
    Last Post: 01-07-2012, 08:09 PM
  2. Why is my list not being created?
    By tunerfreak in forum C++ Programming
    Replies: 10
    Last Post: 05-04-2006, 08:41 PM
  3. fgets(buffer,sizeof(buffer),stdin);
    By linuxdude in forum C Programming
    Replies: 2
    Last Post: 10-28-2003, 09:41 AM
  4. Dialog box, I created it... but I can't see it.
    By drdroid in forum C++ Programming
    Replies: 8
    Last Post: 09-14-2002, 05:43 PM
  5. how are wizards normally created?
    By endo in forum Windows Programming
    Replies: 1
    Last Post: 07-25-2002, 05:12 AM

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