Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7309075
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T23:39:53+00:00 2026-05-28T23:39:53+00:00

I want to copy a part of a class to a buffer. I need

  • 0

I want to copy a part of a class to a buffer. I need this to make something that can look if there are changes in a class, and send it over the network to update the changes on the server.

I made a template class that can back-up and restore classes. Now I am making the “look for differences” function. I want that the user can define how big the memory blocks will be. This split the class in parts and takes less data to send.

The problem is that I can’t copy a part of the class memory to the heap, I can’t get the address correct. If I do “address of the class” + “0x04”. Then I don’t get the correct addres.

This is an exaple that I made:

testclass test1;
testclass test2;

test1.set(1,1);
test2.set(1,2);

cout << &test1 << " " << (&test1 + 0x04) << endl; //0018FF24 0018FF44

memcpy(&test2,&test1 + 0x04,4);

test2.echo(); //Wrong data!

The header:

class testclass
{
    int test,test2;

public:
    void set(int a, int b) {test = a, test2 = b;}
    void echo() {cout << test << " " << test2 << endl;}
};

I hope someone help me with this problem.

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T23:39:54+00:00Added an answer on May 28, 2026 at 11:39 pm

    Basically, you can’t muck around with pointers like that. You generally can’t rely on the compiler to coincidentally put meaningful data there.

    If you want the address of members you should write &(test1.test2) not &test1+0x04 because even IF an int is 4 bytes and IF the compiler hasn’t padded the structure and IF you or someone else hasn’t changed the contents of the class, then &test1+0x04 really means “&test1 plus 4*sizeof(test) bytes”, it’s another way of reaching (&test1)[4] in terms of pointer-array-equivalence.

    Also, you can’t memcpy over classes in general and expect meaningful results, unless they are POD.

    If you want to compare instances of a class, you should write a function which compares each of the members in turn.

    You can’t write a general-purpose method for this because C++ is not a reflective language. That means you can’t write code which magically knows the names and types of the members of a class.

    So, if you want to compare and patch data like this, you will need to do something like this:

    struct Foo {
        int a;
        int b;
    
        void export_differences (std :: ostream & o, const Foo & f) {
            if (a != f.a) o << "a " << f.a << " ";
            if (b != f.b) o << "b " << f.b << " ";
            o << ";";
        }
    
        void import_differences (std :: istream & i) {
            std :: string s;
            while (i >> s) {
                if (s == "a") i >> a;
                else if (s == "b") i >> b;
                else if (s == ";") break;
                else throw std :: runtime_error ("bad input");
            }
        }
    };
    

    You will have to write something like this for each class you want to patch.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a EF4.1 class X and I want to make copy of that
I want to illustrate the pseudo transparency of windows with xcb (copy part of
i want copy pdf file from application path(/data/data/package name) to sdcard.for that i have
I want to copy the newest file that is on a mapped network directory.
I want to copy a file that's in the same directory as the installer
I wanted to make a linked list class ListList that inherits from a class
How can I parse my CSV file without parsing first line ? This class
I have this C# class structure that I would like to refactor to use
In Python I have a file stream, and I want to copy some part
I have a a function, that essentially boils down to this (the part that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.