473,323 Members | 1,537 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,323 software developers and data experts.

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::GetRecData(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 9018
Vince wrote:
I have replaced my BYTE* by a vector<BYTE> and before I used to do :

void CCardRecord::GetRecData(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_FROM_SCRATCH_200105\CardObj.cpp(159) : error C2440: '=' :
cannot convert from 'std::vector<_Ty>' to 'BYTE *'
with
[
_Ty=BYTE
]
"Victor Bazarov" <v.********@comAcast.net> a écrit dans le message de news:
5u******************@newsread1.mlpsca01.us.to.veri o.net...
Vince wrote:
I have replaced my BYTE* by a vector<BYTE> and before I used to do :

void CCardRecord::GetRecData(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_FROM_SCRATCH_200105\CardObj.cpp(159) : error C2440: '=' :
cannot convert from 'std::vector<_Ty>' 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.********@comAcast.net> a écrit dans le message de news:
5u******************@newsread1.mlpsca01.us.to.veri o.net...
Vince wrote:
I have replaced my BYTE* by a vector<BYTE> and before I used to do :

void CCardRecord::GetRecData(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


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

"Victor Bazarov" <v.********@comAcast.net> a écrit dans le message de news:
GD*******************@newsread1.mlpsca01.us.to.ver io.net...
Vince wrote:
d:\RATP\Borne_FROM_SCRATCH_200105\CardObj.cpp(159) : error C2440: '=' :
cannot convert from 'std::vector<_Ty>' 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.********@comAcast.net> a écrit dans le message de
news: 5u******************@newsread1.mlpsca01.us.to.veri o.net...
Vince wrote:

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

void CCardRecord::GetRecData(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


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

It is located somewhere else :

BYTE* pData = NULL;
map<int, CCardFile>::iterator itFile = NULL;
map<int, CCardRecord>::iterator itRec = NULL;
for ( itFile = m_FileIndex.begin(); itFile != m_FileIndex.end(); ++itFile )
{
for (itRec = itFile->second.m_RecIndex.begin(); itRec !=
itFile->second.m_RecIndex.end(); ++itRec)
{
nSFID = itFile->first;
nRecNo = itRec->second.m_nRecNo;
nRecLen = itRec->second.m_nRecLen;
pData = itRec->second.m_ByteArray;
bRet = m_pCardReader->ReadRecord((BYTE)nSFID, (BYTE)nRecNo, pData, nRecLen);
if ( ! bRet )
return bRet;
}
}
Jul 22 '05 #5
Vince wrote:
"Victor Bazarov" <v.********@comAcast.net> a écrit dans le message de news:
GD*******************@newsread1.mlpsca01.us.to.ver io.net...
Vince wrote:
d:\RATP\Borne_FROM_SCRATCH_200105\CardObj.cpp(1 59): error C2440: '=' :
cannot convert from 'std::vector<_Ty>' 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>::iterator itFile = NULL;
map<int, CCardRecord>::iterator itRec = NULL;
for ( itFile = m_FileIndex.begin(); itFile != m_FileIndex.end(); ++itFile )
{
for (itRec = itFile->second.m_RecIndex.begin(); itRec !=
itFile->second.m_RecIndex.end(); ++itRec)
{
nSFID = itFile->first;
nRecNo = itRec->second.m_nRecNo;
nRecLen = itRec->second.m_nRecLen;
pData = itRec->second.m_ByteArray;
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_ByteArray[0]);
bRet = m_pCardReader->ReadRecord((BYTE)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()+nOffset+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::iterator operator+(tSequent::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()+nOffset+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

"White Wolf" <wo***@freemail.hu> wrote in message
news:cs**********@phys-news1.kolumbus.fi...
gMorphus wrote:
[<<<snip] Now I guess all we need is to remember that Michael Jackson, who is not
in jail and not on bail.
Umm... what? Can you diagram that sentence? Or at least complete it? :-)
Or Knuth.
Ditto.
They have placed the root of evil to a very different place than the
current US government.
Tread lightly there, friend. We may not all like Bush/Cheney, but it's
still OUR government.

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


Apology accepted, I guess. ;-)

-Howard
"Life is like a donut. With sprinkles. Mmmm...sprinkles!"
Jul 22 '05 #11
gMorphus wrote:
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?


No, you aren't.

1.) Copy can be overloaded, since it is *noty* an operator function. Now it
may be a very bad idea to overload it, but it can be done. Although it
cannot (eh, must not) be overloaded as std::copy.

2.) The vector iterator *may* be a pointer. In more and more Standard
Library implementations it is more and more frequently not apointer. And
of course any function can be overloaded and any template can be specialized
for pointers. Operators cannot be. It may be a very bad idea to do so with
"well known functions", but it can be done:

void foo(int);
void foo(char const *);
void foo(std::vector<char>::const_iterator);

Of course, if your iterator implementation happens to use typedefs to create
a type alias const_iterator for char const *, the above code will not
compile.

You do have good point, but your wording uses too strict statements:

"That operator overlaoding wouldn't work for vector because the type
/may/ be a "const unsigned char *" - /and/ you cannot overload /an operator
for that type/."

--
WW aka Attila
:::
SUSHIDO: The way of the Tuna.
Jul 22 '05 #12
White Wolf wrote:
[...]
1.) Copy can be overloaded, since it is *noty* [...]


I think it's spelled *naughty*

:-) Sorry, couldn't resist...
Jul 22 '05 #13
Howard wrote:
"White Wolf" <wo***@freemail.hu> wrote in message
news:cs**********@phys-news1.kolumbus.fi...
gMorphus wrote:
[<<<snip]

Now I guess all we need is to remember that Michael Jackson, who is not
in jail and not on bail.


Umm... what? Can you diagram that sentence? Or at least complete it?
:-)


Read Herb Sutter more often. ;-) Did you at least make an attempt to ask
Google about Michel Jackson and optimization? Hints:

