Thread: Question regarding pointer, which points to typecasted object of a class

  1. #1
    Registered User
    Join Date
    Jan 2014
    Posts
    76

    Question regarding pointer, which points to typecasted object of a class

    When a pointer points to an object of any class which is typecasted to that type, does it make the arguments of constructor behave like an array?
    Code:
    #include<iostream>
    using namespace std;
    
    class a{
    int q;
    int w;
    char e;
    
    public:
    a(int i, int j, char c)
    {
    count<<" i = "<<q<<endl;
    count<<" j = "<<w<<endl;
    count<<" c  = "<<e<<"\n"<<endl;
    }
    void b()
    {
    count<<" i = "<<q<<endl;
    count<<" j = "<<w<<endl;
    count<<" c  = "<<e<<"\n"<<endl;
    }
    };
    int main()
    {
    a obj(1,2,'c');
    int *i ;
    *(i+0)=90;
    *(i+1)=89;
    *(i+2)='z';
    return 0;
    }
    
    OUTPUT:
    i = 1
    j = 2
    C = y
    
    i = 90
    j = 89
    C = z
    It behaves like an array : *(i+0) =90
    How is this happening?

    OR

    is it directly changing the member variable?
    Last edited by gaurav#; 12-28-2016 at 05:04 AM.

  2. #2
    Old Took
    Join Date
    Nov 2016
    Location
    Londonistan
    Posts
    121
    Your code is malformed nor will it have the output you suggest. count is never defined, you write on memory you don't own through a pointer. a::b() is never called. It's a mess.
    I know what you were attempting (poorly) to show and that is writing to an a object through a pointer to int. That won't work either. sometimes it might, sometimes it might not, if it does it's largely through luck, it's certainly not defined behaviour.

  3. #3
    Registered User
    Join Date
    Jan 2014
    Posts
    76
    SORRY I had type the code on a cell phone which lead to few auto-corrects along with some missing parts, here is the complete code:

    Code:
    #include<iostream>
    using namespace std;
     
    class a{
    int q;
    int w;
    char e;
     
    public:
    a(int i, int j, char c): q(i),w(j),e(c)
    {
    cout<<" i = "<<q<<endl;
    cout<<" j = "<<w<<endl;
    cout<<" c  = "<<e<<"\n"<<endl;
    }
    void b()
    {
    cout<<" i = "<<q<<endl;
    cout<<" j = "<<w<<endl;
    cout<<" c  = "<<e<<"\n"<<endl;
    }
    };
    int main()
    {
    a obj(1,2,'c');
    int *i ;
    i = (int *)&obj;
    *(i+0)=90;
    *(i+1)=89;
    *(i+2)='z';
    obj.b();
    return 0;
    }
    OUTPUT:
    i = 1
    j = 2
    C = y

    i = 90
    j = 89
    C = z

  4. #4
    Old Took
    Join Date
    Nov 2016
    Location
    Londonistan
    Posts
    121
    As I said before it's purely luck that happened. It's not defined behaviour, it can't be relied on especially when you come to inheritance from non-empty base classes.

  5. #5
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    It's definitely undefined behavior.

    With regards to classes, each member is guaranteed to come after the one before it. _But_ alignment is entirely out of your control. In your case, you have a structure that goes int, int, char. This is cool.

    The first int starts at an offset of 0 when you take the address of the struct. The next int is guaranteed to come after the first but it doesn't have to be at an exact 4 bytes. The compiler can decide how to best pad the structure which makes pointer-like accesses incredibly dangerous because there's no guarantees about where data is going to live.

    So to answer your question, no, it does not behave like an array at all. In fact, you're just lucky as Hobbit has pointed out.

    I'm going to suggest something controversial but if you _must_ do this, use a static_assert to enforce the proper size.

    Let's say you have a struct with 3 4 byte types. You want to access it like an array. Given how members are guaranteed to be one after the other, you can static_assert the structure to be of size 12 and then yeah, it is legal to use pointer-like accesses. But at the same time, this is a horrible, horrible red flag and it means you're most likely doing something wrong.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Class Question - Object Orientation
    By Freem in forum C++ Programming
    Replies: 2
    Last Post: 08-22-2011, 09:17 AM
  2. Base-class pointer, accessing object from derived class
    By Korhedron in forum C++ Programming
    Replies: 15
    Last Post: 09-28-2008, 05:30 AM
  3. Replies: 8
    Last Post: 01-13-2008, 05:57 PM
  4. pointer to struct object in class object
    By Stevo in forum C++ Programming
    Replies: 3
    Last Post: 02-25-2004, 07:58 PM
  5. basic object/class question
    By Noobie in forum C++ Programming
    Replies: 8
    Last Post: 05-17-2003, 05:54 AM

Tags for this Thread