Results 1 to 16 of 16

Thread: Array of pointers, dynamically

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Sep 2000
    Posts
    18

    Post Array of pointers, dynamically

    How do i create an array of pointers to a certain class (eg LLNode), but the size of the array needs to be dynamic????



    Thanks in advance.

  2. #2
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    you could use vector<LLNode*> from STL, but there are some things to remember when using pointers in the STL containers.

    I just forgot where I found it. basically, watch out if you remove any pointers from the array, if they pointed to heap memory you have a leak, since STL doesn't give you a chance to deallocate them.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Sep 2000
    Posts
    18
    well basically i dont want to use templates

    and the purpose of the array is to delete heap memory of a Linked Link

    but a forward linked link only (no backwards pointers) .. so i was going to read all the pointers into an array and delete them one my one ...

  4. #4
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    I have no idea what you wanna do...
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Sep 2000
    Posts
    18
    psuedo code

    while (still more members)
    copy pointer into array
    move on
    end while

    while (not at end of array)
    delete pointer
    end while


    but the array needs to be dynamic in size at run time

  6. #6
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    why do you use this complicated syntax?
    It would work with STL why not use it?
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Sep 2000
    Posts
    18
    because i dont know how .. and to me this seams easy

  8. #8
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    If you don't use STL you'd have to write your own dynamic array...
    Using vector is very simple. You include <vector> and use std namespace. Then you can create vector objects:
    Code:
    #include <vector>
    using namespace std;
    
    vector<LLNode*> nodes;
    see this link for example:
    reference
    It's a litle technical, but it's complete with iterators and algorithms.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  9. #9
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    a vector has it's own drawbacks (an empty takes 16 bytes), but it's generally usefull for many purposes. Creating a simpler container is good if you want to be strict about resources, for instance if you're developing a large scale project. But most of the case you will do fine with vectors, and developing your own containers just takes unnesseraily much time and you still might end up with not too usefull containers with possible hidden bugs that hardly don't exist in STL.

    If you still wan't to create your own containers, I could of course help.
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  10. #10

    Thread Starter
    Junior Member
    Join Date
    Sep 2000
    Posts
    18
    thanks ... yeah i want to .. but i want to stay away from templates for now

  11. #11
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    Why? Is there something you don't think you can understand without much insight?
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  12. #12

    Thread Starter
    Junior Member
    Join Date
    Sep 2000
    Posts
    18
    not really .. its just something i cant be bothered to learn right now .. and there has got to be another way?

  13. #13
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    xeah there is: do what the compiler does for you with templates: hard code it for every data type you need.
    If you want to write your own dynamic container, it must fulfill this criterias:
    At adding new elements, it must check it's own size and reallocate it's memory if necessary.
    At removing elements, it should free unused memory.
    It should provide the same interface as an array, meaning you should (but don't have to if only you use it) should override the [] operator and such.
    It must not leave any leak anywhere. Use the destructor to make sure of that.
    Oh, yeah, it should not be too slow...

    Anything else?
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  14. #14

    Thread Starter
    Junior Member
    Join Date
    Sep 2000
    Posts
    18
    thanks for that, i shall do

  15. #15
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    Damn Cornedbee, we failed!
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  16. #16
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Next time I will tell them that there is no other way
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width