Simple structure and copying data to pointer of the same structure

A

A

let's say I have:

struct TWhatever
{
int MyInt;
bool MyBool;
}

Now I have already preallocated memory to where pWhatever points so:

TWhatever* pWhatever;

// Memory is already preallocated so the below lines work properly
pWhatever->MyInt = 1;
pWhatever->MyBool = false;

And now I also have vector:

std::vector<TWhatever> MyVect;
MyVect.pushBack(TWhatever());
MyVect[0].MyInt = 1;
MyVect[0].MyBool = false;


And now the question finally - all I want is to copy data from vector to
memory that pWhatever points to.

So I tried:
pWhatever = &MyVect[0]; // doesn't seem to work

But this worked:
pWhatever->MyInt = MyVect[0].MyInt; // works
pWhatever->MyBool = MyVect[0].MyBool; // also works

The question - can I do it somehow simpler than the last example? The
problem is not in last 2 lines but there are 30 or so lines for each
initialization so something simpler would be much more readable in code.
 
A

A

If your objects are types that perform their own initializationsor have
internal
pointers (which they do not seem to be given the above TWhatever) , then
your
struct should also define a copy constructor. (cctor)

I want to avoid having additional constructors within structure itself
because structure is allocated thousands of times so additional constructors
means additional memory for each of them.

So effectively you're saying there is no other way except memcpy or the
stuff I just did above right (if we rule out putting a copy constructor to
save memory)?
 
A

A

OK I tried:

memcpy(pWhatever, &MyVect[0], sizeof(TWhatever));

and that seems to work and is simple enough for what I need.
Thanks for your suggestion.

If anyone has anything to add go ahead ;)
 
A

A

memcpy(pWhatever, &MyVect[0], sizeof(TWhatever));

one added question: if the original structure contains dynamically allocated
stuff like for example std::string

struct TWhatever
{
int a;
bool b;
std::string c;
}

will the above memcpy still work without any memory leaks because size of
the structure is constant.... or the std::string handles that automatically?
 
A

A

Your question appears to be about how to copy POD structs, cctor likely
will not
be needed, but the question of whether or not to use the copy constructor
is one

Yes, but it's not just POD's... in fact, in C++ Builder which I use this
struct also contains UnicodeString's
But I got no memory leaks so I'm guessing it works with memcpy....
 
T

Thomas J. Gritzan

Am 14.04.2011 17:44, schrieb A:
I want to avoid having additional constructors within structure itself
because structure is allocated thousands of times so additional constructors
means additional memory for each of them.

That's wrong. Constructors don't consume any memory in the objects.
So effectively you're saying there is no other way except memcpy or the
stuff I just did above right (if we rule out putting a copy constructor to
save memory)?

You can just assign the object. Don't assign the pointers.

Instead of
memcpy(pWhatever, &MyVect[0], sizeof(TWhatever));
you can do:
*pWhatever = MyVect[0];
 
T

Thomas J. Gritzan

Am 14.04.2011 17:59, schrieb A:
memcpy(pWhatever,&MyVect[0], sizeof(TWhatever));

one added question: if the original structure contains dynamically allocated
stuff like for example std::string

struct TWhatever
{
int a;
bool b;
std::string c;
}

will the above memcpy still work without any memory leaks because size of
the structure is constant.... or the std::string handles that automatically?

No, don't use memcpy with that struct. memcpy only works on POD types,
that is (basicly, but not fully correct) structs that could be used in a
C program. Since a std::string is not a POD, the struct that contains a
std::string also is not a POD.

You can use a simple assignment instead, since the compiler
automatically generates an assignment operator for you. Example:

TWhatever a,b;
.... // initialize b here
a = b;
 
A

Alf P. Steinbach /Usenet

* A, on 14.04.2011 17:25:
let's say I have:

struct TWhatever
{
int MyInt;
bool MyBool;
}

Now I have already preallocated memory to where pWhatever points so:

TWhatever* pWhatever;

// Memory is already preallocated so the below lines work properly
pWhatever->MyInt = 1;
pWhatever->MyBool = false;

And now I also have vector:

std::vector<TWhatever> MyVect;
MyVect.pushBack(TWhatever());
MyVect[0].MyInt = 1;
MyVect[0].MyBool = false;


And now the question finally - all I want is to copy data from vector to
memory that pWhatever points to.

So I tried:
pWhatever =&MyVect[0]; // doesn't seem to work

But this worked:
pWhatever->MyInt = MyVect[0].MyInt; // works
pWhatever->MyBool = MyVect[0].MyBool; // also works

The question - can I do it somehow simpler than the last example?

Yes.

*pWhatever = myVect[0];

The
problem is not in last 2 lines but there are 30 or so lines for each
initialization so something simpler would be much more readable in code.

Define constructors to do initialization.


Cheers & hth.,

- Alf
 
A

Alf P. Steinbach /Usenet

* A, on 14.04.2011 17:59:
memcpy(pWhatever,&MyVect[0], sizeof(TWhatever));

one added question: if the original structure contains dynamically allocated
stuff like for example std::string

struct TWhatever
{
int a;
bool b;
std::string c;
}

will the above memcpy still work without any memory leaks because size of
the structure is constant.... or the std::string handles that automatically?

The memcpy yields Undefined Behavior.

Most likely that some internal buffer will be deallocated twice or more. That
is, that the code will attempt to do so.

Remember: each occurrence of "memcpy" indicates a novice. So to progress out of
that category, you have to (temporarily) forget about existence of "memcpy".
Simply, don't use it -- and that way you also avoid a lot of bugs.


Cheers & hth.,

- Alf
 
A

A

Instead of
memcpy(pWhatever, &MyVect[0], sizeof(TWhatever));
you can do:
*pWhatever = MyVect[0];

