Another question related to pointers.

Discussion in 'C Programming' started by anonymous, Jan 5, 2006.

  1. anonymous

    anonymous Guest

    Dear All,

    >From my understanding of pointers, a pointer should not be able to

    access a memory location until that memory has been allocated either by
    assiging the address of a variable
    or either through malloc i.e.

    ptr = &somevariable;

    or

    ptr = malloc ( sizeof ( data ) );

    However, what I discovered with the following program has made me a bit
    uneasy.
    Not only can I read the memory pointed to by pointers that still have
    not been allocated any
    memory, I can write as well.

    Can somebody please explain, why pointers p1 and p2 can read from and
    read to memory
    that they have not been allocated?

    Thanks in advance for all your help.

    #include <stdio.h>
    int main ( void )
    {
    int *ptr1, *ptr2;

    /* Get read access to the memory pointed to by p1 and p2 */
    printf ( "*p1 = %d\n", *p1 );
    printf ( "*p2 = %d\n", *p2 );

    /* Write to the memory pointed to by p1 and p2 */

    *p1 = 1;
    *p2 = 2;

    /* Confirm the write operation */
    printf ( "*p1 = %d\n", *p1 );
    printf ( "*p2 = %d\n", *p2 );

    return 0;
    }
     
    anonymous, Jan 5, 2006
    #1
    1. Advertising

  2. anonymous

    Guest

    Hello


    This is luck that it works.

    The declared pointers have a value, C doesn't zero memory on
    declaration!!!

    What actual is happening is that you are reading/writeing on a random
    place in memory.

    the behaivior of this is undefined.

    All things can happen.

    Greetings Olaf
     
    , Jan 5, 2006
    #2
    1. Advertising

  3. anonymous

    Guest

    anonymous <> wrote:
    > Dear All,
    >
    >>From my understanding of pointers, a pointer should not be able to

    > access a memory location until that memory has been allocated either
    > by assiging the address of a variable or either through malloc i.e.
    >
    > ptr = &somevariable;
    >
    > or
    >
    > ptr = malloc ( sizeof ( data ) );
    >
    > However, what I discovered with the following program has made me a
    > bit uneasy. Not only can I read the memory pointed to by pointers
    > that still have not been allocated any memory, I can write as well.
    >
    > Can somebody please explain, why pointers p1 and p2 can read from and
    > read to memory that they have not been allocated?


    You have not yet allocated any storage, but pointers always point
    *somewhere*. Since you declare the pointers p1 and p2 as automatic
    variables (on the stack), their value (the memory location they are
    pointing to, not their *content*) is uninitialized, and just contain
    the value that happened to be in that spot of memory.

    What you are doing by accessing the pointers is just reading and - even
    worse, writing - to *some* memory, but you don't know where. Blindfold
    yourself, take a gun, turn around 20 times and shoot. You might hit
    nothing at all, you might hit the ground, or you might hit the gastank
    you are standing next to. Just don't do it.

    --
    :wq
    ^X^Cy^K^X^C^C^C^C
     
    , Jan 5, 2006
    #3
  4. anonymous

    anonymous Guest

    wrote:
    > Hello
    >
    >
    > This is luck that it works.
    >
    > The declared pointers have a value, C doesn't zero memory on
    > declaration!!!
    >
    > What actual is happening is that you are reading/writeing on a random
    > place in memory.
    >
    > the behaivior of this is undefined.
    >
    > All things can happen.
    >
    > Greetings Olaf


    That is what I also think i.e. the pointers are pointing to *any*
    memory as per garbage
    values stored in p1 and p2 and, in turn, garbage value in the pointed
    memory is being
    accessed. But just try this program below. According to above
    hypothesis, this should
    work again, but it does not. You get a segmentation fault even on a
    read operation of an
    arbitrary memory location. Why cannot I access this memory now if I
    could access the
    memory pointed to by *garbage* address in p1. In fact, I tried with
    various input values
    of address and it would not read or write to any other memory location
    that I could think of. May be try generating all possible addresses and
    try to read and write all memory
    locations right from 0000 0000 to ffff ffff and then figure out any
    result.


    #include <stdio.h>
    int main ( void )
    {
    int *p1, address;

    /* You can do read and write with the garbage address in p1 */
    printf ( " *p1 = %d\n", *p1 );
    *p1 = 0x12345678;



    /* Now get some address from the user */
    scanf ( "%x", &address );

    /* Initialize your pointer with this address */
    p1 = ( int * ) address;

    /* Try reading from memory pointed to by the address */
    /* You get a segmentation fault this point onwards */

    printf ( " *p1 = %d\n", *p1 );

    /* Try writing to memory pointed to be the address */
    *p1 = 0x12345678;

    /* Confirm the write operation */
    printf ( " *p1 = %d\n", *p1 );

    return 0;
    }
     
    anonymous, Jan 5, 2006
    #4
  5. anonymous

    Eric Sosman Guest

    anonymous wrote:
    > [...]
    > However, what I discovered with the following program has made me a bit
    > uneasy. [...]


    I'd be uneasy, too, if the compiler actually accepted
    the program without issuing a diagnostic as the Standard
    requires.

    When will people learn to post the actual code whose
    behavior mystifies them, instead of typing in something
    with a sketchy resemblance to that code? If a program's
    behavior baffles you, you are the LEAST qualified to make
    a paraphrase that preserves all the important points --
    you don't know what's going on, so you don't know what
    matters and what doesn't, what to leave in and what to
    remove. Solution: Take out nothing, add nothing, and
    post the actual code. Period.

    In this particular case it's easy to work backwards
    and see what you probably meant, but please Please PLEASE
    don't make a habit of this nonsense! Next time you may not
    be so lucky, and the combined Great Minds of c.l.c. will
    happily debug the errors you introduced in the process of
    making your paraphrase, leaving undiagnosed the actual error
    that's bothering you but that vanished in translation.

    Harrumph. Consider your wrist officially slapped.

    --
    Eric Sosman
    lid
     
    Eric Sosman, Jan 5, 2006
    #5
  6. "anonymous" <> writes:
    > From my understanding of pointers, a pointer should not be able to
    > access a memory location until that memory has been allocated either
    > by assiging the address of a variable or either through malloc i.e.
    >
    > ptr = &somevariable;
    >
    > or
    >
    > ptr = malloc ( sizeof ( data ) );
    >
    > However, what I discovered with the following program has made me a
    > bit uneasy. Not only can I read the memory pointed to by pointers
    > that still have not been allocated any memory, I can write as well.


    You *can*, but you may not.

    --
    Keith Thompson (The_Other_Keith) <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.
     
    Keith Thompson, Jan 5, 2006
    #6
  7. anonymous

    Guest

    anonymous wrote:
    > Dear All,
    >
    > From my understanding of pointers, a pointer SHOULD NOT BE ABLE to
    > access a memory location until that memory has been allocated either by
    > assiging the address of a variable or either through malloc i.e.
    > (note: emphasis mine)


    Actually this is not true. A pointer SHOULD NOT BE USED to access
    memory location until that memory has been allocated. C, being a system
    programming language SHOULD BE ABLE to do this though. C was intended
    to expose as much of the hardware as possible so that one can write an
    OS in it.

    If C can't do this how are you supposed to write a malloc function in C
    (for example the gnu malloc)? Another example use of an unallocated
    pointer is in embedded systems where you often don't have an OS
    running. In such cases it is common to access memory mapped hardware by
    simply pointing to it. Say for example you have an I/O card at memory
    location 0xffff10. You can simply access it by declaring int * io_card
    = 0xffff10. Of course, on a memory protected OS this doesn't work and
    segfaults. But C makes no assumption about the OS which is good since
    sometimes one needs to use it where an OS is not running.
     
    , Jan 5, 2006
    #7
  8. anonymous

    Guest

    anonymous wrote:
    > Dear All,
    >
    > From my understanding of pointers, a pointer SHOULD NOT BE ABLE to
    > access a memory location until that memory has been allocated either by
    > assiging the address of a variable or either through malloc i.e.
    > (note: emphasis mine)


    Actually this is not true. A pointer SHOULD NOT BE USED to access
    memory location until that memory has been allocated. C, being a system
    programming language SHOULD BE ABLE to do this though. C was intended
    to expose as much of the hardware as possible so that one can write an
    OS in it.

    If C can't do this how are you supposed to write a malloc function in C
    (for example the gnu malloc)? Another example use of an unallocated
    pointer is in embedded systems where you often don't have an OS
    running. In such cases it is common to access memory mapped hardware by
    simply pointing to it. Say for example you have an I/O card at memory
    location 0xffff10. You can simply access it by declaring int * io_card
    = 0xffff10. Of course, on a memory protected OS this doesn't work and
    segfaults. But C makes no assumption about the OS which is good since
    sometimes one needs to use it where an OS is not running.
     
    , Jan 5, 2006
    #8
  9. anonymous

    Guest

    anonymous wrote:
    > Dear All,
    >
    > From my understanding of pointers, a pointer SHOULD NOT BE ABLE to
    > access a memory location until that memory has been allocated either by
    > assiging the address of a variable or either through malloc i.e.
    > (note: emphasis mine)


    Actually this is not true. A pointer SHOULD NOT BE USED to access
    memory location until that memory has been allocated. C, being a system
    programming language SHOULD BE ABLE to do this though. C was intended
    to expose as much of the hardware as possible so that one can write an
    OS in it.

    If C can't do this how are you supposed to write a malloc function in C
    (for example the gnu malloc)? Another example use of an unallocated
    pointer is in embedded systems where you often don't have an OS
    running. In such cases it is common to access memory mapped hardware by
    simply pointing to it. Say for example you have an I/O card at memory
    location 0xffff10. You can simply access it by declaring int * io_card
    = 0xffff10. Of course, on a memory protected OS this doesn't work and
    segfaults. But C makes no assumption about the OS which is good since
    sometimes one needs to use it where an OS is not running.
     
    , Jan 5, 2006
    #9
  10. anonymous

    Jack Klein Guest

    On 5 Jan 2006 03:14:07 -0800, "anonymous" <> wrote
    in comp.lang.c:

    >
    > wrote:
    > > Hello
    > >
    > >
    > > This is luck that it works.
    > >
    > > The declared pointers have a value, C doesn't zero memory on
    > > declaration!!!
    > >
    > > What actual is happening is that you are reading/writeing on a random
    > > place in memory.
    > >
    > > the behaivior of this is undefined.
    > >
    > > All things can happen.
    > >
    > > Greetings Olaf

    >
    > That is what I also think i.e. the pointers are pointing to *any*
    > memory as per garbage
    > values stored in p1 and p2 and, in turn, garbage value in the pointed
    > memory is being
    > accessed. But just try this program below. According to above
    > hypothesis, this should
    > work again, but it does not. You get a segmentation fault even on a
    > read operation of an
    > arbitrary memory location. Why cannot I access this memory now if I
    > could access the
    > memory pointed to by *garbage* address in p1. In fact, I tried with
    > various input values
    > of address and it would not read or write to any other memory location
    > that I could think of. May be try generating all possible addresses and
    > try to read and write all memory
    > locations right from 0000 0000 to ffff ffff and then figure out any
    > result.
    >
    >
    > #include <stdio.h>
    > int main ( void )
    > {
    > int *p1, address;
    >
    > /* You can do read and write with the garbage address in p1 */
    > printf ( " *p1 = %d\n", *p1 );
    > *p1 = 0x12345678;
    >
    >
    >
    > /* Now get some address from the user */
    > scanf ( "%x", &address );
    >
    > /* Initialize your pointer with this address */
    > p1 = ( int * ) address;
    >
    > /* Try reading from memory pointed to by the address */
    > /* You get a segmentation fault this point onwards */
    >
    > printf ( " *p1 = %d\n", *p1 );
    >
    > /* Try writing to memory pointed to be the address */
    > *p1 = 0x12345678;
    >
    > /* Confirm the write operation */
    > printf ( " *p1 = %d\n", *p1 );
    >
    > return 0;
    > }


    "Undefined behavior" has a specific meaning in C, as defined in the C
    standard. Once a program generates undefined behavior, the C standard
    no longer places any requirements on it. No requirement to do what
    you want, no requirement to do something you did not want, no
    requirement to crash the program. No requirement to do the same thing
    twice. No requirements at all.

    Once you know that your program causes undefined behavior, as you have
    been told, than the reasons why any particular result happens or does
    not happen is not a language issue. There is no C answer.

    --
    Jack Klein
    Home: http://JK-Technology.Com
    FAQs for
    comp.lang.c http://c-faq.com/
    comp.lang.c++ http://www.parashift.com/c -faq-lite/
    alt.comp.lang.learn.c-c++
    http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html
     
    Jack Klein, Jan 6, 2006
    #10
  11. anonymous

    suresh Guest

    wrote:
    > anonymous wrote:
    > > Dear All,
    > >
    > > From my understanding of pointers, a pointer SHOULD NOT BE ABLE to
    > > access a memory location until that memory has been allocated either by
    > > assiging the address of a variable or either through malloc i.e.
    > > (note: emphasis mine)

    >
    > Actually this is not true. A pointer SHOULD NOT BE USED to access
    > memory location until that memory has been allocated. C, being a system
    > programming language SHOULD BE ABLE to do this though. C was intended
    > to expose as much of the hardware as possible so that one can write an
    > OS in it.
    >
    > If C can't do this how are you supposed to write a malloc function in C
    > (for example the gnu malloc)?


    Well, I don't have to implement malloc because it is provided by
    standard C. (I assume the standard C libraries provided need not be
    implemented in standard C)

    -suresh
     
    suresh, Jan 6, 2006
    #11
  12. anonymous

    anonymous Guest

    Eric Sosman wrote:
    > anonymous wrote:
    > > [...]
    > > However, what I discovered with the following program has made me a bit
    > > uneasy. [...]

    >
    > I'd be uneasy, too, if the compiler actually accepted
    > the program without issuing a diagnostic as the Standard
    > requires.
    >
    > When will people learn to post the actual code whose
    > behavior mystifies them, instead of typing in something
    > with a sketchy resemblance to that code? If a program's
    > behavior baffles you, you are the LEAST qualified to make
    > a paraphrase that preserves all the important points --
    > you don't know what's going on, so you don't know what
    > matters and what doesn't, what to leave in and what to
    > remove. Solution: Take out nothing, add nothing, and
    > post the actual code. Period.
    >
    > In this particular case it's easy to work backwards
    > and see what you probably meant, but please Please PLEASE
    > don't make a habit of this nonsense! Next time you may not
    > be so lucky, and the combined Great Minds of c.l.c. will
    > happily debug the errors you introduced in the process of
    > making your paraphrase, leaving undiagnosed the actual error
    > that's bothering you but that vanished in translation.


    I agree with all of the above that you have written. BUT, how do you
    ASSUME that I have not posted the *actual code*. Though it would
    be OT to describe all this, but the fact is I was just refreshing my
    knowledge reading a book and tried the above program on a
    thought to test pointers. Nothing more. There is no other *actual*
    code. So, who should LEARN in this case, what to post and what
    not to post?

    >
    > Harrumph. Consider your wrist officially slapped.


    Though what I have described above is fact of matter. You may
    take this as an official reply :)

    Thanks for your help.

    >
    > --
    > Eric Sosman
    > lid
     
    anonymous, Jan 6, 2006
    #12
  13. "anonymous" <> writes:
    > Eric Sosman wrote:
    >> anonymous wrote:
    >> > [...]
    >> > However, what I discovered with the following program has made me a bit
    >> > uneasy. [...]

    >>
    >> I'd be uneasy, too, if the compiler actually accepted
    >> the program without issuing a diagnostic as the Standard
    >> requires.
    >>
    >> When will people learn to post the actual code whose
    >> behavior mystifies them, instead of typing in something
    >> with a sketchy resemblance to that code? If a program's
    >> behavior baffles you, you are the LEAST qualified to make
    >> a paraphrase that preserves all the important points --
    >> you don't know what's going on, so you don't know what
    >> matters and what doesn't, what to leave in and what to
    >> remove. Solution: Take out nothing, add nothing, and
    >> post the actual code. Period.
    >>
    >> In this particular case it's easy to work backwards
    >> and see what you probably meant, but please Please PLEASE
    >> don't make a habit of this nonsense! Next time you may not
    >> be so lucky, and the combined Great Minds of c.l.c. will
    >> happily debug the errors you introduced in the process of
    >> making your paraphrase, leaving undiagnosed the actual error
    >> that's bothering you but that vanished in translation.

    >
    > I agree with all of the above that you have written. BUT, how do you
    > ASSUME that I have not posted the *actual code*. Though it would
    > be OT to describe all this, but the fact is I was just refreshing my
    > knowledge reading a book and tried the above program on a
    > thought to test pointers. Nothing more. There is no other *actual*
    > code. So, who should LEARN in this case, what to post and what
    > not to post?


    Your question was:

    ] Can somebody please explain, why pointers p1 and p2 can read from
    ] and read to memory that they have not been allocated?

    And here's the "actual code" that you posted:

    #include <stdio.h>
    int main ( void )
    {
    int *ptr1, *ptr2;

    /* Get read access to the memory pointed to by p1 and p2 */
    printf ( "*p1 = %d\n", *p1 );
    printf ( "*p2 = %d\n", *p2 );

    /* Write to the memory pointed to by p1 and p2 */

    *p1 = 1;
    *p2 = 2;

    /* Confirm the write operation */
    printf ( "*p1 = %d\n", *p1 );
    printf ( "*p2 = %d\n", *p2 );

    return 0;
    }

    You declare two variables called ptr1 and ptr2. You then refer to two
    variables called p1 and p2, which were not declared anywhere. Your
    question makes it clear that you were able to compile the program, and
    you were wondering about its run-time behavior. The code that you
    posted could not possibly have compiled.

    That's how we know you didn't post the actual code. Or am I missing
    something?

    --
    Keith Thompson (The_Other_Keith) <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.
     
    Keith Thompson, Jan 6, 2006
    #13
  14. anonymous said:

    > BUT, how do you ASSUME that I have not posted the *actual code*.


    Because you implied that you had run the code (otherwise how could you
    observe its behaviour?), and yet the code contained errors that would have
    prevented its compilation.

    So, in short, you lied.

    > Though it would
    > be OT to describe all this, but the fact is I was just refreshing my
    > knowledge reading a book and tried the above program on a
    > thought to test pointers.


    How can you try it without running it?
    How can you run it without compiling it?
    How can you compile it without getting rid of the errors in it?

    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999
    http://www.cpax.org.uk
    email: rjh at above domain (but drop the www, obviously)
     
    Richard Heathfield, Jan 6, 2006
    #14
  15. anonymous

    Guest

    suresh wrote:
    > wrote:
    > > anonymous wrote:
    > > > Dear All,
    > > >
    > > > From my understanding of pointers, a pointer SHOULD NOT BE ABLE to
    > > > access a memory location until that memory has been allocated either by
    > > > assiging the address of a variable or either through malloc i.e.

    > > <snip>
    > > If C can't do this how are you supposed to write a malloc function in C
    > > (for example the gnu malloc)?

    >
    > Well, I don't have to implement malloc because it is provided by
    > standard C. (I assume the standard C libraries provided need not be
    > implemented in standard C)
    >


    You don't HAVE to implement malloc. But it is nice to have the power to
    implement your own malloc if needed. Again my example is the gnu
    malloc, see http://www.faqs.org/docs/securing/gnumaloc.html for why you
    may want this.
     
    , Jan 6, 2006
    #15
  16. anonymous

    Chuck F. Guest

    suresh wrote:
    > wrote:
    >

    .... snip ...
    >>
    >> Actually this is not true. A pointer SHOULD NOT BE USED to
    >> access memory location until that memory has been allocated.
    >> C, being a system programming language SHOULD BE ABLE to do
    >> this though. C was intended to expose as much of the hardware
    >> as possible so that one can write an OS in it.
    >>
    >> If C can't do this how are you supposed to write a malloc
    >> function in C (for example the gnu malloc)?

    >
    > Well, I don't have to implement malloc because it is provided by
    > standard C. (I assume the standard C libraries provided need
    > not be implemented in standard C)


    You need not bend the standard very much to write a malloc. In the
    case of my nmalloc for DJGPP (ref below) I think all that is needed
    is the assumption that pointer to/from int casts are meaningful,
    that alignment can be measured against those casts, and that memory
    can be secured from the OS with the (non-standard) sbrk call.

    <http://cbfalconer.home.att.net/download/nmalloc.zip>

    --
    Some informative links:
    news:news.announce.newusers
    http://www.geocities.com/nnqweb/
    http://www.catb.org/~esr/faqs/smart-questions.html
    http://www.caliburn.nl/topposting.html
    http://www.netmeister.org/news/learn2quote.html
     
    Chuck F., Jan 6, 2006
    #16
  17. anonymous

    anonymous Guest

    Keith Thompson wrote:
    > "anonymous" <> writes:
    > > Eric Sosman wrote:
    > >> anonymous wrote:
    > >> > [...]
    > >> > However, what I discovered with the following program has made me a bit
    > >> > uneasy. [...]
    > >>
    > >> I'd be uneasy, too, if the compiler actually accepted
    > >> the program without issuing a diagnostic as the Standard
    > >> requires.
    > >>
    > >> When will people learn to post the actual code whose
    > >> behavior mystifies them, instead of typing in something
    > >> with a sketchy resemblance to that code? If a program's
    > >> behavior baffles you, you are the LEAST qualified to make
    > >> a paraphrase that preserves all the important points --
    > >> you don't know what's going on, so you don't know what
    > >> matters and what doesn't, what to leave in and what to
    > >> remove. Solution: Take out nothing, add nothing, and
    > >> post the actual code. Period.
    > >>
    > >> In this particular case it's easy to work backwards
    > >> and see what you probably meant, but please Please PLEASE
    > >> don't make a habit of this nonsense! Next time you may not
    > >> be so lucky, and the combined Great Minds of c.l.c. will
    > >> happily debug the errors you introduced in the process of
    > >> making your paraphrase, leaving undiagnosed the actual error
    > >> that's bothering you but that vanished in translation.

    > >
    > > I agree with all of the above that you have written. BUT, how do you
    > > ASSUME that I have not posted the *actual code*. Though it would
    > > be OT to describe all this, but the fact is I was just refreshing my
    > > knowledge reading a book and tried the above program on a
    > > thought to test pointers. Nothing more. There is no other *actual*
    > > code. So, who should LEARN in this case, what to post and what
    > > not to post?

    >
    > Your question was:
    >
    > ] Can somebody please explain, why pointers p1 and p2 can read from
    > ] and read to memory that they have not been allocated?
    >
    > And here's the "actual code" that you posted:
    >
    > #include <stdio.h>
    > int main ( void )
    > {
    > int *ptr1, *ptr2;
    >
    > /* Get read access to the memory pointed to by p1 and p2 */
    > printf ( "*p1 = %d\n", *p1 );
    > printf ( "*p2 = %d\n", *p2 );
    >
    > /* Write to the memory pointed to by p1 and p2 */
    >
    > *p1 = 1;
    > *p2 = 2;
    >
    > /* Confirm the write operation */
    > printf ( "*p1 = %d\n", *p1 );
    > printf ( "*p2 = %d\n", *p2 );
    >
    > return 0;
    > }
    >
    > You declare two variables called ptr1 and ptr2. You then refer to two
    > variables called p1 and p2, which were not declared anywhere. Your
    > question makes it clear that you were able to compile the program, and
    > you were wondering about its run-time behavior. The code that you
    > posted could not possibly have compiled.
    >
    > That's how we know you didn't post the actual code. Or am I missing
    > something?


    Oh. Now I see the point and understand what is meant by *actual code*.
    Very right, the code I posted was not the same file ptr.c that I tested
    on
    my computer which I do not use for the Internet. I could have
    probably taken the file to the other machine, but rather just typed it
    instead
    in my mail. Apart from the two erros, rest of the code is the same,
    *otherwise*
    ( I think this would convince me and others as well ).

    I understand posting the file gets me better help which you all
    provide.

    Many thanks,

    A.

    >
    > --
    > Keith Thompson (The_Other_Keith) <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.
     
    anonymous, Jan 6, 2006
    #17
  18. anonymous

    anonymous Guest

    Richard Heathfield wrote:
    > anonymous said:
    >
    > > BUT, how do you ASSUME that I have not posted the *actual code*.

    >
    > Because you implied that you had run the code (otherwise how could you
    > observe its behaviour?), and yet the code contained errors that would have
    > prevented its compilation.


    You are absolutely alright and I have explained in the other post.

    > So, in short, you lied.


    I regret the mistake, but don't you think that "lie" is a bit harsh
    word as used here?

    >
    > > Though it would
    > > be OT to describe all this, but the fact is I was just refreshing my
    > > knowledge reading a book and tried the above program on a
    > > thought to test pointers.

    >
    > How can you try it without running it?
    > How can you run it without compiling it?
    > How can you compile it without getting rid of the errors in it?
    >
    > --
    > Richard Heathfield
    > "Usenet is a strange place" - dmr 29/7/1999
    > http://www.cpax.org.uk
    > email: rjh at above domain (but drop the www, obviously)
     
    anonymous, Jan 6, 2006
    #18
  19. anonymous <> schrieb:

    > int main ( void )
    > {
    > int *ptr1, *ptr2;
    >
    > /* Write to the memory pointed to by p1 and p2 */
    >
    > *p1 = 1;
    > *p2 = 2;


    Hey, don't you ever do that again! One of these pointers actually
    pointed to my bank account and corrupted it.

    ;-)

    Markus
     
    Markus Becker, Jan 6, 2006
    #19
  20. In article <>,
    <> wrote:
    >anonymous wrote:
    >> Dear All,
    >>
    >> From my understanding of pointers, a pointer SHOULD NOT BE ABLE to
    >> access a memory location until that memory has been allocated either by
    >> assiging the address of a variable or either through malloc i.e.
    >> (note: emphasis mine)

    >
    >Actually this is not true. A pointer SHOULD NOT BE USED to access
    >memory location until that memory has been allocated. C, being a system
    >programming language SHOULD BE ABLE to do this though. C was intended
    >to expose as much of the hardware as possible so that one can write an
    >OS in it.


    According to the "experts" in this ng, you *can't* write an OS in C.

    (This is taking the religious dogma as expressed by some that anything that
    isn't "standard C" isn't "C". Yes, as hard as it is to believe, I've seen
    exactly that thought expressed here in just those words.)
     
    Kenny McCormack, Jan 6, 2006
    #20
    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. Maxwell Hammer
    Replies:
    7
    Views:
    669
    Peter Hansen
    Jun 18, 2005
  2. doubt related to string pointers.

    , May 16, 2005, in forum: C Programming
    Replies:
    8
    Views:
    364
    Barry Schwarz
    May 22, 2005
  3. Replies:
    3
    Views:
    320
    Morris Keesan
    Sep 2, 2009
  4. cerr

    pointers, pointers, pointers...

    cerr, Apr 7, 2011, in forum: C Programming
    Replies:
    12
    Views:
    729
  5. Replies:
    8
    Views:
    184
    MenTaLguY
    Dec 9, 2005
Loading...

Share This Page