memcpy question (slightly OT)

2

2b|!2b==?

I have a data structure that I want to make a copy of. Here are the details:

struct MyStructA
{
char barney;
char fred;
char foo;
char foobar;
};

struct MyStructB
{
struct MyStructA *widget;
double m1;
double m2;
double m3;
};


int AllocIt(const size_t size, struct MyStructB *item)
{
if((item->widget = (struct MyStructA *)calloc(size * sizeof(struct
MyStructA))) == 0)
return -1;
else
return 0;
}


int CopyIt(struct MyStructB* dest, const struct MyStructB* src)
{
/* problem seems to be that I don't know the size (i.e. "length in
memory" of the src object) - or do I ? - is there a way I can RELIABLY
and CONSISTENTLY derive the size, knowing that it is a multiple of
sizeof(struct MyStructA)) ??? */
}
 
J

Jim Langston

2b|!2b==? said:
I have a data structure that I want to make a copy of. Here are the
details:

struct MyStructA
{
char barney;
char fred;
char foo;
char foobar;
};

struct MyStructB
{
struct MyStructA *widget;
double m1;
double m2;
double m3;
};


int AllocIt(const size_t size, struct MyStructB *item)
{
if((item->widget = (struct MyStructA *)calloc(size * sizeof(struct
MyStructA))) == 0)
return -1;
else
return 0;
}


int CopyIt(struct MyStructB* dest, const struct MyStructB* src)
{
/* problem seems to be that I don't know the size (i.e. "length in
memory" of the src object) - or do I ? - is there a way I can RELIABLY
and CONSISTENTLY derive the size, knowing that it is a multiple of
sizeof(struct MyStructA)) ??? */
}

I would store the size somewhere. Inside MyStructB seems like the logical
choice. So inside your AllocIt I would add:

item->size = size

then you can use that value in CopyIt.
 
G

Gianni Mariani

2b|!2b==? said:
I have a data structure that I want to make a copy of. Here are the
details:

struct MyStructA
{
char barney;
char fred;
char foo;
char foobar;
};

struct MyStructB
{
struct MyStructA *widget;
double m1;
double m2;
double m3;
};


int AllocIt(const size_t size, struct MyStructB *item)
{
if((item->widget = (struct MyStructA *)calloc(size * sizeof(struct
MyStructA))) == 0)
return -1;
else
return 0;
}


int CopyIt(struct MyStructB* dest, const struct MyStructB* src)
{
/* problem seems to be that I don't know the size (i.e. "length in
memory" of the src object) - or do I ? - is there a way I can RELIABLY
and CONSISTENTLY derive the size, knowing that it is a multiple of
sizeof(struct MyStructA)) ??? */
}

This is a C++ question, right (posting in comp.lang.c++ ? If so, this
is the C++ to do it.

struct MyStructA
{
char barney;
char fred;
char foo;
char foobar;
};

struct MyStructB
{
std::vector< MyStructA > widget;
double m1;
double m2;
double m3;

MyStructB()
: m1(), m2(), m3()
{
}

MyStructB( const MyStructB & rhs )
: widget( rhs.widget ),
m1(rhs.m1), m2(rhs.m2), m3(rhs.m3)
{
}

MyStructB & operator= const MyStructB & rhs )
{
widget = rhs.widget;

m1 = rhs.m1;
m2 = rhs.m2;
m3 = rhs.m3;

return *this;
}
};


int main()
{
MyStructB * x = new MyStructB(); // dynamically allocate

MyStructB y = *x; // copy

* x = y; // assignment

delete x;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top