473,386 Members | 1,804 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

memcpy question (slightly OT)

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)) ??? */
}
Apr 25 '07 #1
2 1164
"2b|!2b==?" <ro**@your.box.comwrote in message
news:3r*********************@bt.com...
>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.
Apr 25 '07 #2
2b|!2b==? wrote:
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;
}
Apr 25 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: franky.backeljauw | last post by:
Hello, following my question on "std::copy versus pointer copy versus member copy", I had some doubts on the function memcpy, as was used by tom_usenet in his reply. - Is this a c++ standard...
2
by: rinku24 | last post by:
What is the difference between memcpy and memmove? Which is more expensive?
5
by: manya | last post by:
Ok, it's been a while since I've done the whole memcpy stuff with C++ and I'm having a hard time remembering everything. I hope, however, that you can help me with my problem. I memcpy a...
35
by: Christopher Benson-Manica | last post by:
(if this is a FAQ or in K&R2, I didn't find it) What parameters (if any) may be 0 or NULL? IOW, which of the following statements are guaranteed to produce well-defined behavior? char src;...
33
by: Case | last post by:
#define SIZE 100 #define USE_MEMCPY int main(void) { char a; char b; int n; /* code 'filling' a */
6
by: myhotline | last post by:
hi all im very confused about using memcpy and i have three questions....memcpy takes a pointer to src and a pointer to dest and copies src to destination...but im very confuzed about when to...
70
by: Rajan | last post by:
Hi, I am trying to simulate a memcpy like this void* mem_cpy(void* dest, void* src, int bytes) { dest = malloc(bytes); } Now if I want to copy the bytes from src to dest, how do I copy these...
11
by: smnoff | last post by:
How does one get the source code for memcpy()?
18
by: Mark | last post by:
Hi List, I want to write a function to copy some data out of a hardware buffer. The hardware can change the contents of this buffer without it being written to by my function. I want to use...
18
by: sam | last post by:
(newbie)Technically what's the difference between memset() and memcpy() functions?
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.