By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,383 Members | 2,075 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,383 IT Pros & Developers. It's quick & easy.

BYTE* array into a list

P: n/a
My program needs to take a pointer to BYTE array (unsigned char*) and
convert it into a STL list so that each BYTE in the array has its own
element in the list, i.e. if the array has hundred bytes then the list needs
to have a hundred entries, at present when I try to do this each element in
my list points to the entire BYTE array, when what I really need is copies
of each single BYTE in its own part of the list.

I have used code similar to the following for when the list was only used
locally:

std::list<char> m_STLList;
for (int nByteCount = 0; nByteCount < entries_in_array; nByteCount++)
{
char pByte = *(pByteArray + nByteCount);
m_STLList.push_back(pByte);
}

however I now need the list to be returned and then the contents of it
deleted by a different method in another class.

Does anyone know how I can do this?

cheers,
Andy

Jul 22 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a

"Harry Overs" <ho****@dera.gov.uk> wrote in message
news:cj**********@hamble.qinetiq.com...
My program needs to take a pointer to BYTE array (unsigned char*) and
convert it into a STL list so that each BYTE in the array has its own
element in the list, i.e. if the array has hundred bytes then the list needs to have a hundred entries, at present when I try to do this each element in my list points to the entire BYTE array, when what I really need is copies
of each single BYTE in its own part of the list.

I have used code similar to the following for when the list was only used
locally:

std::list<char> m_STLList;
for (int nByteCount = 0; nByteCount < entries_in_array; nByteCount++)
{
char pByte = *(pByteArray + nByteCount);
m_STLList.push_back(pByte);
}

however I now need the list to be returned and then the contents of it
deleted by a different method in another class.

Does anyone know how I can do this?


This is not a question about lists, its a question about how you organise
your classes. For instance where to you want the list to be held, in the
current class, in the new class, in both or in neither? All of these are
possible but you need to say which you actually want.

For instance this might be what you want

class X
{
std::list<char> create_list()
{
std::list<char> l;
for (int nByteCount = 0; nByteCount < entries_in_array;
nByteCount++)
{
char pByte = *(pByteArray + nByteCount);
l.push_back(pByte);
}
return l;
}
};

Here the list is no longer part of the X class, instead the method just
creates a local list and returns it to whoever calls create_list(). Whoever
that is can then do whatever they like with the list, including deleteing
its contents.

If this doesn't seem right then post again, describing in a bit more detail
what you actually want.

john
Jul 22 '05 #2

P: n/a
"Harry Overs" <ho****@dera.gov.uk> wrote in message
news:cj**********@hamble.qinetiq.com...
My program needs to take a pointer to BYTE array (unsigned char*) and
convert it into a STL list so that each BYTE in the array has its own
element in the list, i.e. if the array has hundred bytes then the list
needs
to have a hundred entries, at present when I try to do this each element
in
my list points to the entire BYTE array, when what I really need is copies
of each single BYTE in its own part of the list.

I have used code similar to the following for when the list was only used
locally:

std::list<char> m_STLList;
for (int nByteCount = 0; nByteCount < entries_in_array; nByteCount++)
{
char pByte = *(pByteArray + nByteCount);
m_STLList.push_back(pByte); Doesn't this lead to a compile error?
}

however I now need the list to be returned and then the contents of it
deleted by a different method in another class.

Does anyone know how I can do this?


You can replace the body of the loop with:
m_STLList.push_back( pByteArray[nByteCount] );

Alternatively, replace the whole loop with:
m_STLList.assign( pByteArray, pByteArray+entries_in_array );
This said, an std::vector is likely to be more efficient than
an std::list to manipulate a sequence of bytes...
hth -Ivan
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
Brainbench MVP for C++ <> http://www.brainbench.com



Jul 22 '05 #3

P: n/a
cheers Ivan, I'll give this a go.

Andy

"Ivan Vecerina" <NO**********************************@vecerina.com > wrote in
message news:cj**********@newshispeed.ch...
"Harry Overs" <ho****@dera.gov.uk> wrote in message
news:cj**********@hamble.qinetiq.com...
My program needs to take a pointer to BYTE array (unsigned char*) and
convert it into a STL list so that each BYTE in the array has its own
element in the list, i.e. if the array has hundred bytes then the list
needs
to have a hundred entries, at present when I try to do this each element
in
my list points to the entire BYTE array, when what I really need is copies of each single BYTE in its own part of the list.

I have used code similar to the following for when the list was only used locally:

std::list<char> m_STLList;
for (int nByteCount = 0; nByteCount < entries_in_array; nByteCount++)
{
char pByte = *(pByteArray + nByteCount);
m_STLList.push_back(pByte);

Doesn't this lead to a compile error?
}

however I now need the list to be returned and then the contents of it
deleted by a different method in another class.

Does anyone know how I can do this?


