Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Two floats on same page?

Posted on 2008-10-27
12
Medium Priority
?
238 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

 
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 400 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 400 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 400 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 800 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
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…
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

715 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