 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
fernandez.dan@gmail.com Guest
|
Posted: Wed Jul 27, 2005 11:22 pm Post subject: Design question with memcpy with a method having a char* pa |
|
|
Hi,
I have a question dealing if I should do a memcopy or pointer
assigment. An example with a simplication of my class
class MyClass
{
public:
void methodA(char* data, long size);
private:
long m_size;
char* m_data;
};
My question is dealing with my methodA. What is the appropriate
strategy for assigning member variable m_data from methodA. I'm not
sure if I should do a memcpy so I don't have to worry about the scope
of the pointer. Example of the two different implementations of methodA
are
void MyClass::methodA(char* data, long size)
{
if(data == NULL || size == 0)
return;
if(m_data != NULL)
delete m_data;
m_data = new char[size];
m_size = size;
memcpy(m_data, data, size);
}
or
void MyClass::methodA(char* data, long size)
{
if(data == NULL || size == 0)
return;
if(m_data != NULL)
delete m_data;
m_data = data;
m_size = size;
}
Hopefully with this simple example you kind of get my question. Thanks
for any input or advice on this point.
Danny
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Shantanu Garg Guest
|
Posted: Thu Jul 28, 2005 11:21 am Post subject: Re: Design question with memcpy with a method having a char |
|
|
| Quote: | My question is dealing with my methodA. What is the appropriate
strategy for assigning member variable m_data from methodA. I'm not
sure if I should do a memcpy so I don't have to worry about the scope
of the pointer. Example of the two different implementations of methodA
are
void MyClass::methodA(char* data, long size)
{
if(data == NULL || size == 0)
return;
if(m_data != NULL)
delete m_data;
m_data = new char[size];
m_size = size;
memcpy(m_data, data, size);
}
or
void MyClass::methodA(char* data, long size)
{
if(data == NULL || size == 0)
return;
if(m_data != NULL)
delete m_data;
m_data = data;
m_size = size;
}
The answer is it depends. If the 'char* data' is being allocated on heap |
and only purpose of this is to just pass it in methodA the second
approach is good. Otherwise the first aproach is good. In general case
we should avoid the second case, since we dont want to impose anything
to the caller of 'methodA'. The first approach is good, clean and will
always work irresoective of the way caller allocates 'data' or use it
afterwards.
-Shantanu Garg
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
lampard Guest
|
Posted: Thu Jul 28, 2005 11:49 am Post subject: Re: Design question with memcpy with a method having a char* |
|
|
[email]fernandez.dan (AT) gmail (DOT) com[/email] wrote:
| Quote: | Hi,
I have a question dealing if I should do a memcopy or pointer
assigment. An example with a simplication of my class
class MyClass
{
public:
void methodA(char* data, long size);
private:
long m_size;
char* m_data;
};
My question is dealing with my methodA. What is the appropriate
strategy for assigning member variable m_data from methodA. I'm not
sure if I should do a memcpy so I don't have to worry about the scope
of the pointer. Example of the two different implementations of methodA
are
This approach looks safer to me with the only exception that the |
parameter should be declared as a const variable. With this approach
you need not care what happens to the passed in paramter in the caller.
//void MyClass::methodA(const char* data, long size)
| Quote: | void MyClass::methodA(char* data, long size)
{
if(data == NULL || size == 0)
return;
if(m_data != NULL)
delete m_data;
m_data = new char[size];
m_size = size;
memcpy(m_data, data, size);
}
or
This approach is very dangerous, as you may delete the memory pointed |
to by the caller, unless if it is
what you intend to do.
Try calling the same method twice with the same data, you may find
unhandled exception.
| Quote: | void MyClass::methodA(char* data, long size)
{
if(data == NULL || size == 0)
return;
if(m_data != NULL)
delete m_data;
m_data = data;
m_size = size;
}
Hopefully with this simple example you kind of get my question. Thanks
for any input or advice on this point.
|
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Owen Guest
|
|
| Back to top |
|
 |
Stephan Brönnimann Guest
|
Posted: Thu Jul 28, 2005 11:56 am Post subject: Re: Design question with memcpy with a method having a char* |
|
|
[email]fernandez.dan (AT) gmail (DOT) com[/email] wrote:
| Quote: | Hi,
I have a question dealing if I should do a memcopy or pointer
assigment. An example with a simplication of my class
class MyClass
{
public:
void methodA(char* data, long size);
private:
long m_size;
char* m_data;
};
My question is dealing with my methodA. What is the appropriate
strategy for assigning member variable m_data from methodA. I'm not
sure if I should do a memcpy so I don't have to worry about the scope
of the pointer. Example of the two different implementations of methodA
are
void MyClass::methodA(char* data, long size)
{
if(data == NULL || size == 0)
return;
if(m_data != NULL)
delete m_data;
m_data = new char[size];
m_size = size;
memcpy(m_data, data, size);
}
or
void MyClass::methodA(char* data, long size)
{
if(data == NULL || size == 0)
return;
if(m_data != NULL)
delete m_data;
m_data = data;
m_size = size;
}
Hopefully with this simple example you kind of get my question. Thanks
for any input or advice on this point.
Danny
|
Unless you have very good reasons, you should copy `data' in
void MyClass::methodA(char* data, long size);
Because you don't modify `data' you might
take the argument as const char* data.
Rationale:
Think about the implementation of MyClass's copy constructor,
assignment operator and destructor:
If you don't copy m_data from the right-hand-side two (or more)
objects will share the same data and both of it will delete it
in the destructor which is undefined behavior.
If you really want to transfer the ownership of `data'
then you should consider to use std::auto_ptr<char> for
MyClass::methodA(...).
regards
Stephan Brönnimann
[email]broeni (AT) osb-systems (DOT) com[/email]
Open source rating and billing engine for communication networks.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|