473,804 Members | 2,126 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How do we know the memory arrangement using in microprocessors ? Top-Bottom or Bottom-Top?

Hi folks,

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.

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;
}

// 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
$

Sep 12 '06 #1
8 2006
"Cuthbert" <cu**********@g mail.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_Keit h) 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.
Sep 13 '06 #2
Thanks a lot for your reply, Keith.

In the view of C language, the addressese of array elements are always
increased along the index. Which means my way cannot relect the real
order of memony allocation.

At last, you said "(You'd have to compare addresses of distinct
objects, which invokes undefined behavior.)." However, the addresses,
or locations, of the static or local objects are optimized by
compilers, their addresses can tell nothing about the arrangement.

How about dynamic objects? Can they tell me the order of memory
allocation?

Cuthbert
Keith Thompson wrote:
"Cuthbert" <cu**********@g mail.comwrites:
This question is a little deep into memory management of
............... .......

Sep 13 '06 #3
"Cuthbert" <cu**********@g mail.comwrites:
Thanks a lot for your reply, Keith.

In the view of C language, the addressese of array elements are always
increased along the index. Which means my way cannot relect the real
order of memony allocation.
That (almost certainly) *is* the real order of memory allocation.

More precisely, on most hosted systems, C addresses (pointer values)
directly correspond to machine-level virtual addresses. (On some
embedded systems, C addresses might correspond to machine-level
physical addresses.)
At last, you said "(You'd have to compare addresses of distinct
objects, which invokes undefined behavior.)." However, the addresses,
or locations, of the static or local objects are optimized by
compilers, their addresses can tell nothing about the arrangement.

How about dynamic objects? Can they tell me the order of memory
allocation?
Your question is still very unclear. What exactly are you asking
about?

Whatever your actual question is, the answer is almost certainly that
you can't determine it in portable C, and there's almost certainly no
good reason why you should care.

Objects in C are allocated differently depending on their storage
durations. Objects with "static" storage duration exist throughout
the execution of the program. Objects with "automatic" storage
duration, such as (non-static) local objects in functions, exist
during the execution of the block in which they're declared. Objects
with "allocated" storage duration are created by malloc(), calloc(),
or realloc(), and destroyed by free() or realloc() (is this what you
mean by "dynamic objects"?).

The language says nothing about the relative addresses of any objects,
regardless of their storage durations.

Also, please learn to post properly. See
<http://cfaj.freeshell. org/google/and
<http://www.caliburn.nl/topposting.html >.

--
Keith Thompson (The_Other_Keit h) 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.
Sep 13 '06 #4

Keith Thompson wrote:
"Cuthbert" <cu**********@g mail.comwrites:
Thanks a lot for your reply, Keith.

In the view of C language, the addressese of array elements are always
increased along the index. Which means my way cannot relect the real
order of memony allocation.

That (almost certainly) *is* the real order of memory allocation.

More precisely, on most hosted systems, C addresses (pointer values)
directly correspond to machine-level virtual addresses. (On some
embedded systems, C addresses might correspond to machine-level
physical addresses.)
At last, you said "(You'd have to compare addresses of distinct
objects, which invokes undefined behavior.)." However, the addresses,
or locations, of the static or local objects are optimized by
compilers, their addresses can tell nothing about the arrangement.

How about dynamic objects? Can they tell me the order of memory
allocation?

Your question is still very unclear. What exactly are you asking
about?

Whatever your actual question is, the answer is almost certainly that
you can't determine it in portable C, and there's almost certainly no
good reason why you should care.

Objects in C are allocated differently depending on their storage
durations. Objects with "static" storage duration exist throughout
the execution of the program. Objects with "automatic" storage
duration, such as (non-static) local objects in functions, exist
during the execution of the block in which they're declared. Objects
with "allocated" storage duration are created by malloc(), calloc(),
or realloc(), and destroyed by free() or realloc() (is this what you
mean by "dynamic objects"?).

The language says nothing about the relative addresses of any objects,
regardless of their storage durations.

