473,721 Members | 2,133 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

copy content of vector<BYTE> to BYTE*

Hi,

I have replaced my BYTE* by a vector<BYTE> and before I used to do :

void CCardRecord::Ge tRecData(int nOffset, int nDataSize, CString& csValue)
{
BYTE *pTmp = NULL;
pTmp = new BYTE[nDataSize + 1];
memset(pTmp, 0, nDataSize + 1);
//memcpy(pTmp, &m_pData[ nOffset ], nDataSize);
csValue = pTmp;
}

But now m_pData is a vector.
Jul 22 '05 #1
16 9206
Vince wrote:
I have replaced my BYTE* by a vector<BYTE> and before I used to do :

void CCardRecord::Ge tRecData(int nOffset, int nDataSize, CString& csValue)
{
BYTE *pTmp = NULL;
pTmp = new BYTE[nDataSize + 1];
memset(pTmp, 0, nDataSize + 1);
//memcpy(pTmp, &m_pData[ nOffset ], nDataSize);
Nothing changes, really. You should still be able to do

memcpy(pTmp, &m_pData[nOffset], nDataSize);

The reason is that vector's storage is a contiguous chunk of elements,
essentially an array. 'm_pData[nOffset]' does call the operator[] for the
vector, but it returns a reference to the nOffset-th element, and you may
take its address. All elements of the vector after the nOffset-th sit in
the same array, so you may pass it to 'memcpy'.
csValue = pTmp;
}

But now m_pData is a vector.


V
Jul 22 '05 #2
d:\RATP\Borne_F ROM_SCRATCH_200 105\CardObj.cpp (159): error C2440: '=' :
cannot convert from 'std::vector<_T y>' to 'BYTE *'
with
[
_Ty=BYTE
]
"Victor Bazarov" <v.********@com Acast.net> a écrit dans le message de news:
5u************* *****@newsread1 .mlpsca01.us.to .verio.net...
Vince wrote:
I have replaced my BYTE* by a vector<BYTE> and before I used to do :

void CCardRecord::Ge tRecData(int nOffset, int nDataSize, CString&
csValue)
{
BYTE *pTmp = NULL;
pTmp = new BYTE[nDataSize + 1];
memset(pTmp, 0, nDataSize + 1);
//memcpy(pTmp, &m_pData[ nOffset ], nDataSize);


Nothing changes, really. You should still be able to do

memcpy(pTmp, &m_pData[nOffset], nDataSize);

The reason is that vector's storage is a contiguous chunk of elements,
essentially an array. 'm_pData[nOffset]' does call the operator[] for the
vector, but it returns a reference to the nOffset-th element, and you may
take its address. All elements of the vector after the nOffset-th sit in
the same array, so you may pass it to 'memcpy'.
csValue = pTmp;
}

But now m_pData is a vector.


V

Jul 22 '05 #3
Vince wrote:
d:\RATP\Borne_F ROM_SCRATCH_200 105\CardObj.cpp (159): error C2440: '=' :
cannot convert from 'std::vector<_T y>' to 'BYTE *'
with
[
_Ty=BYTE
]
WHERE? There is no assignment in your code fragment except to 'csValue'
and 'pTmp', none of which is a vector.

You said 'm_pData' was a vector. Is it? Or is it a pointer to a vector?
Did you leave the rest of the function intact? Did you change it? If you
did change it, how?

And don't top-post, please. Thank you.


"Victor Bazarov" <v.********@com Acast.net> a écrit dans le message de news:
5u************* *****@newsread1 .mlpsca01.us.to .verio.net...
Vince wrote:
I have replaced my BYTE* by a vector<BYTE> and before I used to do :

