Sudden doubt

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • K. Jennings

    #1

    Sudden doubt

    We have the following:

    typedef struct {
    char *p ;
    } data_structure ;

    void f(data_structur e *data)
    {
    char *r = malloc(16) ;

    data->p = r ;

    return ;
    }

    Assuming that malloc returns a non-NULL pointer, data->p will
    point to a valid memory region for as long as data is not released. Is
    this correct? I know that when f() returns the value of r becomes
    undefined, but I assume that whatever it was pointing to will be left
    untouched.

  • Richard Bos

    #2
    Re: Sudden doubt

    "K. Jennings" <kjennings@resu rgence.netwrote :
    typedef struct {
    char *p ;
    } data_structure ;
    >
    void f(data_structur e *data)
    {
    char *r = malloc(16) ;
    >
    data->p = r ;
    >
    return ;
    }
    >
    Assuming that malloc returns a non-NULL pointer, data->p will
    point to a valid memory region for as long as data is not released. Is
    this correct? I know that when f() returns the value of r becomes
    undefined, but I assume that whatever it was pointing to will be left
    untouched.
    Entirely correct. Do remember to free() data->p (which is now your only
    pointer to that allocated block) later on.

    Richard

    Comment

    • santosh

      #3
      Re: Sudden doubt

      On Wednesday 18 Jul 2007 5:57 pm, in comp.lang.c, K. Jennings
      <kjennings@resu rgence.netwrote :
      Message ID: <pan.2007.07.18 .12.27.06@resur gence.net>
      We have the following:
      >
      typedef struct {
      char *p ;
      } data_structure ;
      >
      void f(data_structur e *data)
      Hopefully data is initialised to point to a valid data_structure
      object.
      {
      char *r = malloc(16) ;
      >
      data->p = r ;
      >
      return ;
      }
      >
      Assuming that malloc returns a non-NULL pointer, data->p will
      point to a valid memory region for as long as data is not released.
      Is this correct?
      Yes.
      I know that when f() returns the value of r
      becomes undefined, but I assume that whatever it was pointing to
      will be left untouched.
      Yes, provided you've taken care to save the value previously in r.


      Comment

      • Barry Schwarz

        #4
        Re: Sudden doubt

        On 18 Jul 2007 12:27:05 GMT, "K. Jennings" <kjennings@resu rgence.net>
        wrote:
        > We have the following:
        >
        >typedef struct {
        char *p ;
        >} data_structure ;
        >
        >void f(data_structur e *data)
        >{
        char *r = malloc(16) ;
        >
        data->p = r ;
        >
        return ;
        >}
        >
        > Assuming that malloc returns a non-NULL pointer, data->p will
        >point to a valid memory region for as long as data is not released. Is
        >this correct? I know that when f() returns the value of r becomes
        >undefined, but I assume that whatever it was pointing to will be left
        Others have answered your intended question. This is just a
        terminology nit. When f returns, r ceases to exist. Consequently,
        the value that was in r no longer exists **in r**. However, since you
        had the foresight to copy the value to data->p, that value is still
        available to the function that called f or to any other function that
        can find the struct.

        That value is the address of allocated memory which will remain
        allocated until you explicitly free it. If the address in r had
        pointed to another automatic object in f, that object would also cease
        to exist when f returned and, by definition, the value in data->p
        would then become indeterminate.
        >untouched.
        >

        Remove del for email

        Comment

        • Kenneth Brody

          #5
          Re: Sudden doubt

          "K. Jennings" wrote:
          >
          We have the following:
          >
          typedef struct {
          char *p ;
          } data_structure ;
          >
          void f(data_structur e *data)
          {
          char *r = malloc(16) ;
          >
          data->p = r ;
          >
          return ;
          }
          >
          Assuming that malloc returns a non-NULL pointer, data->p will
          point to a valid memory region for as long as data is not released. Is
          this correct? I know that when f() returns the value of r becomes
          undefined, but I assume that whatever it was pointing to will be left
          untouched.
          I believe you are getting confused because of previous posts which
          included code along the lines of:

          void f(data_structur e *data)
          {
          char r[16];

          data->p = r;

          return;
          }

          In this case, you are left with data->p pointing to something that
          no longer exists. (That is, r goes away upon return, yet data->p
          still points to r.)

          However, in your case, r is simply a pointer to the address returned
          by malloc(). You are storing the _value_ of r into data->p, not its
          address. The address returned by malloc() (assuming it succeeded)
          is still valid until you free() it. The fact that r goes away is
          irrelevent, as r was simply used to hold a value.

          Consider that the body of your function could be written:

          data->p = malloc(16);

          The results are identical, and there is no "r" do disappear. (In
          fact, a modern optimizing compiler may treat your code as if r did
          not exist in the first place.)

          --
          +-------------------------+--------------------+-----------------------+
          | Kenneth J. Brody | www.hvcomputer.com | #include |
          | kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer .h|
          +-------------------------+--------------------+-----------------------+
          Don't e-mail me at: <mailto:ThisIsA SpamTrap@gmail. com>

          Comment

          Working...