473,842 Members | 1,415 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_arra y; 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 6324

"Harry Overs" <ho****@dera.go v.uk> wrote in message
news:cj******** **@hamble.qinet iq.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_arra y; 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_arra y;
nByteCount++)
{
char pByte = *(pByteArray + nByteCount);
l.push_back(pBy te);
}
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.go v.uk> wrote in message
news:cj******** **@hamble.qinet iq.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_arra y; 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.assig n( pByteArray, pByteArray+entr ies_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************ *************** *******@vecerin a.com> wrote in
message news:cj******** **@newshispeed. ch...
"Harry Overs" <ho****@dera.go v.uk> wrote in message
news:cj******** **@hamble.qinet iq.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_arra y; 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.assig n( pByteArray, pByteArray+entr ies_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.go v.uk> wrote in message
news:cj******** **@hamble.qinet iq.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_arra y; 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_arra y;
nByteCount++)
{
char pByte = *(pByteArray + nByteCount);
l.push_back(pBy te);
}
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.go v.uk> wrote in message
news:cj******** **@hamble.qinet iq.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_arra y;
nByteCount++)
{
char pByte = *(pByteArray + nByteCount);
l.push_back(pBy te);
}
}
};

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.go v.uk> wrote in message
news:cj******** **@hamble.qinet iq.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_arra y; 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(pByte Array, pByteArray+entr ies_in_array);
Jul 22 '05 #7

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

Similar topics

8
10726
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 receive a byte array as one of its parameters. The project is marked for COM interop, and that all proceeds normally. When I reference the type library in the VB6 project, and write the code to call the function that returns the byte array, it works
9
12695
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
4650
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 that will do this easily? I've tried messing with Array.IndexOf but can't seem to get it to indicate where the byte sequnce starts.
6
53739
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 do I convert it to: dim myStr as string = "11,22,33,44"
5
4369
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
11986
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 = <hardwareDeviceClass>.GetBufPtr();
17
7265
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 to show the array data to the end user. Can I do that? How?
2
5946
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 for loop to find the existence of the byte i am looking for (Or) Use some thing like List<byte> byteList and then "byteList.Contains((byte)byteVar)" .
0
4132
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 a specified base to an /// equivalent 8-bit unsigned integer array (byte). /// </summary> /// <param name="text">A System.String containing an array of numbers separated by '\r' or '\n' or '\t' or ' ' (space) or ','...
0
9876
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
9717
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
10950
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10617
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
10681
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
9459
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
7862
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
7045
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();...
2
4096
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.