You can replace the body of the loop with:
m_STLList.push_back( pByteArray[nByteCount] );

Alternatively, replace the whole loop with:
m_STLList.assign( pByteArray, pByteArray+entries_in_array );
This said, an std::vector is likely to be more efficient than
an std::list to manipulate a sequence of bytes...
hth -Ivan
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
Brainbench MVP for C++ <> http://www.brainbench.com


Jul 22 '05 #4

P: n/a
thanks for your reply, (bit of history) the program in question used to be
in C and has gradually been converted into C++. Also we have added
multi-threading into the program so what we are doing is creating a snapshot
of the array/list/vector at the current point and then returning it so that
another section of the code can then do some processing on it, whilst this
processing is happening the original list is been continually updated by the
main part of the program. Also I'm changing the array into a list or vector
as I have found that the program uses a lot more memory than expected and as
such I believe that the arrays aren't getting created, deleted and ammended
correctly (when I first came on to the project the start of the arrays were
actually getting lot due to people incrementing the original pointer)
So to answer your question the new list list will be held by the class that
calls the method.

Andy,

"John Harrison" <jo*************@hotmail.com> wrote in message
news:2s*************@uni-berlin.de...

"Harry Overs" <ho****@dera.gov.uk> wrote in message
news:cj**********@hamble.qinetiq.com...
My program needs to take a pointer to BYTE array (unsigned char*) and
convert it into a STL list so that each BYTE in the array has its own
element in the list, i.e. if the array has hundred bytes then the list needs
to have a hundred entries, at present when I try to do this each element

in
my list points to the entire BYTE array, when what I really need is copies of each single BYTE in its own part of the list.

I have used code similar to the following for when the list was only used locally:

std::list<char> m_STLList;
for (int nByteCount = 0; nByteCount < entries_in_array; nByteCount++)
{
char pByte = *(pByteArray + nByteCount);
m_STLList.push_back(pByte);
}

however I now need the list to be returned and then the contents of it
deleted by a different method in another class.

Does anyone know how I can do this?


This is not a question about lists, its a question about how you organise
your classes. For instance where to you want the list to be held, in the
current class, in the new class, in both or in neither? All of these are
possible but you need to say which you actually want.

For instance this might be what you want

class X
{
std::list<char> create_list()
{
std::list<char> l;
for (int nByteCount = 0; nByteCount < entries_in_array;
nByteCount++)
{
char pByte = *(pByteArray + nByteCount);
l.push_back(pByte);
}
return l;
}
};

Here the list is no longer part of the X class, instead the method just
creates a local list and returns it to whoever calls create_list().

Whoever that is can then do whatever they like with the list, including deleteing
its contents.

If this doesn't seem right then post again, describing in a bit more detail what you actually want.

john

Jul 22 '05 #5

P: n/a

"Harry Overs" <ho****@dera.gov.uk> wrote in message
news:cj**********@hamble.qinetiq.com...
thanks for your reply, (bit of history) the program in question used to be
in C and has gradually been converted into C++. Also we have added
multi-threading into the program so what we are doing is creating a snapshot of the array/list/vector at the current point and then returning it so that another section of the code can then do some processing on it, whilst this
processing is happening the original list is been continually updated by the main part of the program. Also I'm changing the array into a list or vector as I have found that the program uses a lot more memory than expected and as such I believe that the arrays aren't getting created, deleted and ammended correctly (when I first came on to the project the start of the arrays were actually getting lot due to people incrementing the original pointer)
So to answer your question the new list list will be held by the class that calls the method.

Andy,


In that case the code I suggested should work. But copying lists is
expensive so this variation might be better

class X
{
void create_list(std::list<char>& l)
{
l.clear();
for (int nByteCount = 0; nByteCount < entries_in_array;
nByteCount++)
{
char pByte = *(pByteArray + nByteCount);
l.push_back(pByte);
}
}
};

A reference to the list is passed in, instead of the list being returned by
value.

john
Jul 22 '05 #6

P: n/a

"Harry Overs" <ho****@dera.gov.uk> wrote in message
news:cj**********@hamble.qinetiq.com...
My program needs to take a pointer to BYTE array (unsigned char*) and
convert it into a STL list so that each BYTE in the array has its own
element in the list, i.e. if the array has hundred bytes then the list
needs
to have a hundred entries, at present when I try to do this each element
in
my list points to the entire BYTE array, when what I really need is copies
of each single BYTE in its own part of the list.

I have used code similar to the following for when the list was only used
locally:

std::list<char> m_STLList;
for (int nByteCount = 0; nByteCount < entries_in_array; nByteCount++)
{
char pByte = *(pByteArray + nByteCount);
m_STLList.push_back(pByte);
}

however I now need the list to be returned and then the contents of it
deleted by a different method in another class.


Much too complicated. Do this:

std::list<BYTE> m_STLList(pByteArray, pByteArray+entries_in_array);
Jul 22 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.