void CCardRecord::Ge tRecData(int nOffset, int nDataSize, CString&
csValue)
{
BYTE *pTmp = NULL;
pTmp = new BYTE[nDataSize + 1];
memset(pTm p, 0, nDataSize + 1);
//memcpy(pTmp, &m_pData[ nOffset ], nDataSize);


Nothing changes, really. You should still be able to do

memcpy(pTmp, &m_pData[nOffset], nDataSize);

The reason is that vector's storage is a contiguous chunk of elements,
essentially an array. 'm_pData[nOffset]' does call the operator[] for the
vector, but it returns a reference to the nOffset-th element, and you may
take its address. All elements of the vector after the nOffset-th sit in
the same array, so you may pass it to 'memcpy'.

csValue = pTmp;
}

But now m_pData is a vector.


V


--
Please remove capital As from my address when replying by mail
Jul 22 '05 #4

"Victor Bazarov" <v.********@com Acast.net> a écrit dans le message de news:
GD************* ******@newsread 1.mlpsca01.us.t o.verio.net...
Vince wrote:
d:\RATP\Borne_F ROM_SCRATCH_200 105\CardObj.cpp (159): error C2440: '=' :
cannot convert from 'std::vector<_T y>' to 'BYTE *'
with
[
_Ty=BYTE
]


WHERE? There is no assignment in your code fragment except to 'csValue'
and 'pTmp', none of which is a vector.

You said 'm_pData' was a vector. Is it? Or is it a pointer to a vector?
Did you leave the rest of the function intact? Did you change it? If you
did change it, how?

And don't top-post, please. Thank you.


"Victor Bazarov" <v.********@com Acast.net> a écrit dans le message de
news: 5u************* *****@newsread1 .mlpsca01.us.to .verio.net...
Vince wrote:

I have replaced my BYTE* by a vector<BYTE> and before I used to do :

void CCardRecord::Ge tRecData(int nOffset, int nDataSize, CString&
csValue)
{
BYTE *pTmp = NULL;
pTmp = new BYTE[nDataSize + 1];
memset(pTmp , 0, nDataSize + 1);
//memcpy(pTmp, &m_pData[ nOffset ], nDataSize);

Nothing changes, really. You should still be able to do

memcpy(pTmp, &m_pData[nOffset], nDataSize);

The reason is that vector's storage is a contiguous chunk of elements,
essentiall y an array. 'm_pData[nOffset]' does call the operator[] for
the
vector, but it returns a reference to the nOffset-th element, and you may
take its address. All elements of the vector after the nOffset-th sit in
the same array, so you may pass it to 'memcpy'.
csValue = pTmp;
}

But now m_pData is a vector.

V


--
Please remove capital As from my address when replying by mail

It is located somewhere else :

BYTE* pData = NULL;
map<int, CCardFile>::ite rator itFile = NULL;
map<int, CCardRecord>::i terator itRec = NULL;
for ( itFile = m_FileIndex.beg in(); itFile != m_FileIndex.end (); ++itFile )
{
for (itRec = itFile->second.m_RecIn dex.begin(); itRec !=
itFile->second.m_RecIn dex.end(); ++itRec)
{
nSFID = itFile->first;
nRecNo = itRec->second.m_nRecN o;
nRecLen = itRec->second.m_nRecL en;
pData = itRec->second.m_ByteA rray;
bRet = m_pCardReader->ReadRecord((BY TE)nSFID, (BYTE)nRecNo, pData, nRecLen);
if ( ! bRet )
return bRet;
}
}
Jul 22 '05 #5
Vince wrote:
"Victor Bazarov" <v.********@com Acast.net> a écrit dans le message de news:
GD************* ******@newsread 1.mlpsca01.us.t o.verio.net...
Vince wrote:
d:\RATP\Born e_FROM_SCRATCH_ 200105\CardObj. cpp(159): error C2440: '=' :
cannot convert from 'std::vector<_T y>' to 'BYTE *'
with
[
_Ty=BYTE
]
WHERE? There is no assignment in your code fragment except to 'csValue'
and 'pTmp', none of which is a vector.

