M
ma740988
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.
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
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.
wiith a function pointer or function object.From the looks of things I might be better off using std::find_if()
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