Memory Addressing

Discussion in 'C++' started by moo, Sep 6, 2012.

  1. moo

    moo Guest

    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?
     
    moo, Sep 6, 2012
    #1
    1. Advertising

  2. moo

    Jorgen Grahn Guest

    On Thu, 2012-09-06, moo wrote:
    > 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

    --
    // Jorgen Grahn <grahn@ Oo o. . .
    \X/ snipabacken.se> O o .
     
    Jorgen Grahn, Sep 6, 2012
    #2
    1. Advertising

  3. On 06.09.2012 20:10, moo wrote:
    > 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
     
    Marcel Müller, Sep 6, 2012
    #3
  4. moo

    moo Guest

    On 07/09/12 10:25, Sam wrote:
    > moo writes:
    >
    >> 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?

    >

    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.
     
    moo, Sep 7, 2012
    #4
  5. moo <> wrote:
    > 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?
     
    Juha Nieminen, Sep 7, 2012
    #5
  6. On Sep 7, 5:59 am, moo <> wrote:
    > On 07/09/12 10:25, Sam wrote:> moo writes:


    > >> 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?


    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.
     
    Nick Keighley, Sep 7, 2012
    #6
  7. moo

    Krice Guest

    On 7 syys, 07:59, moo <> wrote:
    > 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.
     
    Krice, Sep 7, 2012
    #7
  8. moo

    moo Guest

    On 07/09/12 18:23, Krice wrote:
    > On 7 syys, 07:59, moo <> wrote:
    >> 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.
    >


    I was under the impression it was a high level language.
     
    moo, Sep 7, 2012
    #8
  9. moo

    moo Guest


    > 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.
     
    moo, Sep 7, 2012
    #9
  10. moo

    Paul N Guest

    On Sep 7, 5:59 am, moo <> wrote:
    > On 07/09/12 10:25, Sam wrote:> moo writes:
    >
    > >> 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?

    >
    > 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.
     
    Paul N, Sep 7, 2012
    #10
  11. moo

    army1987 Guest

    On Fri, 07 Sep 2012 21:59:46 +1000, moo wrote:

    > 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.



    --
    [ T H I S S P A C E I S F O R R E N T ]
    Troppo poca cultura ci rende ignoranti, troppa ci rende folli.
    -- fathermckenzie di it.cultura.linguistica.italiano
    <http://xkcd.com/397/>
     
    army1987, Sep 7, 2012
    #11
  12. moo

    Jorgen Grahn Guest

    On Fri, 2012-09-07, Nick Keighley wrote:
    ....
    >> 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).


    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

    --
    // Jorgen Grahn <grahn@ Oo o. . .
    \X/ snipabacken.se> O o .
     
    Jorgen Grahn, Sep 8, 2012
    #12
  13. moo <> wrote:
    > 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.
     
    Kelly Fergason, Sep 8, 2012
    #13
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alex
    Replies:
    4
    Views:
    3,744
  2. Larry Bates

    Indirect Memory Addressing

    Larry Bates, Jun 4, 2004, in forum: Python
    Replies:
    2
    Views:
    387
    Michel Claveau/Hamster
    Jun 4, 2004
  3. Simon Berube

    Memory addressing

    Simon Berube, Apr 27, 2007, in forum: Python
    Replies:
    12
    Views:
    503
    Hendrik van Rooyen
    Apr 29, 2007
  4. cnoe

    what is memory addressing ?

    cnoe, Nov 29, 2008, in forum: C Programming
    Replies:
    15
    Views:
    995
    S M Ryan
    Nov 30, 2008
  5. Nomen Nescio

    what is memory addressing ?

    Nomen Nescio, Nov 29, 2008, in forum: C Programming
    Replies:
    0
    Views:
    305
    Nomen Nescio
    Nov 29, 2008
Loading...

Share This Page