473,503 Members | 2,178 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

comparing two structs .. revisited

There's a need for me to move around at specified offsets within
memory. As as a result - long story short - unsigned char* is the type
of choice.

At issue: Consider the case ( test code ) where I'm comparing two
structs. The struct test1 has information with regards to data_size
and pointer
to address. The struct test2 has information with regards to data_size
and value. I will compare test1 and test2. For each matching data
size, I'll will look within address (ptr_addr) in test1 for the
appropriate value. In this case 0xA9000000. To simulate this, I've put
together test code below.

Trouble is Visual Studio. NET complains about the find operation in
the compare function claiming :

" binary '==' : no operator found which takes a left-hand operand of
type 'const std::allocator<_Ty>::value_type' (or there is no
acceptable conversion)"

That puzzles me, since I'm doing _pretty much the exact same thing on
another machine - not readily accessible.

So now:

# include <iostream>
# include <algorithm>
# include <vector>
# include <string>

using namespace std;

typedef unsigned int uint_type;
typedef unsigned char* ADDR_TYPE;

struct test1
{
unsigned int data_size;
ADDR_TYPE ptr_addr;
test1(
uint_type data_size,
ADDR_TYPE ptr_addr
)
: data_size(data_size)
, ptr_addr(ptr_addr)
{}
};
struct test2
{
unsigned int data_size;
unsigned int value;
test2(
uint_type data_size,
uint_type value
)
: data_size(data_size)
, value(value)
{}
};

bool operator==( const test2& lhs, const test1& rhs ) {
ADDR_TYPE rhs_end = rhs.ptr_addr + rhs.data_size;
return
( lhs.data_size == rhs.data_size &&
find( rhs.ptr_addr, rhs_end, lhs.value )
!= rhs_end );
}
#if 0
bool operator==( const test2& lhs, const test1& rhs ) {
return rhs == lhs;
}
#endif
void compare( const vector<test1>& vec, const test2& s)
{
vector<test1>::const_iterator it = find( vec.begin(), vec.end(), s );
if ( it != vec.end() ) {
// found a test2 object that matches 's'
cout << " FOUND " << endl;
}
else {
// none there
}

}
int main()
{
ADDR_TYPE ptr_addr = new unsigned char [ 0x200000 ];
std::fill ( ptr_addr, ptr_addr + 0x200000, 0xA9000000);

typedef std::vector<test1> TEST1_VEC;
TEST1_VEC t1_vec;
test1 t1a(0x200000, ptr_addr );
test1 t1b(0x200000, ptr_addr );
t1_vec.push_back(t1a);
t1_vec.push_back(t1b);
test2 t2(0x200000, 0xA9000000 );
compare ( t1_vec, t2 );
}
In addition, within the code on the machine - that's not readily
accessible - I had to cast to unsigned int pointer before the compare
function returned true. i.e. I had to do:

find( (unsigned int*) rhs.ptr_addr, (unsigned int*)rhs_end, lhs.value )

!= (unsigned int*)rhs_end );

but it worked.
From the looks of things I might be better off using std::find_if()

wiith a function pointer or function object.
If that's the case could I get a source snippet on this?

I get the feeling the std::find() function is only guaranteed to
compile if a T is being searched within a container of T.
I'm still not sure why this would work on antother machine though (
though I might need to check again - to ensure there's no noticable
difference )

Not sure what I'm missing here.
Thanks in advance

Feb 7 '06 #1
5 2205
In article <11**********************@o13g2000cwo.googlegroups .com>,
"ma740988" <ma******@gmail.com> wrote:
There's a need for me to move around at specified offsets within
memory. As as a result - long story short - unsigned char* is the type
of choice.

At issue: Consider the case ( test code ) where I'm comparing two
structs. The struct test1 has information with regards to data_size
and pointer
to address. The struct test2 has information with regards to data_size
and value. I will compare test1 and test2. For each matching data
size, I'll will look within address (ptr_addr) in test1 for the
appropriate value. In this case 0xA9000000. To simulate this, I've put
together test code below.

Trouble is Visual Studio. NET complains about the find operation in
the compare function claiming :

" binary '==' : no operator found which takes a left-hand operand of
type 'const std::allocator<_Ty>::value_type' (or there is no
acceptable conversion)"
You need to fix the part inside the "#if 0" block so that test1 objects
can be compared to test2 objects.

But that's the least of your problems.
That puzzles me, since I'm doing _pretty much the exact same thing on
another machine - not readily accessible.

