memcpy question

2

2b|!2b==?

My C appears to be more rusty than I thought, after several years in the
C++ camp. 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)) ??? */
}
 
G

Gordon Burditt

My C appears to be more rusty than I thought, after several years in the
C++ camp. 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)) ??? */

*KNOW* the size of the src object. Either make it an element of
MyStructB, pass it as an argument to CopyIt, or otherwise keep track
of it. You can use any units you with (number of MyStructA or the
argument you passed to malloc() to allocate it). I might consider
adding to MyStructB the number of *allocated* MyStructA's and the
number of *used* MyStructA's.
 
R

Richard Heathfield

2b|!2b==? said:
My C appears to be more rusty than I thought, after several years in
the C++ camp. I have a data structure that I want to make a copy of.
Here are the details:
struct MyStructB
{
struct MyStructA *widget;
double m1;
double m2;
double m3;

Add:

size_t numWidgets;

so that you can keep track of the number of widgets you allocated.

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

This won't compile, and is in any case overly complicated. Consider:

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

Note that you will need said:
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)) ??? */
}

When you allocate it, you know it. Don't Forget.
 
D

David Starr

2b|!2b==? said:
My C appears to be more rusty than I thought, after several years in the
C++ camp. 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)) ??? */
}
Sizeof. You use sizeof to pass the size of MyStrucA to calloc. It
will work to pass the size of MyStrucB to memcpy. Sizeof returns the
size of objects in bytes so that it works with malloc and calloc and
memcpy and other stuff.

David Starr
 
K

Keith Thompson

David Starr said:
Sizeof. You use sizeof to pass the size of MyStrucA to calloc. It
will work to pass the size of MyStrucB to memcpy. Sizeof returns the
size of objects in bytes so that it works with malloc and calloc and
memcpy and other stuff.

I think the OP's point (which he didn't express very clearly) is that
the pointer points to (the first element of) an *array* of structures.
As you can see from the block comment in the CopyIt function, he
already knows about sizeof; "knowing that it is a multiple of
sizeof(struct MyStructA)".

The answer is that, given a pointer value, there's no way to determine
the size of the array to which it points. You just have to keep track
of it yourself.
 

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,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top