Results 1 to 10 of 10

Thread: Drowning in Vectors

  1. #1

    Thread Starter
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256

    Drowning in Vectors

    Okay. I just learned how to use vectors. And now I'm addicted. I have like 10 in my project right now, and I'm wondering if that's too many?

    Do vectors use a lot of memory? Do they clean themselves up? Is there something special I should do to clean up the vectors at the end of my program.

    Thanks.
    My evil laugh has a squeak in it.

    kristopherwilson.com

  2. #2
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    They clean them up themselves. And they have only little overhead over the size of the data they actually contain. So unless it's just a hello world app 10 vectors is not too much.

    But you should still think for every single vector: "Do I need this dynamic?". The vector class is really nice, but there's no excuse for using a vector for a static array.
    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
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    The reason I'm using them to begin with is that I created a sub to make increase the size of a dynamic array, then posted it here to get feedback on how I was doing it, and the feedback was "use a vector."

    I just happened to have 4 or 5 dynamic arrays and changed them to vectors.

    So if I ask myself "do I really need this one?" I can't really answer unless I know that it's the best way to do it.
    My evil laugh has a squeak in it.

    kristopherwilson.com

  4. #4
    Frenzied Member
    Join Date
    Jul 2002
    Posts
    1,370
    After the first instance of a class is declared & used the overhead is a lot less for each subsequent instance of the same class object.

    So, ten is only too many if can't keep track of them or you really don't need them.

    One of the things I do is to make existing code faster. As a side effect, it usually means way fewer lines of code. If you pass data back and forth between unnecessary variables it adds overhead as well as increasing complexity (probability of failure goes up).

    It's your call

  5. #5
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Nothing wrong with converting dynamic arrays to vectors. But do you need them dynamic?

    How large is your project? If it's a real app you write alone (small to medium project size) 10 vectors is not much. If it's some uni assignment (tiny project size) 10 dynamic arrays should make you wonder if your approach to the problem is good.
    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.

  6. #6

    Thread Starter
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    Thanks, guys.

    I'm pretty sure I need them dynamic. My program calls for the user to be able to enter stock info, and I want them to enter however many stocks they please, from 1 to however many stocks there are.
    My evil laugh has a squeak in it.

    kristopherwilson.com

  7. #7
    Frenzied Member
    Join Date
    Jul 2002
    Posts
    1,370
    FWIW - the C99 standard supports dynamic arrays in a bass-ackwards way.

    They are called variable length arrays.

    Code:
    void f(int dimension1, int dimension2){
           char whatever[dimension1][dimension2];
    }

  8. #8
    Junior Member
    Join Date
    Oct 2002
    Location
    MI
    Posts
    23
    Maybe i should get around to learning more about C++, this stuff sounds kinda cool.

  9. #9

    Thread Starter
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    Originally posted by Zaffir
    Maybe i should get around to learning more about C++, this stuff sounds kinda cool.
    I just learned about them yesterday. They're pretty sweet.
    My evil laugh has a squeak in it.

    kristopherwilson.com

  10. #10
    Frenzied Member Zaei's Avatar
    Join Date
    Jul 2002
    Location
    My own little world...
    Posts
    1,710
    Originally posted by jim mcnamara
    FWIW - the C99 standard supports dynamic arrays in a bass-ackwards way.

    They are called variable length arrays.

    Code:
    void f(int dimension1, int dimension2){
           char whatever[dimension1][dimension2];
    }
    That is interesting, Jim. Thanks for the info!

    Z.

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