pointers issue

This is a discussion on pointers issue within the C++ Programming forums, part of the General Programming Boards category; Originally Posted by quo points to data of a block file where objects of type A are stored Don't just ...

  1. #16
    Registered User antred's Avatar
    Join Date
    Apr 2012
    Location
    Germany
    Posts
    254
    Quote Originally Posted by quo View Post
    points to data of a block file where objects of type A are stored
    Don't just tell us that. Show us where you make it happen. Your code very likely doesn't do what you think it does. Otherwise you wouldn't be having trouble with this in the first place.

  2. #17
    quo
    quo is offline
    Registered User
    Join Date
    May 2011
    Posts
    116
    Quote Originally Posted by manasij7479 View Post
    Sorry, I misunderstood your question.

    In both the cases, B's pointers are not initialized... and the first one only works by luck.
    (Well, it could be initialized, for the 2nd one but you likely didn't)

    Now, would you tell me that you do not need to allocate memory ?
    The B b1 is initialized with its constructor
    The B *b2 is b1 which is sent in the function as &b1

  3. #18
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    Quote Originally Posted by quo View Post
    The B b1 is initialized with its constructor
    The B *b2 is b1 which is sent in the function as &b1
    The default one ? That means the pointer is still invalid.
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  4. #19
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Quote Originally Posted by manasij7479 View Post
    Works?
    Do you realize that the pVoid s can be anything when you copy stuff into locations pointed by it?
    Right. Obviously I wasn't thinking. Still, it's easily fixed.
    Code:
    #include <iostream>
    #include <cstring>
    
    struct A {
        int n;
    };
    
    class B {
    public:
        A* a1;
        void* pVoid;
    };
    
    void func(B *b) {
        A a;
        a.n = 2;
        b->pVoid = new A;
        std::memcpy(b->pVoid, (void*)&a, sizeof(A));
        std::cout << ((A*)(b->pVoid))->n << '\n';
        delete (A*)b->pVoid;
    }
    
    int main() {
        A a;
        a.n = 1;
        B b;
        b.pVoid = new A;
        std::memcpy(b.pVoid, (void*)&a, sizeof(A));
        std::cout << ((A*)(b.pVoid))->n << '\n';
        delete (A*)b.pVoid;
        func(&b);
    }
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  5. #20
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    Quote Originally Posted by oogabooga View Post
    Still, it's easily fixed.
    Still it should be in a constructor.
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  6. #21
    quo
    quo is offline
    Registered User
    Join Date
    May 2011
    Posts
    116
    Quote Originally Posted by manasij7479 View Post
    The default one ? That means the pointer is still invalid.
    No,not the default one,it has a constructor

  7. #22
    msh
    msh is offline
    Novice
    Join Date
    Jul 2009
    Posts
    568
    Quote Originally Posted by antred View Post
    That means nothing. The only difference between a struct and a class is the default access level (public vs private). It's impossible to tell whether memcpy on a class / struct is safe without knowning the definition of said struct or class.
    I'm fairly certain that there's quite a lot more then just the default access level.
    Disclaimer: This post shows my ignorance at the time of its making. I claim ownership of but not responsibility for all errors in it. Reference at your own peril.

  8. #23
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    Quote Originally Posted by msh View Post
    I'm fairly certain that there's quite a lot more then just the default access level.
    in C++, there isn't, technically.
    Perhaps, you mean the distinction between POD and non-POD class/structs ?
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  9. #24
    Registered User antred's Avatar
    Join Date
    Apr 2012
    Location
    Germany
    Posts
    254
    Quote Originally Posted by msh View Post
    I'm fairly certain that there's quite a lot more then just the default access level.
    Well, then you're fairly wrong. Excerpt from the C++ standard:


    A structure is a class defined with the class-key struct; its members and base classes (clause 10) are public
    by default (clause 11)
    . A union is a class defined with the class-key union; its members are public by
    default and it holds only one data member at a time (9.5). [...]

  10. #25
    msh
    msh is offline
    Novice
    Join Date
    Jul 2009
    Posts
    568
    Quote Originally Posted by manasij7479 View Post
    in C++, there isn't, technically.
    Perhaps, you mean the distinction between POD and non-POD class/structs ?
    Yes. That's exactly what I meant.
    Disclaimer: This post shows my ignorance at the time of its making. I claim ownership of but not responsibility for all errors in it. Reference at your own peril.

  11. #26
    Registered User antred's Avatar
    Join Date
    Apr 2012
    Location
    Germany
    Posts
    254
    Quote Originally Posted by msh View Post
    Yes. That's exactly what I meant.
    Too bad that's not exactly what you said.

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

Similar Threads

  1. Issue with pointers and data types (in functions)
    By protofarmer720 in forum C Programming
    Replies: 1
    Last Post: 11-06-2010, 04:08 PM
  2. Issue with arrays, pointers, and structs.
    By RexInTheCity in forum C Programming
    Replies: 5
    Last Post: 03-29-2010, 03:30 PM
  3. Pointers Issue
    By Bakster in forum C Programming
    Replies: 9
    Last Post: 08-30-2009, 02:22 PM
  4. pointers and arrays issue
    By KoshiB in forum C++ Programming
    Replies: 4
    Last Post: 04-17-2006, 10:36 AM
  5. array of pointers issue
    By Brain Cell in forum C++ Programming
    Replies: 8
    Last Post: 03-27-2005, 03:18 PM

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