BYTE* array into a list

H

Harry Overs

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
 
J

John Harrison

Harry Overs said:
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
 
I

Ivan Vecerina

Harry Overs said:
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
 
H

Harry Overs

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,
 
H

Harry Overs

cheers Ivan, I'll give this a go.

Andy

Ivan Vecerina said:
Harry Overs said:
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
 
J

John Harrison

Harry Overs said:
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
 
A

Andrew Koenig

Harry Overs said:
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);
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,176
Messages
2,570,950
Members
47,503
Latest member
supremedee

Latest Threads

Top