ok, that worked too :)

but it copies data right? it doesn't just change memory pointer points to?
 
T

Thomas J. Gritzan

Am 14.04.2011 18:15, schrieb A:
Instead of
memcpy(pWhatever,&MyVect[0], sizeof(TWhatever));
you can do:
*pWhatever = MyVect[0];

ok, that worked too :)

but it copies data right? it doesn't just change memory pointer points to?

Yes.

pWhatever = &MyVect[0]; // this assigns the *pointer*
*pWhatever = MyVect[0]; // this assigns the object; i.e. all of its data
 
P

Paul

A said:
I want to avoid having additional constructors within structure itself
because structure is allocated thousands of times so additional
constructors means additional memory for each of them.

This is not the case, there will be only 1 constructor even if you have 500
objects allocated.

struct TWhatever{
TWhatever():MyInt(0), MyBool(0){}
TWhatever(const TWhatever& rhs){
MyInt= rhs.MyInt;
MyBool= rhs.MyBool;}
TWhatever& operator=(const TWhatever& rhs){
MyInt= rhs.MyInt;
MyBool = rhs.MyBool;
return *this;}
~TWhatever (){}
int MyInt;
bool MyBool;
};

struct TWhatever2{
int MyInt;
bool MyBool;
};


int main(){
std::cout<<sizeof(TWhatever) << "\t" << sizeof(TWhatever2);
}
 
M

Martijn van Buul

* Alf P. Steinbach /Usenet:
Remember: each occurrence of "memcpy" indicates a novice.

A statement like that doesn't help. In fact, it shows more about you than
anything else.

Just because *you* are safely hiding in an ivory tower where you don't need
memcpy doesn't mean that people who *do* have a clue are idiots.
 
N

Noah Roberts

let's say I have:

struct TWhatever
{
int MyInt;
bool MyBool;
}

Now I have already preallocated memory to where pWhatever points so:

TWhatever* pWhatever;

// Memory is already preallocated so the below lines work properly
pWhatever->MyInt = 1;
pWhatever->MyBool = false;

And now I also have vector:

std::vector<TWhatever> MyVect;
MyVect.pushBack(TWhatever());
MyVect[0].MyInt = 1;
MyVect[0].MyBool = false;


And now the question finally - all I want is to copy data from vector to
memory that pWhatever points to.

So I tried:
pWhatever =&MyVect[0]; // doesn't seem to work

But this worked:
pWhatever->MyInt = MyVect[0].MyInt; // works
pWhatever->MyBool = MyVect[0].MyBool; // also works

*pWhatever = MpVect[0];
 
A

Alf P. Steinbach /Usenet

* Martijn van Buul, on 14.04.2011 18:30:
* Alf P. Steinbach /Usenet:

A statement like that doesn't help.

If the statement does not help you, then just ignore it. No need to tell the
world that it doesn't help you. It's akin to yelling "i'm a super programmer who
is not helped by simple advice like that" -- nobody believes it...

In fact, it shows more about you than anything else.

What do you think it shows?

Just because *you* are safely hiding in an ivory tower where you don't need
memcpy

I'm not hiding, I'm not in an ivory tower, and it's not the case that I never
need memcpy.

Why this hostility & lying?

doesn't mean that people who *do* have a clue are idiots.

Why do you think that "people who do have a clue are idiots"?


Puzzled,

- Alf
 
M

Martijn van Buul

* Alf P. Steinbach /Usenet:
* Martijn van Buul, on 14.04.2011 18:30:
I'm not hiding, I'm not in an ivory tower, and it's not the case that I never
need memcpy.

Ah, so it's "Each occurrence of memcpy indicates a novice unless it's me" ?
 
A

Alf P. Steinbach /Usenet

* Martijn van Buul, on 14.04.2011 19:01:
* Alf P. Steinbach /Usenet:

Ah, so it's "Each occurrence of memcpy indicates a novice unless it's me" ?

No, it's just as I wrote.

If you find that sentence inordinately difficult to understand, then just ask
about it.

For my part, I admit that I failed to guess that your out of the blue ad hominem
attack, could be really just a conversation opener, preparing you for asking
your so-extremely-hard-to-ask question (whatever it is)?


- Alf
 
G

gwowen

No, it's just as I wrote.

If you find that sentence inordinately difficult to understand, then justask
about it.

To be honest Alf, I find most of your posts an enlightening breath of
sanity, but I'm not altogether sure what you mean about memcpy being
the sign of a novice. Using memcpy *wrongly*, or a using it a lot is
the sign of a novice, sure but ... anyway, I miss your point.
 
A

Alf P. Steinbach /Usenet

* gwowen, on 14.04.2011 23:32:
To be honest Alf, I find most of your posts an enlightening breath of
sanity, but I'm not altogether sure what you mean about memcpy being
the sign of a novice. Using memcpy *wrongly*, or a using it a lot is
the sign of a novice, sure but ... anyway, I miss your point.

It's about the same as, in the 1970's, one could say that every "goto" was the
sign of a novice.

Sure, there are situations where it's the right thing to do. And yes, when there
is a "goto" which is at least arguably acceptable, then it was most likely
placed there by a very competent person (since chances of anyone else managing
that feat would be infinitesimal). Then you can weight up other evidence to
counter the evidence that the "goto" gives by itself.

In modern C++ "memcpy", on its own, just says: this programmer don't even know
about curly braces initializations, or was not able to express the logical types
as C++ types, or... So, that would most likely be a novice. Much less likely, I
think, but still possible, it could perhaps be an incompetent or someone forced
by a silly coding guideline, or perhaps an MS engineer[1].


Cheers & hth.,

- Alf

Notes:
[1] Hey, that's just trolling. ;-)
 

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,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top