Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Simple structure and copying data to pointer of the same structure

Reply
Thread Tools

Simple structure and copying data to pointer of the same structure

 
 
A
Guest
Posts: n/a
 
      04-14-2011
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.


 
Reply With Quote
 
 
 
 
A
Guest
Posts: n/a
 
      04-14-2011
> 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)?


 
Reply With Quote
 
 
 
 
A
Guest
Posts: n/a
 
      04-14-2011
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


 
Reply With Quote
 
A
Guest
Posts: n/a
 
      04-14-2011
> 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?


 
Reply With Quote
 
A
Guest
Posts: n/a
 
      04-14-2011
> 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....


 
Reply With Quote
 
Thomas J. Gritzan
Guest
Posts: n/a
 
      04-14-2011
Am 14.04.2011 17:44, schrieb 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.


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];

--
Thomas
 
Reply With Quote
 
Thomas J. Gritzan
Guest
Posts: n/a
 
      04-14-2011
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;

--
Thomas
 
Reply With Quote
 
Alf P. Steinbach /Usenet
Guest
Posts: n/a
 
      04-14-2011
* 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

--
blog at <url: http://alfps.wordpress.com>
 
Reply With Quote
 
Alf P. Steinbach /Usenet
Guest
Posts: n/a
 
      04-14-2011
* 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

--
blog at <url: http://alfps.wordpress.com>
 
Reply With Quote
 
A
Guest
Posts: n/a
 
      04-14-2011
> 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?


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Never ever use a raw pointer when a smart pointer can do the same job Hicham Mouline C++ 100 08-25-2009 05:07 PM
eval of Data::Dumper output not same as original data structure himanshu.garg@gmail.com Perl Misc 4 08-01-2007 01:49 PM
pointer to structure from pointer to member sieg1974@yahoo.com C Programming 6 08-27-2006 01:20 AM
Pointer-to-pointer-to-pointer question masood.iqbal@lycos.com C Programming 10 02-04-2005 02:57 AM
copying data to a structure member array anonymous C Programming 12 01-11-2005 09:56 PM



Advertisments