Newb: Pointers, Refs, & Arrays

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jeremy Cowles

    Newb: Pointers, Refs, & Arrays

    I have the following:

    int *array = new int[5];


    and I destroy it with:

    delete [ ] array;


    So my questions is, what happens when you destory the array like this:

    delete array;

    ?


    Furthermore, I understand that References are like a pointer, but are
    somewhat safer (and less flexable). So what is a "weak" reference?

    TIA,
    Jeremy


  • Jeremy Cowles

    #2
    Re: Pointers, Refs, & Arrays

    BTW: I actually WAS destroying the array improperly up until about 5 min ago
    when I noticed the missing brackets.

    :)




    "Jeremy Cowles" <jeremy.stop-spam-now.cowles@asif l.com> wrote in message
    news:jRVSa.1329 67$ic1.2968502@ twister.tampaba y.rr.com...[color=blue]
    > I have the following:
    >
    > int *array = new int[5];
    >
    >
    > and I destroy it with:
    >
    > delete [ ] array;
    >
    >
    > So my questions is, what happens when you destory the array like this:
    >
    > delete array;
    >
    > ?
    >
    >
    > Furthermore, I understand that References are like a pointer, but are
    > somewhat safer (and less flexable). So what is a "weak" reference?
    >
    > TIA,
    > Jeremy
    >
    >
    >[/color]

    Comment

    • Alf P. Steinbach

      #3
      Re: Newb: Pointers, Refs, &amp; Arrays

      On Mon, 21 Jul 2003 17:58:39 GMT, "Jeremy Cowles" <jeremy.stop-spam-now.cowles@asif l.com> wrote:
      [color=blue]
      >I have the following:
      >
      >int *array = new int[5];
      >
      >
      >and I destroy it with:
      >
      >delete [ ] array;
      >
      >
      >So my questions is, what happens when you destory the array like this:
      >
      >delete array;[/color]

      For an array of basic type this will "work" with most compilers. But
      formally it's undefined, and so you have no reason to complain if all
      or nothing of the memory is reclaimed, or if operator delete decides
      to send a truthful e-mail to W. Whatever.

      In practice, delete[] does this in addition to what delete does: it
      calls destructors of all objects in the array, for an array of objects.



      [color=blue]
      >Furthermore, I understand that References are like a pointer, but are
      >somewhat safer (and less flexable). So what is a "weak" reference?[/color]

      That is not C++ terminology but general terminology. A "weak" reference
      or pointer to an object O is a reference or pointer that doesn't keep O
      alive. Various schemes such as reference counting are used to keep
      objects alive. With ref-counting an object self-destroys when its ref-
      count goes down to zero, and in that case a "weak" reference is a pointer
      to O where O's ref-count hasn't been incremented to reflect that this
      pointer exists, which can be useful for example to receive events from O.

      Comment

      • Karl Heinz Buchegger

        #4
        Re: Newb: Pointers, Refs, &amp; Arrays



        "Alf P. Steinbach" wrote:[color=blue]
        >[color=green]
        > >delete array;[/color]
        >
        > For an array of basic type this will "work" with most compilers.[/color]

        It 'appears' as if it would work. But think of the following:
        Somewhere there has to be stored the information how large that array
        has been allocated. If someone faulty does:

        int* i = new int [ some_num ];
        delete i;

        this additional information is never freed. And depending on how this
        additional information is allocated exactly (some systems store the
        array size in a few bytes before the actual array) the whole array is
        never freed.

        --
        Karl Heinz Buchegger
        kbuchegg@gascad .at

        Comment

        • Alf P. Steinbach

          #5
          Re: Newb: Pointers, Refs, &amp; Arrays

          On Tue, 22 Jul 2003 09:42:23 +0200, Karl Heinz Buchegger <kbuchegg@gasca d.at> wrote:
          [color=blue]
          >"Alf P. Steinbach" wrote:[color=green]
          >>[color=darkred]
          >> >delete array;[/color]
          >>
          >> For an array of basic type this will "work" with most compilers.[/color][/color]

          Out of context quote; continuation was


          But formally it's undefined, and so you have no reason to
          complain if all or nothing of the memory is reclaimed, or if
          operator delete decides to send a truthful e-mail to W. Whatever.

          [color=blue]
          >It 'appears' as if it would work. But think of the following:
          >Somewhere there has to be stored the information how large that array
          >has been allocated. If someone faulty does:
          >
          > int* i = new int [ some_num ];
          > delete i;
          >
          >this additional information is never freed.[/color]

          That is incorrect. It _may_ never be freed, depending on the memory
          management, the compiler and perhaps more things. But that would be
          a most unusual C++ implementation (and to be very clear, that does
          not mean that it is formally well-defined or acceptable code). The
          reason it would be most unusual is that 'int' is a built-in type, and
          so there is no reason for additional C++ level information. All that's
          needed is the memory manager's own block size.

          [color=blue]
          >And depending on how this additional information is allocated exactly
          >(some systems store the array size in a few bytes before the actual
          >array) the whole array is never freed.[/color]

          That again would be most unusual; I don't believe there is any C++
          compiler where that is the case, but it could in principle exist.

          Comment

          • Jeremy Cowles

            #6
            Re: Newb: Pointers, Refs, &amp; Arrays

            Thanks all.


            "Alf P. Steinbach" <alfps@start.no > wrote in message
            news:3f1d15d4.4 30495968@News.C IS.DFN.DE...[color=blue]
            > On Tue, 22 Jul 2003 09:42:23 +0200, Karl Heinz Buchegger[/color]
            <kbuchegg@gasca d.at> wrote:[color=blue]
            >[color=green]
            > >"Alf P. Steinbach" wrote:[color=darkred]
            > >>
            > >> >delete array;
            > >>
            > >> For an array of basic type this will "work" with most compilers.[/color][/color]
            >
            > Out of context quote; continuation was
            >
            >
            > But formally it's undefined, and so you have no reason to
            > complain if all or nothing of the memory is reclaimed, or if
            > operator delete decides to send a truthful e-mail to W. Whatever.
            >
            >[color=green]
            > >It 'appears' as if it would work. But think of the following:
            > >Somewhere there has to be stored the information how large that array
            > >has been allocated. If someone faulty does:
            > >
            > > int* i = new int [ some_num ];
            > > delete i;
            > >
            > >this additional information is never freed.[/color]
            >
            > That is incorrect. It _may_ never be freed, depending on the memory
            > management, the compiler and perhaps more things. But that would be
            > a most unusual C++ implementation (and to be very clear, that does
            > not mean that it is formally well-defined or acceptable code). The
            > reason it would be most unusual is that 'int' is a built-in type, and
            > so there is no reason for additional C++ level information. All that's
            > needed is the memory manager's own block size.
            >
            >[color=green]
            > >And depending on how this additional information is allocated exactly
            > >(some systems store the array size in a few bytes before the actual
            > >array) the whole array is never freed.[/color]
            >
            > That again would be most unusual; I don't believe there is any C++
            > compiler where that is the case, but it could in principle exist.
            >
            >[/color]

            Comment

            Working...