Solved

Two floats on same page?

Posted on 2008-10-27
12
234 Views
Last Modified: 2010-04-15
Hi guys, I have a C and operating systems question.

I was recently asked how to determine if two floats reside within the same page, where page sizes are 10KB large, and they start at 0. Basically, implement the following method:

bool areOnSamePage( float * x , float * y ) {

}

To start, I believe this is done by checking how much memory space resides between both pointers? Is this correct? If so, how could I proceed?
0
Comment
Question by:Vense
  • 5
  • 3
  • 2
  • +2
12 Comments
 
LVL 53

Expert Comment

by:Infinity08
ID: 22816252
>> To start, I believe this is done by checking how much memory space resides between both pointers?

If you subtract one pointer from the other, what value do you get ?
0
 
LVL 84

Expert Comment

by:ozo
ID: 22816485
If the pointers are not elements of the same array object or one past the last element of the array object, the result of subtracting one pointer from the other is undefined.
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 22816529
ozo, this seems to be a platform dependent question, so I think we can assume that subtracting pointers is well defined on the platform in question (otherwise the question wouldn't make much sense).
0
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
LVL 84

Expert Comment

by:ozo
ID: 22816619
Your C implementation may support a non portable, non strictly conforming method of extracting the page from a pointer.
If so, you may then be able to compare them.
Even if subtracting pointers that are not elements of the same array object produces a sensible result, the it may still be difficult to distinguish between  pointers at the end of one page and the beginning of the next page
from pointers at the beginning and end of the same page.
0
 

Author Comment

by:Vense
ID: 22816834
I assume this could be done in two steps.

1. Get difference of pointers, and if it's greater than 10KB, then they must reside on separate pages.
2. If difference < 10KB, somehow figure out the page bounds for each pointer and see if they're equal.

I think?
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 22817554
What information do you have on the platform ?
Do pages start at memory addresses that are multiples of 10kB ?
0
 
LVL 23

Assisted Solution

by:Mysidia
Mysidia earned 100 total points
ID: 22817993
Subtracting the two pointers is not even a viable solution.
Suppose the object 'x'  points to resides at the very  end of a 10kb page
and the object 'y'  resides at the very beginning of the following page.

And even though the results of your pointer subtraction are undefined according to the C language definition,  you do it anyways

and you find that  with

ptrdiff_t  val = y - x;

val  is equal to   -sizeof(float)

what then?
If  you conclude that the two pointers are on the same page,  because the  integer representation of the subtraction is less than 10240
then your conclusion is in error.


You need an algorithm to  determine which page 'x'  is in,  or to at least find the boundary of the page x is in.

Your 10kb  page size has a base 2 representation of:   10100000000000b


What you really need to determine is if     x  > y     or  x < y

And if  x > y,  then does x   reside beyond the next multiple of your page size above y or not?

Or if  y > x,  then does y  reside beyond the next multiple of your page size above x or not?


Some pseudocode

ptrdiff_t  difference = x - y;

if ( difference < 0 ) {
  /*  y > x  */
  /*  now check   if   (int)y    is beyond  the next multiple of your page size *
   *  after x or not  */
 }
else if ( difference > 0 ) {
  /* x > y */
}
else {
  /* x == y */
}





0
 

Author Comment

by:Vense
ID: 22818220
Infinity08: No info on the platform. And yeah, the pages start at addr 0 and keep going up in multiples of 10KB.
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 22819856
>> And yeah, the pages start at addr 0 and keep going up in multiples of 10KB.

Then it's easyt o know what page a certain address is on. The modulo operator will be of great assistance ;)
0
 
LVL 84

Assisted Solution

by:ozo
ozo earned 100 total points
ID: 22820214
the shift or divide or bitwise and operator may be of greater assistance
0
 
LVL 53

Assisted Solution

by:Infinity08
Infinity08 earned 100 total points
ID: 22820237
>> the shift or divide or bitwise and operator may be of greater assistance

I meant the division operator obviously lol.

re. the binary operators : afaik, 10kB is not a power of 2 ;) Which complicates things slightly.
0
 
LVL 1

Accepted Solution

by:
brunovinicius earned 200 total points
ID: 22836306
Hi,

Based on the info provided by the author, i figure out that the following algorithm may solve the problem.
Hope its useful.
#define PAGE_SEZE 10240 // 10kb
 
bool areOnSamePage( float * x , float * y ) {
   
    // discover in which page x and y are
    int xPage = x / PAGE_SIZE;
    int yPage = y / PAGE_SIZE;
 
    return xPage == yPage;
}

Open in new window

0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Nadia
Linear search (searching each index in an array one by one) works almost everywhere but it is not optimal in many cases. Let's assume, we have a book which has 42949672960 pages. We also have a table of contents. Now we want to read the content on p…
When there is a disconnect between the intentions of their creator and the recipient, when algorithms go awry, they can have disastrous consequences.
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now