"Cuthbert" <cu**********@gmail.comwrites:
This question is a little deep into memory management of
microprocessor.
How do we know the memory arrangement using in microprocessors?
Top-Bottom or Bottom-Top?
For example, the "Top-Bottom" is arranging memory resource from higher
address to lower address.
I don't think that's even meaningful.
If we have no hardware knowledge of microprocessors, how can we know
the arrangement of microprocessors?
I have idea which is declearing an array and check the addresses of
array elements because the allocation of array elements is sequential.
And, then, we know how memory resource is arranged.
BUT, is there any other way to find out?
Cuthbert
===================================
int main (void)
{
char var[10];
int i;
for ( i = 0; i < 10; i++)
printf("addr = %X\n", &var[i]);
return 0;
}
Since you're using printf(), you *must* have a "#include <stdio.h>" at
the top of your program.
The "%X" format expects an unsigned int; you're giving it a char*,
which invokes undefined behavior. Use "%p" to print pointers.
Here's a corrected version of your program:
#include <stdio.h>
int main (void)
{
char var[10];
int i;
for ( i = 0; i < 10; i++)
printf("addr = %p\n", (void*)&var[i]);
return 0;
}
// Bottom-Top arrangement:
// Result:
$ ./addr
addr = BF9CEF66
addr = BF9CEF67
addr = BF9CEF68
addr = BF9CEF69
addr = BF9CEF6A
addr = BF9CEF6B
addr = BF9CEF6C
addr = BF9CEF6D
addr = BF9CEF6E
addr = BF9CEF6F
With the corrected program, I get:
addr = 0x22ccd0
addr = 0x22ccd1
addr = 0x22ccd2
addr = 0x22ccd3
addr = 0x22ccd4
addr = 0x22ccd5
addr = 0x22ccd6
addr = 0x22ccd7
addr = 0x22ccd8
addr = 0x22ccd9
Array elements are always allocated at increasing addresses, by
definition. Displaying the addresses isn't necessarily going to tell
you anything; using "%X" can give you completely meaningless result,
and even "%p" gives you implementation-defined results.
You can compare addresses of array elements using "<", and ">" (since
the addresses are within the same object). For example:
#include <stdio.h>
int main (void)
{
char var[10];
int i;
for ( i = 0; i < 9; i++) {
if (&var[i] < &var[i+1]) {
printf("&var[%d] < &var[%d]\n", i, i+1);
}
else if (&var[i] == &var[i+1]) {
printf("&var[%d] == &var[%d] (?)\n", i, i+1);
}
else if (&var[i] &var[i+1]) {
printf("&var[%d] &var[%d] (?)\n", i, i+1);
}
else {
printf("&var[%d] and &var[%d] are incomparable (?)\n", i, i+1);
}
}
return 0;
}
But this still tells you nothing about the CPU architecture. If the
program produces any output other than:
&var[0] < &var[1]
&var[1] < &var[2]
&var[2] < &var[3]
&var[3] < &var[4]
&var[4] < &var[5]
&var[5] < &var[6]
&var[6] < &var[7]
&var[7] < &var[8]
&var[8] < &var[9]
on *any* system, then there's a serious problem, probably a compiler
bug.
If you're asking the order in which memory is *allocated* (say, for
successive function calls), there is no *portable* way to determine
this. On many systems, the system stack grows either from high to low
addresses, or vice versa, and there's likely to be some non-portable
way to determine which. (You'd have to compare addresses of distinct
objects, which invokes undefined behavior.) Other systems may not use
a system stack in the usual sense at all.
If you want to write portable code, there's no reason why you should
care one way or the other.
--
Keith Thompson (The_Other_Keith)
ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.