Memory Addressing

M

moo

If you were to designate a specific memory address for a variable and
the compiled program was run on a system with that address in use
already what would happen?
 
J

Jorgen Grahn

If you were to designate a specific memory address for a variable and
the compiled program was run on a system with that address in use
already what would happen?

I would redesign my program. You cannot make such assumptions.

/Jorgen
 
M

Marcel Müller

If you were to designate a specific memory address for a variable and
the compiled program was run on a system with that address in use
already what would happen?

Modern operating systems do not even tell you which physical memory is
visible at a certain address (if any). In fact yo can link with a static
base address for all private memory, and this is not uncommon. Only if
you intend to be loaded into shared memory (like shared libraries) then
your code needs to be relocatable.


Marcel
 
M

moo

Thanks for all the replies, Im new to C++ so sorry if the question didnt
make complete sense I was watching a tutorial the other night which
prompted this thought as if a physical address was already in use and
somethiong tried to access it that could easily crash a system right? So
why would C++ allow that be coded? Has to be a reason.
 
J

Juha Nieminen

moo said:
Thanks for all the replies, Im new to C++ so sorry if the question didnt
make complete sense I was watching a tutorial the other night which
prompted this thought as if a physical address was already in use and
somethiong tried to access it that could easily crash a system right? So
why would C++ allow that be coded? Has to be a reason.

Can you give some example code that you think does this?
 
N

Nick Keighley

On 07/09/12 10:25, Sam wrote:> moo writes:

in the words of the standard "the behavior is undefined". Which means
the implementor is free to do what he damn well pleases. Since you can
only do this on a system where you deal in real physical addresses
(ie. not anything that runs on your desk or even your phone) there is
likely no memory protection. All bets are off. It depends. But chances
are nothing good.

"Doctor, it hurst when I do this!" "don't do that"
Thanks for all the replies, Im new to C++ so sorry if the question didnt
make complete sense I was watching a tutorial the other night which
prompted this thought as if a physical address was already in use and
somethiong tried to access it that could easily crash a system right?

maybe. maybe not.
So why would C++ allow that be coded? Has to be a reason.

C++ (and its ancestor, C) implicitly assumes the programmer knows what
he is doing. If you want to stuff bits into arbitary memory locations
then C++ will allow you to do this (or can be made to on many
implementations). Why would you do this? Memory mapped hardware,
access to special memory (EEPROM, DMA).

If you don't know what you are doing use Java or something.
 
K

Krice

prompted this thought as if a physical address was already in use and
somethiong tried to access it that could easily crash a system right?

It could easily crash the program.
So why would C++ allow that be coded? Has to be a reason.

C++ is a low level language that has no default protection
against memory handling errors.
 
M

moo

It could easily crash the program.


C++ is a low level language that has no default protection
against memory handling errors.

I was under the impression it was a high level language.
 
P

Paul N

Thanks for all the replies, Im new to C++ so sorry if the question didnt
make complete sense I was watching a tutorial the other night which
prompted this thought as if a physical address was already in use and
somethiong tried to access it that could easily crash a system right? So
why would C++ allow that be coded? Has to be a reason.

As other people have said, anything could happen. Essentially, if each
program thinks that what is stored is what it put there, then each can
get a nasty surprise when the other one stores something completely
different there.

As to why this should be allowed... Well, firstly, for most ways of
reserving memory C++ will find you a memory location that isn't
already in use. You have to do something special to get the situation
you describe. C++ is based on C, and C tends to assume that the user
knows what they are doing and so will do what they ask for.

One time whem you might use this is if you want to get more than one
value returned from a function. For example, if you want to get a
point, you might have:

void getpoint(int *x, int *y, int *z) {
*x = ...
*y = ...
*z = ...
return;
}

This will allow the function to write into memory locations that are
already in use. For example:

int ax, bx, cx;
getpoint(&ax, &bx, &cx);

which will set the variable ax to the required x value.

There are of course other ways of doing this, one being to return a
struct and one (in C++ but not C) being to use references.

Hope this helps.
Paul.
 
A

army1987

I was under the impression it was a high level language.

It's higher-level than machine code, assembly and C, but lower-level than
pretty much anything else in common use today.
 
J

Jorgen Grahn

.
C++ (and its ancestor, C) implicitly assumes the programmer knows what
he is doing. If you want to stuff bits into arbitary memory locations
then C++ will allow you to do this (or can be made to on many
implementations). Why would you do this? Memory mapped hardware,
access to special memory (EEPROM, DMA).

Put differently, C++ was designed to be useful for (among other
things) low-level systems programming. "Leave no room for another
lower-level language" was how Stroustrup put it, I think.
If you don't know what you are doing use Java or something.

Or use C++ on a modern computer with memory protection provided by the
OS and hardware ...

I suspect the OP misunderstood whatever material he was reading,
because corrupting other processes' memory is something very few
programmers have to worry about, no matter what language they use.

/Jorgen
 
K

Kelly Fergason

moo said:
If you were to designate a specific memory address for a variable and the
compiled program was run on a system with that address in use already what would happen?

to answer the question, the value at the address would be overwritten with
the new value.
i don't know what use this is in modern os's, if it is even possible, but
doing this on embedded boards was normal.
specifically in my case, a motorola 68hc11 evb with small-c.

bare metal programming in c. those were the days.
 

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
473,767
Messages
2,569,571
Members
45,045
Latest member
DRCM

Latest Threads

Top