473,396 Members | 1,836 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,396 software developers and data experts.

BYTE* array into a list

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
6 6236

"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
"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
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
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

"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

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: intrepid_dw | last post by:
Hello, all. I've created a C# dll that contains, among other things, two functions dealing with byte arrays. The first is a function that returns a byte array, and the other is intended to...
9
by: Charles Law | last post by:
Suppose I have a structure Private Structure MyStruct Dim el1 As Byte Dim el2 As Int16 Dim el3 As Byte End Structure I want to convert this into a byte array where
2
by: Larry | last post by:
I have a Byte Array Dim A1() as byte = {1,2,3,4,9,9,9,11,12,13,14,9,9,9} I want to find the location of the first occurance of the byte sequence {9,9,9}. Is there a built in Framework class...
6
by: moondaddy | last post by:
I'm writing an app in vb.net 1.1 and need to convert a byte array into a string, and then from a string back to a byte array. for example Private mByte() as New Byte(4){11,22,33,44} Now how...
5
by: Gerrit | last post by:
Hi all, I'm getting an OutOfMemoryException when I initialize a byte array in C# like this: Byte test = new Byte; I'm using ASP.NET 2.0. In ASP.Net 1.1 it works fine. So what am I doing
4
by: Rainer Queck | last post by:
Hi NG, is there a way to copy a buffer pointed to by a IntPtr directly into a two dimensional byte-array? I tried this, what obviously doesn't work: byte image = new byte; IntPtr p =...
17
by: =?Utf-8?B?U2hhcm9u?= | last post by:
Hi Gurus, I need to transfer a jagged array of byte by reference to unmanaged function, The unmanaged code should changed the values of the array, and when the unmanaged function returns I need...
2
by: Nag76 | last post by:
I have a requirement in the C# app to find if a byte exists as part of byte array and take decision based on that. I came across few of two ways to do that. Use the basic byte Array and use...
0
by: laszlobarta | last post by:
Hello, For saving 1 hour of your precious time use this function for parsing a text to a byte array: /// <summary> /// Converts the string representation of an array of numbers in...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
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...
0
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...

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.