So now:

# include <iostream>
# include <algorithm>
# include <vector>
# include <string>

using namespace std;

typedef unsigned int uint_type;
typedef unsigned char* ADDR_TYPE;

struct test1
{
unsigned int data_size;
ADDR_TYPE ptr_addr;
test1(
uint_type data_size,
ADDR_TYPE ptr_addr
)
: data_size(data_size)
, ptr_addr(ptr_addr)
{}
};
struct test2
{
unsigned int data_size;
unsigned int value;
test2(
uint_type data_size,
uint_type value
)
: data_size(data_size)
, value(value)
{}
};

bool operator==( const test2& lhs, const test1& rhs ) {
ADDR_TYPE rhs_end = rhs.ptr_addr + rhs.data_size;
return
( lhs.data_size == rhs.data_size &&
find( rhs.ptr_addr, rhs_end, lhs.value )
!= rhs_end );
The problem with this find is basically like the 'fill' problem below.
As written, it checks if any 'unsigned char' in the ptr_addr equals the
value contained in an 'unsigned int' (in this case 0xa9000000, and there
is no way an unsigned char is going to contain 0xa9000000.)
}
#if 0 Fix the below and remove the preprocessor

bool operator==( const test1& lhs, const test2& rhs ) {
bool operator==( const test2& lhs, const test1& rhs ) {
return rhs == lhs;
}
#endif
void compare( const vector<test1>& vec, const test2& s)
{
Nothing is wrong with this function, as long as the op==s are defined
properly.
vector<test1>::const_iterator it = find( vec.begin(), vec.end(), s );
if ( it != vec.end() ) {
// found a test2 object that matches 's'
cout << " FOUND " << endl;
}
else {
// none there
}

}
int main()
{
ADDR_TYPE ptr_addr = new unsigned char [ 0x200000 ];
std::fill ( ptr_addr, ptr_addr + 0x200000, 0xA9000000);
The code above creates a "fill<unsigned char*, unsigned int>" function,
and the problems begin... A char cannot hold the value 0xA9000000 so
what it ends up with is either 0xA900 or 0x0000 depending on the
endianness of the computer (I think, on my system each char ended up
with 0x0000.)

typedef std::vector<test1> TEST1_VEC;
TEST1_VEC t1_vec;
test1 t1a(0x200000, ptr_addr );
test1 t1b(0x200000, ptr_addr );
t1_vec.push_back(t1a);
t1_vec.push_back(t1b);
test2 t2(0x200000, 0xA9000000 );
compare ( t1_vec, t2 );
}
In addition, within the code on the machine - that's not readily
accessible - I had to cast to unsigned int pointer before the compare
function returned true. i.e. I had to do:

find( (unsigned int*) rhs.ptr_addr, (unsigned int*)rhs_end, lhs.value )

!= (unsigned int*)rhs_end );

but it worked.
It worked because then you were comparing apples to apples.
I get the feeling the std::find() function is only guaranteed to
compile if a T is being searched within a container of T.
Not at all. The problem is when the value can hold things that the items
in the container can't possible hold.
I'm still not sure why this would work on antother machine though (
though I might need to check again - to ensure there's no noticable
difference )

Not sure what I'm missing here.


You have a huge problem with comparing different types, but the problems
are hidden because the compiler is trying to be nice and automatically
typecast them for you rather than throwing up a diagnostic.

Simply fixing your op== as mentioned above, changing ADDR_TYPE to an
unsigned int*, and making a new unsigned int array (rather than an
unsigned char array) in main fixes the code.

--
Magic depends on tradition and belief. It does not welcome observation,
nor does it profit by experiment. On the other hand, science is based
on experience; it is open to correction by observation and experiment.
Feb 7 '06 #2

Thanks Daniel

Feb 7 '06 #3
void compare( const vector<test1>& vec, const test2& s)
{


Nothing is wrong with this function, as long as the op==s are defined
properly.
vector<test1>::const_iterator it = find( vec.begin(), vec.end(), s );
if ( it != vec.end() ) {
// found a test2 object that matches 's'
cout << " FOUND " << endl;
}
else {
// none there
}

}

A question for you. Indeed a lot of this amounts to a tendency for me
to mix to mix apples with oranges. The trouble with find is that
find stops after finding first match in the vector. I'd like to revise
that such that.

