C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

how to calculate the difference between 2 addresses ?

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
In a little while
Guest





PostPosted: Sun Nov 19, 2006 10:11 am    Post subject: how to calculate the difference between 2 addresses ? Reply with quote



thanks
Back to top
Salt_Peter
Guest





PostPosted: Sun Nov 19, 2006 10:11 am    Post subject: Re: how to calculate the difference between 2 addresses ? Reply with quote



In a little while wrote:
Quote:
thanks

That depends of the types at the addresses involved. If you attempt to
calculate the offset of 2 integers, for example, you need to substract
their corresponding address and then mutiply by sizeof(int) in order to
obtain a byte-size quantitative value (if thats what you are getting
at).

An object's pointer is bound to its type. Thats how a pointer does
++ptr to find the next element.
So you need to explain what you mean by difference between 2 addresses,
because in C++ speak, that means "in terms of the objects involved".

And you can forget using void* for that too.

#include <iostream>

template< typename T >
size_t difference(const T& r_lhs, const T& r_rhs)
{
return (&r_lhs - &r_rhs) * sizeof(T);
}

int main()
{
int m(0);
int* p_m = &m;
std::cout << "p_m = " << p_m << std::endl;
int o(0);
int* p_o = &o;
std::cout << "p_o = " << p_o << std::endl;

std::cout << "difference(p_m, p_o) in bytes: ";
std::cout << difference(p_m, p_o) << std::endl;
return 0;
}

/*
p_m = 0x7fff628c8c54
p_o = 0x7fff628c8c44
difference(p_m, p_o) in bytes: 16
*/

Please don't be confused by the above program. This is not something a
C++ programmer needs to do in typical C++ code. Also, a programmer
never relies on a primitive to have some given size.
Back to top
rTrenado
Guest





PostPosted: Sun Nov 19, 2006 10:12 am    Post subject: Re: how to calculate the difference between 2 addresses ? Reply with quote



Address1 - Address2?

In a little while wrote:
> thanks
Back to top
John Carson
Guest





PostPosted: Sun Nov 19, 2006 10:12 am    Post subject: Re: how to calculate the difference between 2 addresses ? Reply with quote

"In a little while" <cppcoder1234567890 (AT) yahoo (DOT) com> wrote in message
news:1163917662.107646.326020 (AT) h48g2000cwc (DOT) googlegroups.com
Quote:
thanks

Cast each of them to an integer type and perform the subtraction.

To cover ever possible address value, you will need a pointer-sized integer
type and it will need to be unsigned.

Since it is unsigned, you can only subtract the smaller from the larger, so
you will need to do an if() test to establish which is smaller before doing
the subtraction.

PS. It aids readability if you repeat your question in the body of your
post.

--
John Carson
Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.