Thread: pointers issue

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    116

    pointers issue

    hello!

    I'm writing a C++ program and I have to use memcpy at some point.
    I will explain my problem:
    I have a simple struct named A and a class named B and I do the following:

    Code:
    class B{
    //private variables
    public:
    A* a1;
    void* pVoid;
    }
    
    
    A a2;
    a2=//assign a2 with a value
    Now,at some point of my program I have an object of type B
    and memcpy works fine.
    Code:
    B b1;
    memcpy((void*)((char*)b1.pVoid,(void*)&a2,sizeof(A));
    In another part of my program I have a pointer to an object of type B
    as I call a function in which I send an object of type B like this:

    function(&b1);

    and when I get the results of memcpy it returns garbage or memory addresses,but not the value of a2 as it does above.So I think it must be pointer's fault..

    Code:
    function(B *b2){
    memcpy((void*)((char*)b2->pVoid,(void*)&a2,sizeof(A));}
    Does anybody have any idea how to fix it?

  2. #2
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Quote Originally Posted by quo View Post
    I'm writing a C++ program and I have to use memcpy at some point.
    Fail (especially in this case !)
    1.Define a Copy constructor. (and also assignment operator and destructor)
    2.Use std::copy.

  3. #3
    Registered User
    Join Date
    May 2011
    Posts
    116
    Quote Originally Posted by manasij7479 View Post
    Fail (especially in this case !)
    1.Define a Copy constructor. (and also assignment operator and destructor)
    2.Use std::copy.
    No I have to use memcpy,that's not the issue.
    I have to write something in memory,I don't want a copy constructor.
    What I don't get is why it's not working with the pointer and works with the object.
    Thank you for your time,hope you can help me

  4. #4
    Registered User antred's Avatar
    Join Date
    Apr 2012
    Location
    Germany
    Posts
    257
    Your code looks very suspicious, but unless you post ALL of your code, it's hard to say whether it's just bad style or actually an error.

  5. #5
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Quote Originally Posted by quo View Post
    No I have to use memcpy,that's not the issue.
    I have to write something in memory,I don't want a copy constructor.\
    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 ?
    Last edited by manasij7479; 05-12-2012 at 02:15 PM.

  6. #6
    Registered User
    Join Date
    May 2011
    Posts
    116
    Quote Originally Posted by antred View Post
    Your code looks very suspicious, but unless you post ALL of your code, it's hard to say whether it's just bad style or actually an error.
    Unfortunately I can't do that as it is very very big

  7. #7
    Registered User antred's Avatar
    Join Date
    Apr 2012
    Location
    Germany
    Posts
    257
    Then at least show us where you actually point that pVoid member variable at something.

  8. #8
    Novice
    Join Date
    Jul 2009
    Posts
    568
    std::memcpy - cppreference.com

    Note that it's undefined behavior to memcpy classes.
    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.

  9. #9
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    It's probably a problem with parentheses. This works:
    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;
        std::memcpy(b->pVoid, &a, sizeof(A));
        std::cout << ((A*)(b->pVoid))->n << '\n';
    }
    
    int main() {
        A a;
        a.n = 1;
        B b;
        std::memcpy(b.pVoid, &a, sizeof(A));
        std::cout << ((A*)(b.pVoid))->n << '\n';
        func(&b);
    }
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  10. #10
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Quote Originally Posted by msh View Post
    std::memcpy - cppreference.com

    Note that it's undefined behavior to memcpy classes.
    It's perfectly defined for "C-compatible structs" (of course!).
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  11. #11
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Quote Originally Posted by oogabooga View Post
    It's probably a problem with parentheses. This works:
    Works?
    Do you realize that the pVoid s can be anything when you copy stuff into locations pointed by it?

  12. #12
    Novice
    Join Date
    Jul 2009
    Posts
    568
    Aha. I missed that A is a struct. My mistake.
    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.

  13. #13
    Registered User
    Join Date
    May 2011
    Posts
    116
    Quote Originally Posted by oogabooga View Post
    It's probably a problem with parentheses. This works:
    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;
        std::memcpy(b->pVoid, &a, sizeof(A));
        std::cout << ((A*)(b->pVoid))->n << '\n';
    }
    
    int main() {
        A a;
        a.n = 1;
        B b;
        std::memcpy(b.pVoid, &a, sizeof(A));
        std::cout << ((A*)(b.pVoid))->n << '\n';
        func(&b);
    }
    thank you for your answer,it doesn't work though
    &a has to be casted to void*

  14. #14
    Registered User antred's Avatar
    Join Date
    Apr 2012
    Location
    Germany
    Posts
    257
    Quote Originally Posted by msh View Post
    Aha. I missed that A is a struct. My mistake.
    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.

  15. #15
    Registered User
    Join Date
    May 2011
    Posts
    116
    Quote Originally Posted by antred View Post
    Then at least show us where you actually point that pVoid member variable at something.
    points to data of a block file where objects of type A are stored

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, 04:18 PM