Also, please learn to post properly. See
<http://cfaj.freeshell. org/google/and
<http://www.caliburn.nl/topposting.html >.

--
Keith Thompson (The_Other_Keit h) 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.
Just to add Keith's mail, All the local variables are generally stored
in the stack during the function call (called by many names as
'activation record', 'function call record', call stack etc. Where as
the objects created by malloc/calloc/realloc/new are created in the
heap memory. Compilers (specifically linker) will place global and
static variables in data section where they are exist through out the
program.
This information can be found when you read the linker document of a
compiler. Storage allocations are interesting topic by itself :)

-kondal

Sep 13 '06 #5

Keith Thompson wrote:
"Cuthbert" <cu**********@g mail.comwrites:
Thanks a lot for your reply, Keith.

In the view of C language, the addressese of array elements are always
increased along the index. Which means my way cannot relect the real
order of memony allocation.

That (almost certainly) *is* the real order of memory allocation.

More precisely, on most hosted systems, C addresses (pointer values)
directly correspond to machine-level virtual addresses. (On some
embedded systems, C addresses might correspond to machine-level
physical addresses.)
At last, you said "(You'd have to compare addresses of distinct
objects, which invokes undefined behavior.)." However, the addresses,
or locations, of the static or local objects are optimized by
compilers, their addresses can tell nothing about the arrangement.

How about dynamic objects? Can they tell me the order of memory
allocation?

Your question is still very unclear. What exactly are you asking
about?

Whatever your actual question is, the answer is almost certainly that
you can't determine it in portable C, and there's almost certainly no
good reason why you should care.

Objects in C are allocated differently depending on their storage
durations. Objects with "static" storage duration exist throughout
the execution of the program. Objects with "automatic" storage
duration, such as (non-static) local objects in functions, exist
during the execution of the block in which they're declared. Objects
with "allocated" storage duration are created by malloc(), calloc(),
or realloc(), and destroyed by free() or realloc() (is this what you
mean by "dynamic objects"?).

The language says nothing about the relative addresses of any objects,
regardless of their storage durations.

Also, please learn to post properly. See
<http://cfaj.freeshell. org/google/and
<http://www.caliburn.nl/topposting.html >.

--
Keith Thompson (The_Other_Keit h) 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.
Just to add Keith's mail, All the local variables are generally stored
in the stack during the function call (called by many names as
'activation record', 'function call record', call stack etc). Where as
the objects created by malloc/calloc/realloc/new are created in the
heap memory. Compilers (specifically linker) will place global and
static variables in data section where they are exist through out the
program.
This information can be found when you read the linker document of a
compiler. Storage allocations are interesting topic by itself :)

-kondal

Sep 13 '06 #6
Cuthbert wrote:
Thanks a lot for your reply, Keith.

In the view of C language, the addressese of array elements are always
increased along the index. Which means my way cannot relect the real
order of memony allocation.

At last, you said "(You'd have to compare addresses of distinct
objects, which invokes undefined behavior.)." However, the addresses,
or locations, of the static or local objects are optimized by
compilers, their addresses can tell nothing about the arrangement.

How about dynamic objects? Can they tell me the order of memory
allocation?
I would look at the documentation for the processor and compiler.

Why do you need to care btw ?
Sep 13 '06 #7
"kondal" <ko******@gmail .comwrites:
[...]
Just to add Keith's mail, All the local variables are generally stored
in the stack during the function call (called by many names as
'activation record', 'function call record', call stack etc. Where as
the objects created by malloc/calloc/realloc/new are created in the
heap memory. Compilers (specifically linker) will place global and
static variables in data section where they are exist through out the
program.
This information can be found when you read the linker document of a
compiler. Storage allocations are interesting topic by itself :)
None of this is guaranteed by the standard. The language itself does
not define "stack", "heap", or "data section". Many implementations
use such things, but not all do.

--
Keith Thompson (The_Other_Keit h) 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.
Sep 13 '06 #8

Cuthbert wrote:
Hi folks,

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?

