Thread: reinterpreting variables etc

  1. #1
    Registered User
    Join Date
    Jul 2009
    Location
    Croatia
    Posts
    272

    reinterpreting variables etc

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main(int argc, char *argv[])
    {
          //undefined behaviour?
          int i = 100;
          *(char *)&i = 25;
          cout << i << endl;
          
          int &r = *new int;
          r = 5;
      
          return 0;
    }
    Is the first part of the code undefined behaviour?

    It seems like it works for any type smaller then an int, but when i try
    Code:
    int i = 100;
    *(float *)&i = 25;
    cout << i << endl;
    That wont work (compiles but the value is changed to a trash val).

    For the second part of the code, with references, what happens?

    A compiler creates a temporary variable in which he stores the new int and reference "points" to the temporary variable which is unnamed?

    What is the point of such references?


    One more thing: is casting away constness undefined behaviour?

    With const_cast i mean.

    And... why is ~ operator when used on for example int, implementation defined? Is it because it depends if the system it's being used on uses 2s complement system to represent variables?
    Last edited by Tool; 07-07-2010 at 08:33 AM.

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    *(float *)&i = 25;
    simply tells the compiler to take the address of i, convert it to a float and assign 25.0f to that float. Float representation is not the same as integers; therefore you see "garbage". How data is stored depends on the type and the compiler uses this type to know how to interpret the data stored at some address. It so happens that a char is stored the same way as an int.

    The reference points to the storage area. An alias for that storage area. What is your question, exactly? A reference is an alias.

    Casting away const can lead to undefined behavior since the compiler may assume its value may never change. But in C++ there are two different types of const--constant expressions (do absolutely not cast away the const here), and regular variables whose values mustn't change (ie variables passed to a function who promises not to change them). In the second case, it should be possible to cast away the const if you have a very good reason. For example, a legacy API.

    ~ simply inverts all bits in a variable, but the layout of the variable in memory is implementation defined because not all systems use 2's complement. This is to the best of my knowledge.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How are variables stored in memory.
    By ineedmunchies in forum C++ Programming
    Replies: 3
    Last Post: 04-29-2010, 05:49 PM
  2. basic question about global variables
    By radeberger in forum C++ Programming
    Replies: 0
    Last Post: 04-06-2009, 12:54 AM
  3. Best way to avoid using global variables
    By Canadian0469 in forum C++ Programming
    Replies: 7
    Last Post: 12-18-2008, 12:02 PM
  4. Replies: 15
    Last Post: 09-30-2008, 02:12 AM
  5. esbo's data sharing example
    By esbo in forum C Programming
    Replies: 49
    Last Post: 01-08-2008, 11:07 PM