You said 'm_pData' was a vector. Is it? Or is it a pointer to a vector?
Did you leave the rest of the function intact? Did you change it? If you
did change it, how?

And don't top-post, please. Thank you.

[...]

It is located somewhere else :

BYTE* pData = NULL;
map<int, CCardFile>::ite rator itFile = NULL;
map<int, CCardRecord>::i terator itRec = NULL;
for ( itFile = m_FileIndex.beg in(); itFile != m_FileIndex.end (); ++itFile )
{
for (itRec = itFile->second.m_RecIn dex.begin(); itRec !=
itFile->second.m_RecIn dex.end(); ++itRec)
{
nSFID = itFile->first;
nRecNo = itRec->second.m_nRecN o;
nRecLen = itRec->second.m_nRecL en;
pData = itRec->second.m_ByteA rray;
I suppose I have to guess again, don't I? What's wrong? Couldn't you
somehow mark the line to which the error message relates?

Anyway, you made m_ByteArray a vector<BYTE>, right? Well, you need to
fix the rest of your code to reflect that. In particular, here you can
probably do

pData = & (itRec->second.m_ByteA rray[0]);
bRet = m_pCardReader->ReadRecord((BY TE)nSFID, (BYTE)nRecNo, pData, nRecLen);
if ( ! bRet )
return bRet;
}
}


But in any case, I strongly recommend you to rethink the implementation of
all your functions. For example, you probably don't need 'm_nRecLen' any
more because the vector keeps its own size. And so on...

Victor
Jul 22 '05 #6
Indeed it would work, although, the right way to do it, if you are
working with STL is to use the "copy" function.
For example:
vector < BYTE > Vect;
copy(Vect.begin ()+nOffset, Vect.begin()+nO ffset+nDataSize , pTmp);

Now let's complicate it.
Assuming you to replace the "vector" with a "list".
The copy fucntion receives 3 iterators so it is only logical we can use
list.
But, the with a list iterator we cannot use the '+' operator, so we
will operator overloading:

typedef list<BYTE> tSequent;
tSequent::itera tor operator+(tSequ ent::iterator it, int n)
{
for (int i=0; i<n; ++i) {
it++;
}
return it;
}

Now we don't even have to change our function. We can still use this
sentence:
copy(Vect.begin ()+nOffset, Vect.begin()+nO ffset+nDataSize , pTmp);

only Vect is a "list"...

P.S.
Effiency: using a vector is a lot more efficient. Using the copy
function with a vector instead the memcpy is a lot _less_ efficient.
The reason is that the copy function have to copy BYTE BYTE, as opposed
to coping the whole segment at once with the memcpy.

Jul 22 '05 #7
gMorphus wrote:
[..] Effiency: using a vector is a lot more efficient. Using the copy
function with a vector instead the memcpy is a lot _less_ efficient.
The reason is that the copy function have to copy BYTE BYTE, as opposed
to coping the whole segment at once with the memcpy.


Actually, this all is not necessarily so. For all we know, 'std::copy'
can be specialised for vectors to perform memcpy under the covers...
Jul 22 '05 #8
gMorphus wrote:
[<<<snip]
Effiency: using a vector is a lot more efficient. Using the copy
function with a vector instead the memcpy is a lot _less_ efficient.
The reason is that the copy function have to copy BYTE BYTE, as opposed
to coping the whole segment at once with the memcpy.


Nothing whatsoever requires a standard vector of POD types to do so.

Also, counting the fact that the full implementation of std::copy is most
probably visible to the compiler, it is not too far fetched to assume that
in certain cases (if inlining happens) std::copy will beat the hell out of
memcpy in speed. Of course this is true as long as memcpy is not a compiler
intrinsic, in which case it may be so unpolitely fast that no code can be
faster. And of course, as Victor has pointed out, std::copy and memcpy can
just be the very same functions.

