CAST question

This is a discussion on CAST question within the C++ Programming forums, part of the General Programming Boards category; What's the difference betwem Code: int address = 1000; int *ptr = (int*)address; and Code: int address = 1000; int ...

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    160

    CAST question

    What's the difference betwem

    Code:
    int address = 1000;
    int *ptr = (int*)address;
    and

    Code:
    int address = 1000;
    int *ptr = address;

    - It may be because I haven't yet used CAST, but please explain.

    Furthermore I don't get what the point is about this:
    int *ptr = (int *)02400000;

    (It's the exact same thing I asked in this thread Memory Addresses but since I got no results I ask once more)
    Well english isn't my first language, (it's instead a useless language called danish which only 5 milion people speak!!) so if you think my grammar SUCKS (it does by the way) than you're more then welcome to correct me.
    Hell I might even learn something

  2. #2
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    In your example address is not an int pointer it is an int. So you use a cast to tell the compiler that you want the int that address contains to be used as a pointer value. That is all a cast does.

  3. #3
    Registered User Nippashish's Avatar
    Join Date
    Sep 2002
    Posts
    34
    The first one is assigning the pointer to an address. The second is assigning a value to the memory pointed to by the pointer.

    int address = 1000;
    int *ptr = (int*)address;

    is equivelent to:

    int address, *ptr;
    address = 1000;
    ptr = (int *)address;

    It just doesn't look like that because you're declaring and defining on the same line.
    (Don't point a pointer to an arbitrary place in memory like that btw, it will (should) crash your program).

    int *ptr = (int *)02400000;

    is equivelent to:

    int *ptr;
    ptr = (int *)02400000;

    C++ won't let you have different datatypes on each side of an equals sign unless there is a one step conversion (Correct me if I'm wrong on that). The (int *) turns the integer 02400000 into an address of an integer.

  4. #4
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    Casting is the process of telling your compiler that you know better than it does. Thats all there is to it.
    The casts shown here are old style c casts. Correctly written in c++ it would look like this...
    Code:
    int address=1000;
    int* ptr= reinterpret_cast<int*>(address);
    here we make an int but we would like to use this int as an address so we tell the compiler to reinterpret that int as an int* instead. address still holds an int but ptr holds the int* representation of that int. This is a poor example of casting really.The times you will need to do this can almost certainly be counted on no fingers.
    In your second example I expect you will get a compiler error saying that there is no valid conversion between int and int*.

    All c/c++ texts will have examples of casting.Have a read through your books.Sometimes it is very necessary to cast especially in windows programming where you often recieve a pointer to a struct as a long. This cannot be used as a pointer until it has been cast back to a pointer to the correct type.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  5. #5
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,655
    There is nothing wrong with C-style casts. In most cases they're sufficient and do the exact same thing as the C++ version, (and a hell of a lot quicker to type ). Just a matter of taste.

  6. #6
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    There is nothing wrong with C-style casts.
    Except they are harder to spot than something that says _cast and also you cannot know whether they failed whereas you can with c++ style casts.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  7. #7
    moi
    moi is offline
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    Originally posted by Stoned_Coder
    you cannot know whether they failed
    failed? C-style casts don't fail.
    hello, internet!

  8. #8
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    A C style cast can fail. they have rules to follow too. Try a few dynamic casts but c style. Try ones that will fail with bad_cast exception if dynamic_cast is used.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  9. #9
    moi
    moi is offline
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    are you sure? show me a cast that fails.
    hello, internet!

  10. #10
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    You could have done this yourself. I gave instructions. Lazy!
    Code:
    #include <iostream>
    using namespace std;
    
    class base
    {
    public:
        virtual void func() const = 0;
    };
    
    class real_derive_a : public base
    {
    public:
        real_derive_a(){}
        virtual void func() const {cout << "real_derive_a" << endl;}
    };
    
    class real_derive_b : public base
    {
    public:
        real_derive_b(){}
        virtual void func() const {cout << "real_derive_b" << endl;}
    };
    
    class imag_derive
    {
    public:
        imag_derive(){}
        virtual void func() const {cout << "imag_derive" << endl;}
    };
    
    int main()
    {
        base* pBase = new real_derive_a;
    
    
        real_derive_b* pRealB;
    
        pRealB = (real_derive_b*)pBase;
        if(pRealB == 0)
            cout << "C style cast failure" << endl;
        else
            pRealB->func(); // function executes but cast fails. Which function would this have called if cast didnt fail?
        pRealB = dynamic_cast<real_derive_b*>(pBase);
        if(pRealB == 0)
            cout << "dynamic cast failure" << endl; 
        else
            pRealB->func();
    
    
        imag_derive* pImag;
    
        pImag = (imag_derive*)pBase;
        if(pImag == 0)
            cout << "C style cast failure" << endl;
        else
            pImag->func(); // same here.
        pImag = dynamic_cast<imag_derive*>(pBase);
        if(pImag == 0)
            cout << "dynamic cast failure" << endl;
        else
            pImag->func();
    
    
        delete pBase;
        return 0;
    }
    Last edited by Stoned_Coder; 12-10-2002 at 10:53 PM.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  11. #11
    moi
    moi is offline
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    ehh? that cast didnt fail. it did exactly what it was supposed to do. the fact that that wasnt what you wanted it to do is irrelevant. like i said before, C casts do not fail. it is the nature of them: explicit conversion from one data type to another, whether or not that conversion makes any sense at all.
    hello, internet!

  12. #12
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    how if you explicitly cast a base* to a real_derive_b* does the function real_derive_a::func() get called when real_derive_b is not a base of real_derive_a ??
    how if you explicitly cast a base* to a imag_derive* does the function real_derive_a::func() get called when imag_derive is not a base of any class??

    Its because the casts fail.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  13. #13
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,655
    ...In most cases they're sufficient and do the exact same thing as the C++ version...
    Well, it's true there are some reasons that you wouldn't want to use a C style cast, Stoner's example being one of them. However, most of the time it really doesn't matter much which you choose. And I really disagree with the argument that "it's more readable" to use the C++ version. Sloppy code can't be saved by a static_cast<>, and well-written code won't be obscured by a (Foo*) one.

  14. #14
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,139
    >Its because the casts fail.

    I guess that's just a point of view. The cast itself will not fail. If you cast an int 1000 to a pointer to hashtable, the 1000 will be correctly converted to a pointer to hashtable. Calling any function on the pointer will fail, because the cast was nonsense from the start. However, the cast itself did not fail, what fails is the operation right after the cast.

    As said above, the C++ Version might be safer, but a good programmer can use both safely and a bad programmer will crash his programs using either style. I have seen so many programs that used C++ style casts, but did not catch exceptions. What is it good for then ? It will crash your app anyway, even if it notices the error and throws a bad_cast exception that isn't caught.
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  15. #15
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    For the most part this is an issue of preference. I actually try and use C++ style coding for C++ and C style coding for C but some do not. The bottom line is that it doesn't matter most of the time. Some of the time it is better. One advantage of using C++ style typecasting is that you are very specific as to what you are doing. It makes your intentions obvious to anyone else who reads your code.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie cast question
    By 200thhorseman in forum C++ Programming
    Replies: 4
    Last Post: 07-05-2009, 02:25 AM
  2. another do while question
    By kbpsu in forum C++ Programming
    Replies: 3
    Last Post: 03-23-2009, 12:14 PM
  3. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  4. question
    By Gil22 in forum C++ Programming
    Replies: 2
    Last Post: 03-31-2003, 07:36 PM
  5. opengl DC question
    By SAMSAM in forum Game Programming
    Replies: 6
    Last Post: 02-26-2003, 08:22 PM

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