I'll search the entire vector for all matching such that:
I'll look at the address for the vector element. run from begin to end
on the address while comparing it with the desired value. The key
here is dont want to stop after the first match. While doing this I'll
keep track via a counter of the number of elements found.
So if vec<test1> was four. Count will be four. I'm experimenting with
an approahc here (utlizing count_if )but I suspect I could use your
expertise.

Feb 8 '06 #4
In article <11**********************@z14g2000cwz.googlegroups .com>,
"ma740988" <ma******@gmail.com> wrote:
void compare( const vector<test1>& vec, const test2& s)
{


Nothing is wrong with this function, as long as the op==s are defined
properly.
vector<test1>::const_iterator it = find( vec.begin(), vec.end(), s );
if ( it != vec.end() ) {
// found a test2 object that matches 's'
cout << " FOUND " << endl;
}
else {
// none there
}

}

A question for you. Indeed a lot of this amounts to a tendency for me
to mix to mix apples with oranges. The trouble with find is that
find stops after finding first match in the vector. I'd like to revise
that such that.

I'll search the entire vector for all matching such that:
I'll look at the address for the vector element. run from begin to end
on the address while comparing it with the desired value. The key
here is dont want to stop after the first match. While doing this I'll
keep track via a counter of the number of elements found.
So if vec<test1> was four. Count will be four. I'm experimenting with
an approahc here (utlizing count_if )but I suspect I could use your
expertise.


There is an standard algorithm called "count" and another called
"count_if". Check them out.

<http://www.sgi.com/tech/stl/stl_index.html>

--
Magic depends on tradition and belief. It does not welcome observation,
nor does it profit by experiment. On the other hand, science is based
on experience; it is open to correction by observation and experiment.
Feb 8 '06 #5
There is an standard algorithm called "count" and another called
"count_if". Check them out.

<http://www.sgi.com/tech/stl/stl_index.html>


Actually I found just the right algorithm for what I need to do.

int main()
{
uint_type* ptr_addr = new uint_type [ 0x200000 ];
std::fill ( ptr_addr, ptr_addr + 0x200000, 0xA9000000);

uint_type *ptr_end = ptr_addr + 0x200000;
if ( std::search_n( ptr_addr, ptr_end, 0x200000, 0xA9000000 ) !=
ptr_end )
cout << " YES " << endl;
}

I'm all hooked on the C++ algorithms and containers now :). Real soon
I'll be able to put together the type of example you gave me two/three
weeks ago. Well I wish.

Thanks again.

Feb 8 '06 #6

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

Similar topics

5
3111
by: Paminu | last post by:
Why make an array of pointers to structs, when it is possible to just make an array of structs? I have this struct: struct test { int a; int b;
5
2232
by: Bob Gregory | last post by:
Hi all, I'm utter C# newbie, do be gentle. VS2005 Express refuses point blank to install on my box, so I'm stuck with C# 1.0 unless someone can point me to a C#2.0 compiler elsewhere. I have...
2
1127
by: Richard | last post by:
In a forum I made in ASP Classic I had the following code. ------------------------------- If Session("dtmLastVisit") = "" AND Request.Cookies("DigiForum")("LastVisit") <> "" Then...
12
25953
by: barcaroller | last post by:
Is it legal to compare the contents of two multi-field variables (of the same struct) using "==" and "!="? struct { int a; int b; } x,y; ...
19
3808
by: Ole Nielsby | last post by:
How does the GetHashCode() of an array object behave? Does it combine the GetHashCode() of its elements, or does it create a sync block for the object? I want to use readonly arrays as...
61
3689
by: Marty | last post by:
I am new to C# and to structs so this could be easy or just not possible. I have a struct defined called Branch If I use Branch myBranch = new Branch(i); // everything works If I use Branch...
2
5488
by: Zytan | last post by:
I wanted to do something simple like checking my own Color variable against Color.Black, and == returned false, even though their RGB values were all 0! Of course, there's the A (alpha) value, but...
29
2741
by: Dom | last post by:
I'm really confused by the difference between a Struct and a Class? Sometimes, I want just a group of fields to go together. A Class without methods seems wrong, in that it carries too much...
17
8616
by: junky_fellow | last post by:
Guys, Is it a good way to compare two structures for equality by using "memcmp" ? If not, what could be the problems associated with this ? And what is the correct method of doing this ? ...
0
7205
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
7093
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
7287
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,...
0
7353
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...
0
7468
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
3180
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3170
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
747
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
401
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.