What is the difference between... (casting question)

This is a discussion on What is the difference between... (casting question) within the C++ Programming forums, part of the General Programming Boards category; I think I'm having a brain f*** at the moment. Why does this work: Code: #include <fstream> #include <string> int ...

  1. #1
    Not stupid, just stupider yaya's Avatar
    Join Date
    May 2007
    Location
    Earthland
    Posts
    204

    Thumbs up What is the difference between... (casting question)

    I think I'm having a brain f*** at the moment. Why does this work:

    Code:
    #include <fstream>
    #include <string>
    
    int main()
    {
    	char temp;
    	std::string str( "this is a string" );
    	std::ofstream out( "test.txt", ios::trunc );
    
    	temp = (char)str.size();
    	out.write( &temp, 1 );
    
    	out.close();
    	
    	return 0;
    };
    ...yet this doesn't:

    Code:
    #include <fstream>
    #include <string>
    
    int main()
    {
    	std::string str( "this is a string" );
    	std::ofstream out( "test.txt", ios::trunc );
    
    	out.write( &((char)str.size()), 1 ); // ERROR HERE!!
    
    	out.close();
    	
    	return 0;
    };
    The error I get is:
    error C2102: '&' requires l-value
    But shouldn't they be the same thing?

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Because 'temp' is an actual variable, whereas the value returned by str.size() is just a number (value). You can't get the address of a value.

    That's like saying:
    Code:
    out.write( &5, 1 );

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Not stupid, just stupider yaya's Avatar
    Join Date
    May 2007
    Location
    Earthland
    Posts
    204
    Ah, I see. I should've known that. Thanks.

  4. #4
    Registered User
    Join Date
    Apr 2006
    Posts
    2,020
    That's not quite right.

    Functions generally give their return value on the stack. After that value is processed, the stack is reused by the next function call. In this case the only processing is taking the address, which will wind up as a pointer to a portion of the stack.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    The function doesn't return an address. It returns a value. It has nothing to do with the stack. It has to do with you not being able to get the address of an actual value.


    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User
    Join Date
    Apr 2006
    Posts
    2,020
    You're right. I was assuming it compiled, in which case the behavior would be as I described, for any real implementation.

    But yeah it doesn't compile, so it's just that you can't take the address of an rvalue.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  7. #7
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    And even if it compiled, this would still be wrong:
    Functions generally give their return value on the stack.
    I don't know that many architectures, but those I do know (x86, x86-64, Alpha, PowerPC) all return sufficiently small values in a register.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  8. #8
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Quote Originally Posted by CornedBee View Post
    And even if it compiled, this would still be wrong:

    I don't know that many architectures, but those I do know (x86, x86-64, Alpha, PowerPC) all return sufficiently small values in a register.
    Seconded that. Specifically, x86 and amd64 store return values in the register [e/r]ax. So it has nothing to do with a stack. Hence it doesn't have an address, as registers don't have addresses as they're not in RAM.
    (That is, for values that fit in the register. For bigger items, such as struct, the items are stored on the stack and [e/r]ax holds its address. At least on my x86.)

    And to the original poster:
    Don't forget to open your file as binary.
    Last edited by EVOEx; 10-02-2009 at 03:36 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Design layer question
    By mdoland in forum C# Programming
    Replies: 0
    Last Post: 10-19-2007, 04:22 AM
  2. Difference Equations / Recurrence Relations
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 10-05-2007, 10:26 AM
  3. question about casting
    By movl0x1 in forum C Programming
    Replies: 8
    Last Post: 07-09-2007, 09:51 PM
  4. opengl DC question
    By SAMSAM in forum Game Programming
    Replies: 6
    Last Post: 02-26-2003, 08:22 PM
  5. Very simple question, problem in my Code.
    By Vber in forum C Programming
    Replies: 7
    Last Post: 11-16-2002, 02:57 PM

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