"Principles of Program Design" by M.A. Jackson, Associated Press 1975
ISBN 0 12 379050 6

Michael Jackson, rules of optimization:

Rule 1. Don't do it.
Rule 2. (for experts only) Don't do it yet.
Or Knuth.


Ditto.


Not knowing Knuth as a programmer is excuseable. But telling it in public
is not wise. First hit on Google:

http://www-cs-faculty.stanford.edu/~knuth/

I feel like the father, whose 25 years old sun calls out from the toilet: I
have finished, you can come!
They have placed the root of evil to a very different place than the
current US government.


Tread lightly there, friend. We may not all like Bush/Cheney, but it's
still OUR government.


Yours my friend. Not mine. Mine is worse than your, but at least has no
nucular weapons, or any other weapons of mass destruction, with
pronounciation errors or without.
Sorry for the (kinda) outburts, nothing personal. I just wanted to
point out that guesswork is evil. Especially if people believe it.


Apology accepted, I guess. ;-)


Understanding what was said may have been more benefitial.

--
WW aka Attila
:::
Nothing makes your clothes go out of fashion faster than getting a raise.
Jul 22 '05 #14
Victor Bazarov wrote:
White Wolf wrote:
[...]
1.) Copy can be overloaded, since it is *noty* [...]


I think it's spelled *naughty*

:-) Sorry, couldn't resist...


Ohm I couldn't resist. (Bad joke, but that's all I can do today.)

--
WW aka Attila
:::
Birthdays are good for you. Statistics show that the people who have the
most live the longest.
Jul 22 '05 #15

"White Wolf" <wo***@freemail.hu> wrote in message
news:cs**********@phys-news1.kolumbus.fi...
Howard wrote:
"White Wolf" <wo***@freemail.hu> wrote in message
news:cs**********@phys-news1.kolumbus.fi...
gMorphus wrote:
[<<<snip]
Now I guess all we need is to remember that Michael Jackson, who is not
in jail and not on bail.


Umm... what? Can you diagram that sentence? Or at least complete it?
:-)


Read Herb Sutter more often. ;-) Did you at least make an attempt to ask
Google about Michel Jackson and optimization? Hints:


Sorry. I thought "remember that..." meant something like "remember that you
need to lift the toilet seat...". I suppose you meant it like "remember
that Michael Jackson guy?" As I read it, it wasn't a complete sentence.
"Principles of Program Design" by M.A. Jackson, Associated Press 1975
ISBN 0 12 379050 6

Michael Jackson, rules of optimization:

Rule 1. Don't do it.
Rule 2. (for experts only) Don't do it yet.
Or Knuth.
Ditto.


Not knowing Knuth as a programmer is excuseable. But telling it in public
is not wise. First hit on Google:


Again, I was commenting (joking) about your lack of complete sentences. "Or
Knuth" lacks something as sentences go.
http://www-cs-faculty.stanford.edu/~knuth/

I feel like the father, whose 25 years old sun calls out from the toilet:
I have finished, you can come!
They have placed the root of evil to a very different place than the
current US government.
Tread lightly there, friend. We may not all like Bush/Cheney, but it's
still OUR government.


Yours my friend. Not mine. Mine is worse than your, but at least has no
nucular weapons, or any other weapons of mass destruction, with
pronounciation errors or without.
Sorry for the (kinda) outburts, nothing personal. I just wanted to
point out that guesswork is evil. Especially if people believe it.


Apology accepted, I guess. ;-)


Understanding what was said may have been more benefitial.


(I thought the smiley faces were a clue I was just joking?)

--
WW aka Attila
:::
Nothing makes your clothes go out of fashion faster than getting a raise.

Jul 22 '05 #16
Howard wrote:
[SNIP]
Read Herb Sutter more often. ;-) Did you at least make an attempt to
ask Google about Michel Jackson and optimization? Hints:
Sorry. I thought "remember that..." meant something like "remember that
you need to lift the toilet seat...". I suppose you meant it like
"remember that Michael Jackson guy?" As I read it, it wasn't a complete
sentence.


Yeah. It also happens to me. It can be quite fun when you stare at your
own slides on a course. That is when I take a note to simplify the
language. :-)

[SNIP] Again, I was commenting (joking) about your lack of complete sentences.
"Or Knuth" lacks something as sentences go.


And deliberately so. ;-)

[SNIP]
Apology accepted, I guess. ;-)


Understanding what was said may have been more benefitial.


(I thought the smiley faces were a clue I was just joking?)


You were joking and I was serious about the part that please please don't do
guesswork.

--
WW aka Attila
:::
Sorry - yesterday was the deadline for all complaints.
Jul 22 '05 #17

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

Similar topics

3
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...
3
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
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...
12
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., ...
16
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
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...
8
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...
2
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...
2
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
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.