Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #66977

Re: I want to ask a question...

From Keith Thompson <kst-u@mib.org>
Newsgroups comp.lang.c
Subject Re: I want to ask a question...
Date 2015-08-07 15:30 -0700
Organization None to speak of
Message-ID <lnmvy2g291.fsf@kst-u.example.com> (permalink)
References <mq2uol$dpj$3@dont-email.me> <87pp2y7wl7.fsf@gmail.com>

Show all headers | View raw


Gareth Owen <gwowen@gmail.com> writes:
> Ramine <ramine@1.1> writes:
>> Suppose that you are using a pointer that points to an allocated chunk
>> of memory, and suppose that you deallocated this chunk of memory and
>> you have keeped the old address of this pointer inside another pointer
>> , is there a problem to access the old address of the pointer and
>> content of the memory even if it was deallocated ?
>
> You can access the pointer itself, but you can't access the memory it
> points to.  Sometimes it might work - and it simple programs it probably
> will work more ofhen than not - but you can't rely on it.
>
> Don't do that.

That's not quite correct.

Accessing a pointer value after the object it points to no longer exists
has undefined behavior, even if you don't dereference it.  It's not
likely to do anything nasty on most systems, but in principle an
implementation could mark the pointer value as invalid and trap on any
reference to it.

An example:

#include <stdlib.h>
#include <stdio.h>
int main(void) {
    int *p = malloc(sizeof *p);
    if (p == NULL) exit(EXIT_FAILURE);
    *p = 42;
    printf("p = %p\n", (void*)p); // OK
    printf("*p = %d\n", *p);      // OK, prints 42

    free(p);

    printf("p = %p\n", (void*)p);
        // Undefined behavior, *probably* won't crash

    printf("*p = %d\n", *p);
        // Undefined behavior, *probably* won't crash
        // The memory location may or may not have been clobbered.

    return 0;
}

Don't access either the pointer itself *or* the memory it points to.

Of course a write access to the pointer object (such as assigning a new
value to it) is fine, since it doesn't access the stored value.

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
Working, but not speaking, for JetHead Development, Inc.
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Re: I want to ask a question... Gareth Owen <gwowen@gmail.com> - 2015-08-07 20:00 +0100
  Re: I want to ask a question... glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2015-08-07 19:04 +0000
  Re: I want to ask a question... Keith Thompson <kst-u@mib.org> - 2015-08-07 15:30 -0700
    Re: I want to ask a question... glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2015-08-07 22:52 +0000
      Re: I want to ask a question... Keith Thompson <kst-u@mib.org> - 2015-08-07 16:46 -0700
        Re: I want to ask a question... glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2015-08-07 23:29 +0000
          Re: I want to ask a question... Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-08-08 01:37 +0100
          Re: I want to ask a question... Keith Thompson <kst-u@mib.org> - 2015-08-07 18:55 -0700
            Re: I want to ask a question... glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2015-08-08 05:51 +0000
              Re: I want to ask a question... Robert Wessel <robertwessel2@yahoo.com> - 2015-08-08 01:44 -0500
    Re: I want to ask a question... Ken Brody <kenbrody@spamcop.net> - 2015-08-10 12:46 -0400
    Re: I want to ask a question... supercat@casperkitty.com - 2015-08-19 16:43 -0700
      Re: I want to ask a question... Keith Thompson <kst-u@mib.org> - 2015-08-19 18:09 -0700
        Re: I want to ask a question... glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2015-08-20 01:00 +0000
          Re: I want to ask a question... Keith Thompson <kst-u@mib.org> - 2015-08-19 20:25 -0700
            Re: I want to ask a question... supercat@casperkitty.com - 2015-08-20 08:15 -0700
              Re: I want to ask a question... Keith Thompson <kst-u@mib.org> - 2015-08-20 11:38 -0700
                Re: I want to ask a question... glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2015-08-20 18:47 +0000
                Re: I want to ask a question... supercat@casperkitty.com - 2015-08-20 14:01 -0700
            Re: I want to ask a question... glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2015-08-20 16:43 +0000
              Re: I want to ask a question... James Kuyper <jameskuyper@verizon.net> - 2015-08-20 13:02 -0400
                Re: I want to ask a question... Tonton Th <tTh@nowhere.invalid> - 2015-08-20 17:40 +0000
                Re: I want to ask a question... James Kuyper <jameskuyper@verizon.net> - 2015-08-20 15:27 -0400
                Re: I want to ask a question... David Brown <david.brown@hesbynett.no> - 2015-08-20 22:58 +0200
                Re: I want to ask a question... James Kuyper <jameskuyper@verizon.net> - 2015-08-20 23:24 -0400
                Re: I want to ask a question... glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2015-08-21 06:19 +0000
                Re: I want to ask a question... Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2015-08-21 09:20 -0600
                Re: I want to ask a question... supercat@casperkitty.com - 2015-08-22 12:25 -0700
                Re: I want to ask a question... Robert Wessel <robertwessel2@yahoo.com> - 2015-08-23 01:33 -0500
                Re: I want to ask a question... glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2015-08-20 19:28 +0000
              Re: I want to ask a question... Keith Thompson <kst-u@mib.org> - 2015-08-20 12:11 -0700
          Re: I want to ask a question... David Brown <david.brown@hesbynett.no> - 2015-08-20 10:02 +0200
        Re: I want to ask a question... raltbos@xs4all.nl (Richard Bos) - 2015-08-22 13:42 +0000
          Re: I want to ask a question... supercat@casperkitty.com - 2015-08-22 12:23 -0700

csiph-web