resizable array of pointers

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • David W

    #16
    Re: resizable array of pointers

    "Mark" <mnbayazit@gmai l.comwrote in message
    news:1160102057 .156398.52090@i 42g2000cwa.goog legroups.com...
    David W wrote:

    My guess is that the numbers you are putting in there no longer exist, but you didn't include
    that
    code. If the ints are ordinary local variables they may have gone out of scope. You may need to
    create each int to be stored with 'new' if that's the case (and delete later).
    >
    okay, that worked like a charm!
    >
    now..last question..i hope.
    >
    i've got my destructor
    >
    ObStash::~ObSta sh()
    {
    delete [] storage;
    }
    >
    but will that be sufficient to prevent leakage?
    Not if the objects pointed to were created with 'new'.
    since it's an array of pointers. i figure i would have to delete each
    pointer first,
    Yes, and to the correct type of pointer as well (the same type created with 'new').
    and then delete the array, since it's declared as void
    **storage, however.. i can't delete each pointer because they delete
    void* is undefined!
    >
    but, from my understanding, it's not necessarily delete integers
    anyways..
    Yes it is.
    they'll take care of themselves..
    No they won't. If they were created with 'new' they consume space in the free store like anything
    else until they are explicitly deleted.
    but the Student class
    however, is another story.
    A related story.
    class Student
    {
    string name;
    ObStash scores;
    >
    public:
    Student(string name);
    void print();
    void cleanUp();
    };
    >
    the string has it's own destructor i believe, and should clean up after
    it's self...
    Yes, as part of the destruction of the Student.
    the obstash's destructor should clean up the scores too i think..
    No, it shouldn't. It can't know the types really being pointed to by the array elements, and it
    can't know whether those objects were created with 'new'.
    but what about when
    >
    ObStash students;
    >
    goes out of scope?
    >
    do i need to call the destructor for each object individually or
    anything..? (delete each student manually)
    Yes.
    or..will they all die
    gracefully?
    No.

    DW



    Comment

    • neel

      #17
      Re: resizable array of pointers


      Mark wrote:
      i need to make a class that can store anything (integers,objec ts,etc)
      it needs to maintain a list of pointers to these objects
      the list needs to be resized to accomodate more elements
      i can't use any special classes
      >
      i just can't seem to figure it out.
      >
      i've got a pointer,
      >
      void *storage
      >
      which will point to the start of the list
      >
      then there will be one pointer, every 4 bytes (sizeof(void*))
      >
      which i *think* i can access by doing
      >
      storage[index*sizeof(vo id*)]
      >
      i'm not sure if I need to cast that to a void pointer or not, since
      that's already what it's defined as...
      >
      but then to resize this beast, i'm completely lost
      >
      i create a new void pointer..
      >
      void* temp = new void*[length]
      >
      where length is the new size (how many pointers it will hold)
      >
      which should allocate length*4 bytes of memory...
      >
      and then i tried doing
      >
      for(int i=0; i<count; i++)
      {
      temp[i] = storage[i*size];
      }
      >
      to copy the pointers over
      where count is the number of elements currently in the stash, but my
      compiler complains...
      >
      `void*' is not a pointer-to-object type
      pointer of type `void *' used in arithmetic
      >
      and...i don't really know what else to do.
      >
      any help?

      Comment

      • neel

        #18
        link list


        i have a single link list with with 5 nodes .if 5 nodes address part
        pointing to 2nd node. then show there is a loop . code it in c.
        help me.

        Comment

        • Victor Bazarov

          #19
          Re: link list

          neel wrote:
          i have a single link list with with 5 nodes .if 5 nodes address part
          pointing to 2nd node. then show there is a loop . code it in c.
          For C questions please ask in comp.lang.c. Also, read the FAQ before
          posting (there and anywhere else). They won't do your homework just
          like we won't.

          V
          --
          Please remove capital 'A's when replying by e-mail
          I do not respond to top-posted replies, please don't ask


          Comment

          • Mark

            #20
            Re: resizable array of pointers


            David W wrote:
            "Mark" <mnbayazit@gmai l.comwrote in message
            news:1160102057 .156398.52090@i 42g2000cwa.goog legroups.com...
            David W wrote:
            >
            My guess is that the numbers you are putting in there no longer exist, but you didn't include
            that
            code. If the ints are ordinary local variables they may have gone out of scope. You may need to
            create each int to be stored with 'new' if that's the case (and delete later).
            >
            okay, that worked like a charm!

            now..last question..i hope.

            i've got my destructor

            ObStash::~ObSta sh()
            {
            delete [] storage;
            }

            but will that be sufficient to prevent leakage?
            >
            Not if the objects pointed to were created with 'new'.
            >
            since it's an array of pointers. i figure i would have to delete each
            pointer first,
            >
            Yes, and to the correct type of pointer as well (the same type created with 'new').
            >
            and then delete the array, since it's declared as void
            **storage, however.. i can't delete each pointer because they delete
            void* is undefined!

            but, from my understanding, it's not necessarily delete integers
            anyways..
            >
            Yes it is.
            >
            they'll take care of themselves..
            >
            No they won't. If they were created with 'new' they consume space in the free store like anything
            else until they are explicitly deleted.
            >
            but the Student class
            however, is another story.
            >
            A related story.
            >
            class Student
            {
            string name;
            ObStash scores;

            public:
            Student(string name);
            void print();
            void cleanUp();
            };

            the string has it's own destructor i believe, and should clean up after
            it's self...
            >
            Yes, as part of the destruction of the Student.
            >
            the obstash's destructor should clean up the scores too i think..
            >
            No, it shouldn't. It can't know the types really being pointed to by the array elements, and it
            can't know whether those objects were created with 'new'.
            >
            but what about when

            ObStash students;

            goes out of scope?

            do i need to call the destructor for each object individually or
            anything..? (delete each student manually)
            >
            Yes.
            >
            or..will they all die
            gracefully?
            >
            No.
            >
            DW
            hm.. I think I was getting confused with something else. thank you for
            your help.

            Comment

            Working...