what is at 0 address in a program?

Y

ypjofficial

Hello All,
While readling the C and C++ literature i came to know that the OS
allocates separate protected memory space for each running program and
the program do not interfare into one another's address space.
Also the address space allocated for each program is totally relative.

Now i want to know that from what address value does the stack starts
and the heap actually start?

What is at address 0(first address) in a program?

In other words,why the variables defined in the program have their
address as 1,2,3...( of course in hexadecimal)?
The address values are always like 0x00012ffc7 and likes..

Regards,
Yogesh Joshi
 
K

Kenny McCormack

Hello All,
While readling the C and C++ literature i came to know that the OS
allocates separate protected memory space for each running program and
the program do not interfare into one another's address space.
Also the address space allocated for each program is totally relative.

Now i want to know that from what address value does the stack starts
and the heap actually start?

"Smashing the Stack" for fun & profit, are we?
 
A

Alf P. Steinbach

* (e-mail address removed):
Hello All,
While readling the C and C++ literature i came to know that the OS
allocates separate protected memory space for each running program and
the program do not interfare into one another's address space.
Also the address space allocated for each program is totally relative.

Now i want to know that from what address value does the stack starts

That depends on just about everything, including the phase of the moon. You
can obtain that address via assembly language. Or you can obtain an address
that is quite near to it by something as simple as

int main()
{
int a;
std::cout << &a << std::endl;
}

and the heap actually start?

There is not necessarily just one heap.

What is at address 0(first address) in a program?

Usually that is the same as pointer value 0, which means that usually (except
for embedded systems programming), on modern computers, trying to access the
contents of that address will result in some kind of exception or trap.


In other words,why the variables defined in the program have their
address as 1,2,3...( of course in hexadecimal)?
The address values are always like 0x00012ffc7 and likes..

What system do you think you're perceiving?
 
?

=?ISO-8859-15?Q?Juli=E1n?= Albo

While readling the C and C++ literature i came to know that the OS
allocates separate protected memory space for each running program and
the program do not interfare into one another's address space.

Not very exact. That lietarure is about how C++ is implemented in some OS,
not about the language by itself. Try for example some C++ compiler for
ms-dos to see it.
Also the address space allocated for each program is totally relative.

Relative to what? The adresses are usually addresses, real or virtual
depending of the OS. Surely is possible a compiler that uses his own way of
addressing starting at 0, by offseting every memory access or something,
but probably nobody wtite a compiler like that. Maybe some day the parrot
guys will do.
 
J

John Harrison

Hello All,
While readling the C and C++ literature i came to know that the OS
allocates separate protected memory space for each running program and
the program do not interfare into one another's address space.
Also the address space allocated for each program is totally relative.

The above might be true, but it has nothing at all to do with C or C++.
The above are statements about OS design, not about languages.
Now i want to know that from what address value does the stack starts
and the heap actually start?

Neither C or C++ defines this.
What is at address 0(first address) in a program?

Neither C nor C++ define this either.
In other words,why the variables defined in the program have their
address as 1,2,3...( of course in hexadecimal)?
The address values are always like 0x00012ffc7 and likes..

Regards,
Yogesh Joshi

You aren't asking questions about programming languages you are asking
questions about OS design and/or compiler design. Languages don't define
where the stack starts or what is at a given address.

john
 
M

mario.demiguel

Hello All,
While readling the C and C++ literature i came to know that the OS
allocates separate protected memory space for each running program and
the program do not interfare into one another's address space.
Also the address space allocated for each program is totally relative.

Now i want to know that from what address value does the stack starts
and the heap actually start?

That entirely depends on the system you are dealing with. Even with
that information, it may be hard to know exactly what address contains
the top of the stack.
What is at address 0(first address) in a program?

Usually the first instruction of your compiled/assembled program.
In other words,why the variables defined in the program have their
address as 1,2,3...( of course in hexadecimal)?
The address values are always like 0x00012ffc7 and likes..

I'm confused by what you are asking. Do you want to know why the
variables are at 0x00012ffc7 as opposed to 0x000000000?
 
G

Gordon Burditt

While readling the C and C++ literature i came to know that the OS
allocates separate protected memory space for each running program and
the program do not interfare into one another's address space.