So in a short time we have given acceptable (or at least a tiny bit
convincing) arguments for std::copy being way slower than memcpy, way faster
than memcpy and the same as memcpy. Now I guess all we need is to remember
that Michael Jackson, who is not in jail and not on bail. Or Knuth. They
have placed the root of evil to a very different place than the current US
government. They were pointing out, that premature optimization is the root
of all evil. Which it may not be, but it surely is a damn dumb waste of
time guesswork to optimize code, wich has not been written yet, unknown if
it is slow or not, unknown what causes it to be slow if it is... It is as
reasonable spending of ones' time as me thinking about what should I wear on
my date with Claudia Schiffer.

Sorry for the (kinda) outburts, nothing personal. I just wanted to point
out that guesswork is evil. Especially if people believe it.

--
WW aka Attila
:::
Constant change is here to stay
Jul 22 '05 #9
Ok, after a little investigation I have realized that my solution is
not good.
That operator overlaoding wouldn't work for vector because the type
will be a "const unsigned char *" - which you cannot overload.

Moreover, I think the copy function _cannot_ be overloaded. The reason
for is the same - the vector iterator is simply a pointer, which you
cannot be overloaded or specialised.

Am I right?

Jul 22 '05 #10

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

Similar topics

3
4143
by: klaas | last post by:
the following code gives rise to the beneath error message, only when a matrix object is instantiated as matrix<bool>, not with matrix<float>: /*returns a reference to the object at position (Row,Col) in matrix*/ template <class num_type,template <class T> class functor> num_type & matrix<num_type,functor >::operator()(const int Row,const int Col) {if (Row<rows && Col<cols) {vector<num_type> & x=matrix_core; //num_type & y=x;<-is also...
3
3461
by: Alexandros | last post by:
Hi. How can I create a vector<bool> efficiently from a char* or a vector<char> ? For example, if char* c == (8,10) I want vector<bool> v to be: (0000100000001010)
4
5317
by: Jeff Paciga | last post by:
I have been reading about the problems associated with vector<bool>. Unfortunately, the usual work-arounds aren't viable for me, but I have never seen anyone mention using a class that behaves like a bool: class FakeBool { public: FakeBool() : b_(false) {} FakeBool(bool b) : b_(b) {} operator bool() { return b_; }
12
1557
by: Christian Roth | last post by:
Hello, I am merely asking this for my own understanding: Processing instruction's data part is not entity-aware, i.e. character and numercial entities are not resolved at parsing time. E.g., <?mypi &lt;par/&gt; ?> delivers as data part the String(!) "&lt;par/&gt;".
16
2401
by: call_me_anything | last post by:
why is the following not allowed : vector <Base *vec_of_base; vector <Derived *vec_of_derived; vec_of_base = vec_of_derived; Note : The following is allowed :
11
4585
by: mathieu | last post by:
Hello, I would like to implement a 'vector<uint12_t>' structure, where uint12_t is a 12bits unsigned integer. AFAIK I need to completely duplicate the implementation of let say vector<booland adapt the code to suit my need. I cannot find out if there is way for me to reuse my vendor std::vector ? Suggestions welcome, Mathieu
8
3985
by: barcaroller | last post by:
I have a pointer to a memory block. Is there a way I can map a vector<Tto this memory block? I would like to take advantage of the powerful vector<T> member functions. Please note that I cannot copy the data into the vector because the memory block is used/read by other objects.
2
2608
by: vikasetrx | last post by:
any idea how we can cast from std::vector types. e.g.. i am using the win32 API RegQueryValueEx(). the out buffer it takes should be LPBYTE. i used the code as shown, TCHAR * buf = new TCHAR;
2
12192
by: blueflyy | last post by:
Hello, I wish to initialize a vector<BYTE> from an existing BYTE array. Simple example: BYTE buf = {0,1,2,3,4,5}; std::vector<BYTE> vec(buf, buf + sizeof(buf)/sizeof(buf));
0
8840
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8730
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9215
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9131
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8007
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6669
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5981
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4484
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
2576
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.