Because of the way pointer and array indexing is defined, in C pointer
arithmetic is always "rightside up". There's probably no way to tell
anyway, the compiler would have to hide any funny business. For
example, on the old x86 real-mode segmented architecture, the address
was kept in two separate parts that had to be non-intuitively merged by
the compiler anytime you did math on (huge) pointers. Though from the
user's perspective all you saw was the equivalent of a 64-bit
"unsigned long int".

There have been a few computers with "backwards" address arithmetic.
It turns out to be (slightly) faster to design a subtractor than an
adder-- A few very old computers, like the old IBM 709x series, used
subtractive arithmetic when indexing. I don't know of any currently
running computer that does so though . Perhaps some of those
super-configurable DSP processors can do this.

Sep 13 '06 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
1236
by: Gaurav Jain | last post by:
Hi, While New() is used for allocating objects on heap some additional information is stored before the begning of the block so that delete can release the allocated memory. Any idea what this information is just the size or anything else also ? is it dependent on the compiler used ? Regards,
3
2582
by: Hassan Iqbal | last post by:
Hi, (1)if i do the memory allocation like: int **p; p= malloc(n*sizeof p); for(i=0;i<n;i++) /* i and n are integers */ p= malloc(n*sizeof p); then in that case is this command sufficient to free all the allocated memory:
2
1930
by: wildfyre53207 | last post by:
Here is our problem... We are doing a lot of selects against a table that has one large field in it. If we do a select against all the fields except for description, the query comes back relatively quickly. If we add that last field (768 chars) to the query, our query takes 10x longer (5 seconds vs 56 seconds.)
3
2242
by: subirs | last post by:
Hi, I am using mtrace to check for memory leaks in my code. The code is divided into many fucntion which are placed in different directories. While using mtrace the following output is given. Now the problem is to find where in the code and in which function memory leaks occurs as the trace does not give line number or name of the function. ------------------------------------------------------------------ Memory not freed:...
1
2085
by: geskerrett | last post by:
I was wondering if a ctypes expert could point me in the right direction. I am using the wrapper for the "freeimage" library to create multipage TIFF files from a group of png images. The images load and save fine, the problem seems to be that the memory used during the bitmap creation isn't being released properly until the python program ends and python exits. The multipage tiff's are created in a loop (ie, creating 3 tiffs from a
5
2171
by: cikkamikka | last post by:
Hi friends, Sorry for such basic question. but I wanted to know where does new operator or malloc operator allocate memory? in actualy physical Main memory or virtual memory?
1
3266
by: cody314 | last post by:
Versions: Python 2.5.1 (r251:54863, May 14 2007, 10:50:04) SWIG Version 1.3.20 Hello I have some code that wraps a C++ library so I may use it in python. The code basically just gets some data and puts it in the PyArrayObject* which is returned as a PyObject*. I then call it from python like so: self.c = __f2.fdct2_wrapper(x,self.nbs,self.nba,self.ac)
0
816
by: danishce | last post by:
I am using Visual C Dll in my vb.net application which pass parameters from my application and return value. some times it returns data ok and some time it does not return data or return incorrect values.i think there is a memory problem so how can i handle memory in my vb.net application so it display data correctly. the code for my application
2
2440
by: =?Utf-8?B?SGFubmVzIFN0ZWlua2U=?= | last post by:
Hello, I'm trying to use the command "#pragma omp parallel for" within a thread but each thread that is created (and stopped again) produces a memory leak of about 44kB. If the entry for "Stack Reserver Size" in Properties->ConfigurationsProperties->Linker->System is increased, the memory leak grows up to 10MB and more dependent on the stack reserve size.
3
2718
by: peter peterson | last post by:
I have been looking at memory pools and was wondering what would be wrong with creating a singleton which create a "pool" of (allocated) pointers to a given struct/class( in the case below Messages ), and then when a client needs a "new" one pass them one from your free message queue. Any thoughts, potential problems, etc, would be appreciated. Thanks. class MsgPool { public: ~MsgPool(); static void recycleMem(Message* msg);
0
9711
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10088
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9169
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6862
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5529
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5668
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4306
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3831
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3001
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.