Many OSs do this. MS-DOS and CP/M do not. I don't think Windows
3.1 did either. Certain implementations of UNIX-like OS on an 8086
did not (an 8086 doesn't have the hardware memory protection like
80[23456]86).
Also the address space allocated for each program is totally relative.

Not all systems have the hardware needed for virtual memory.
Now i want to know that from what address value does the stack starts
and the heap actually start?

Whereever they start. It's OS-dependent and may vary with the
particular program in question (it might be "just after the end of
the doohickey segment" or "just before the command-line arguments,
which take up a variable amount of space"). And C doesn't even
guarantee the existence of a stack, and it only guarantees the
existence of a heap if heap is defined as "that place from which
malloc() gets its memory".
What is at address 0(first address) in a program?

Who said address 0 is the first address in a program?

On a number of systems, such as many recent variants of UNIX, there
is no memory mapped there, and attempting to access it gives you a
smegmentation violation or similar program-aborting trap.
In other words,why the variables defined in the program have their
address as 1,2,3...( of course in hexadecimal)?

Who said they do? And not all variables are of size 1.
The address values are always like 0x00012ffc7 and likes..

Gordon L. Burditt
 
Y

ypjofficial

sorry..the question here should be
In other words,why the variables defined in the program DON'T have
their
address as 1,2,3...( of course in hexadecimal)?
The address values are always like 0x00012ffc7 and likes..

sorry for creating confusion..

regards,
Yogesh Joshi
 
E

E. Robert Tisdale

Yogesh said:
While readling the C and C++ literature, I came to know that
the OS allocates separate protected memory space for each running program
and the programs do not interfere with one another's address space.
Also the address space allocated for each program is totally relative.

Not necessarily true.
Now, I want to know from what address value does the stack start
and the heap actually start?

What is at address 0(first address) in a program?

In other words,
"Why [do] the variables defined in the program
have their address as 1,2,3...( of course in hexadecimal)?"
The address values are always like 0x00012ffc7 and the like.

In the typical implementation, [virtual] memory is organized
as a set of sequential address from top to bottom:

00000000 top
00000001
.
.
.
FFFFFFFE
FFFFFFFF bottom

The text (code) segment begins somewhere near the top of memory
followed by the data segment.
The stack begins somewhere near the bottom of memory
and grows up into free storage toward the data and text segments.
The "heap" resides in free storage after the text and data segments
and grows down toward the top of stack.

You need to study the documentation for your machine,
operating system and compiler to find the actual implementation details.
> cat main.c
#include<stdio.h>

int main(int argc, char* argv[]) {
int* p = 0;
fprintf(stderr, "i*p = %d\n", *p);
return 0;
}
> gcc -Wall -std=c99 -pedantic -o main main.c
> ./main
Segmentation fault

Usually, you won't be able to access address 0
because it is reserved for the NULL pointer
or because it is reserved for the operating system
or both or some other reason.
 
Y

ypjofficial

sorry..the question here should be
In other words,why the variables defined in the program DON'T have
their
address as 1,2,3...( of course in hexadecimal)?
The address values are always like 0x00012ffc7 and likes..

sorry for creating confusion..

regards,
Yogesh Joshi
 
M

Malcolm Dew-Jones

(e-mail address removed) wrote:
: Hello All,

: What is at address 0(first address) in a program?

in VMS on VAX, virtual address 0 was allocated to an inaccessible page to
help catch memory accesses from uninitialized pointers.
 
J

John Harrison

sorry..the question here should be
In other words,why the variables defined in the program DON'T have
their
address as 1,2,3...( of course in hexadecimal)?
The address values are always like 0x00012ffc7 and likes..

If you want this question answered (which I guess you do) then you are
going to have to specify, which operating system, which processor, and
which compiler. Because the answer could be different depending on all
of these things.

Your question actually seems to be, given an OS with protected memory,
and a compiler which generates only code using relative addressing why
couldn't the compiler/linker/loader arrange it so that the stack starts
at address zero. Well indeed, but why should they either? Does it matter?

john
 
S

Skarmander

E. Robert Tisdale wrote:
cat main.c
#include<stdio.h>

int main(int argc, char* argv[]) {
int* p = 0;
fprintf(stderr, "i*p = %d\n", *p);
return 0;
}
gcc -Wall -std=c99 -pedantic -o main main.c
./main
Segmentation fault

Usually, you won't be able to access address 0
because it is reserved for the NULL pointer
or because it is reserved for the operating system
or both or some other reason.

Actually, the code posted is certainly a bad idea, invoking undefined
behaviour because you're dereferencing a null pointer. If you want
actual memory location 0, not whatever bit pattern your implementation
has picked for the null pointer, you'll have to be a little trickier:

int i = 0;
int* p = (int*) i;

This invokes unspecified behaviour, of course, and dollars to dougnuts
that this produces the exact same code on the majority of
implementations (which do equate memory location 0 and the null
pointer), but it beats undefined behaviour.

S.
 
J

Jay Nabonne

sorry..the question here should be
In other words,why the variables defined in the program DON'T have
their
address as 1,2,3...( of course in hexadecimal)?
The address values are always like 0x00012ffc7 and likes..

Here's a parallel question for *you*:

Why is your name not "Fred Flintstone"?

The answer: because your parents didn't give you that name, based on
multiple considerations.

The answer to your question is the same: they don't have those addresses
because, based on multiple considerations, the compiler/linker didn't give
them those addresses.

You will probably find more enlightenment studying why the decisions were
made that *were* made before asking why things couldn't be different.

- Jay
 
K

Keith Thompson

Skarmander said:
E. Robert Tisdale wrote:
cat main.c
#include<stdio.h>
int main(int argc, char* argv[]) {
int* p = 0;
fprintf(stderr, "i*p = %d\n", *p);
return 0;
}
gcc -Wall -std=c99 -pedantic -o main main.c
./main
Segmentation fault
Usually, you won't be able to access address 0
because it is reserved for the NULL pointer
or because it is reserved for the operating system
or both or some other reason.

Actually, the code posted is certainly a bad idea, invoking undefined
behaviour because you're dereferencing a null pointer. If you want
actual memory location 0, not whatever bit pattern your implementation
has picked for the null pointer, you'll have to be a little trickier:

int i = 0;
int* p = (int*) i;

This invokes unspecified behaviour, of course, and dollars to dougnuts
that this produces the exact same code on the majority of
implementations (which do equate memory location 0 and the null
pointer), but it beats undefined behaviour.

The code you've shown invokes implementation-defined behavior.
The result of the conversion of an integer to a pointer "is
implementation-defined, might not be correctly aligned, might
not point to an entity of the referenced type, and might be a trap
representation" (C99 6.3.2.3p5). If you then attempt to dereference p,
the behavior is undefined (unless the implementation has defined 0 as a
valid address): "If an invalid value has been assigned to the pointer,
the behavior of the unary * operator is undefined" (C99 6.5.3.2p4).

Nasal demons are still a possibility.
 
S

Skarmander

Keith said:
Skarmander said:
E. Robert Tisdale wrote:
cat main.c
#include<stdio.h>
int main(int argc, char* argv[]) {
int* p = 0;
fprintf(stderr, "i*p = %d\n", *p);
return 0;
}
gcc -Wall -std=c99 -pedantic -o main main.c
./main
Segmentation fault
Usually, you won't be able to access address 0
because it is reserved for the NULL pointer
or because it is reserved for the operating system
or both or some other reason.

Actually, the code posted is certainly a bad idea, invoking undefined
behaviour because you're dereferencing a null pointer. If you want
actual memory location 0, not whatever bit pattern your implementation
has picked for the null pointer, you'll have to be a little trickier:

int i = 0;
int* p = (int*) i;

This invokes unspecified behaviour, of course, and dollars to dougnuts
that this produces the exact same code on the majority of
implementations (which do equate memory location 0 and the null
pointer), but it beats undefined behaviour.


The code you've shown invokes implementation-defined behavior.
The result of the conversion of an integer to a pointer "is
implementation-defined, might not be correctly aligned, might
not point to an entity of the referenced type, and might be a trap
representation" (C99 6.3.2.3p5). If you then attempt to dereference p,
the behavior is undefined (unless the implementation has defined 0 as a
valid address): "If an invalid value has been assigned to the pointer,
the behavior of the unary * operator is undefined" (C99 6.5.3.2p4).

Nasal demons are still a possibility.
*If* address 0 is invalid, yes. We're sort-of assuming it's not,
otherwise, obviously, any attempt to get at location 0 is doomed.

My point was merely that getting at location 0 through a null pointer
was still a worse idea than doing it through a converted int.

It does all boil down to what is "more likely" to yield the desired
result. Let's not put money on that and agree you're basically on your
own if you want to do something like this, and if your implementation
agrees, good for you -- if not, try assembler...

S.
 
M

Malcolm Dew-Jones

(e-mail address removed) wrote:
: Hello All,
: While readling the C and C++ literature i came to know that the OS
: allocates separate protected memory space for each running program and
: the program do not interfare into one another's address space.
: Also the address space allocated for each program is totally relative.

: Now i want to know that from what address value does the stack starts
: and the heap actually start?

: What is at address 0(first address) in a program?

: In other words,why the variables defined in the program have their
: address as 1,2,3...( of course in hexadecimal)?
: The address values are always like 0x00012ffc7 and likes..

One possible layout for your code will have the code itself loaded at a
low address. The stack will start at the end of the code and expand
towards higher addresses. The heap will start at a high address and
expand towards lower addresses.

In that layout, the address of variables on the stack will never have low
numbers because the lowest addresses contain the executable code.

That explanation is true for some systems, but is wrong for some others.
 
K

Kenneth Brody

Jay said:
Here's a parallel question for *you*:

Why is your name not "Fred Flintstone"?

The answer: because your parents didn't give you that name, based on
multiple considerations.

The answer to your question is the same: they don't have those addresses
because, based on multiple considerations, the compiler/linker didn't give
them those addresses.

You will probably find more enlightenment studying why the decisions were
made that *were* made before asking why things couldn't be different.

Consider, for example, a platform where NULL is all-bits-zero, and the
implementors of the compiler/linker decided to start putting global
variables at address 1. (Starting them at zero is obviously "a bad
idea", as &first_global_variable==NULL.) This is still a bad idea,
as NULL pointers can still trounce over these variables. (What happens
if you dereference a NULL pointer to a struct?)

A practical reason for not placing anything near address zero is to
ensure that dereferencing NULL pointers cause a fault. (On platforms
that allow for such faults, of course.)

I've also seen a platform that, for some unexplained reason, acted as
if there were a read-only chunk of all-bits-zero memory at address zero,
allowing for NULL pointer dereferencing to succeed.

--
+-------------------------+--------------------+-----------------------------+
| Kenneth J. Brody | www.hvcomputer.com | |
| kenbrody/at\spamcop.net | www.fptech.com | #include <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------------+
Don't e-mail me at: <mailto:[email protected]>
 
S

Skarmander

Kenneth Brody wrote:
I've also seen a platform that, for some unexplained reason, acted as
if there were a read-only chunk of all-bits-zero memory at address zero,
allowing for NULL pointer dereferencing to succeed.

Ah, I know the answer to this one. It's so broken code like this won't
core-dump, even though it rightfully should:
printf("%s", p);
where p is, of course, 0, by accident or incompetence.

Likewise for all the other string functions: they'll do something
"reasonable". Heck, if all your data types use all-bits-zero as a
representation of their 0 value, it'll work for just about anything, and
give you a nice sensible default!

A splendid feature, until your program is compiled on a more sensible
platform, and crashes horribly.

S.
 
K

Kenneth Brody

Skarmander said:
Kenneth Brody wrote:


Ah, I know the answer to this one. It's so broken code like this won't
core-dump, even though it rightfully should:
printf("%s", p);
where p is, of course, 0, by accident or incompetence.

Likewise for all the other string functions: they'll do something
"reasonable". Heck, if all your data types use all-bits-zero as a
representation of their 0 value, it'll work for just about anything, and
give you a nice sensible default!

I'm sure that was the logic[1] behind it.
A splendid feature, until your program is compiled on a more sensible
platform, and crashes horribly.


[1] FSVO.

--
+-------------------------+--------------------+-----------------------------+
| Kenneth J. Brody | www.hvcomputer.com | |
| kenbrody/at\spamcop.net | www.fptech.com | #include <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------------+
Don't e-mail me at: <mailto:[email protected]>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,169
Messages
2,570,920
Members
47,464
Latest member
Bobbylenly

Latest Threads

Top