CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 3 of 7 FirstFirst 123456 ... LastLast
Results 31 to 45 of 101

Thread: array vs vector

  1. #31
    Join Date
    Aug 2005
    Location
    Netherlands, The
    Posts
    2,184

    Re: array vs vector

    Quote Originally Posted by dcjr84
    There is a simple reason reason to initialize an array, and that's because
    if left uninitialized you just don't know what the elements contain.
    This is very untrue, and your whole post is based around this argument.
    It shuld be initialised. But not by vector. By user. having an array of 0's is also garbage.

  2. #32
    Join Date
    Aug 2005
    Location
    Netherlands, The
    Posts
    2,184

    Re: array vs vector

    Quote Originally Posted by laserlight
    It isnt garbage since in this case the programmer requires a vector of a million 10s.
    ... so you just created million 10's while you only need the number 10, good job.

  3. #33
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: array vs vector

    It shuld be initialised. But not by vector. By user. having an array of 0's is also garbage.
    If the user writes:
    Code:
    std::vector<int> x(1000000);
    Then the user is creating a vector of a million 0s. It is as simple as that. What the user requests be done surely cannot be garbage.

  4. #34
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: array vs vector

    ... so you just created million 10's while you only need the number 10, good job.
    If I only need the number 10 stored in a variable, I would write:
    Code:
    int x = 10;
    If I needed an int vector with a capacity of million elements, the first of which I currently know to be 10, I would write:
    Code:
    std::vector<int> x;
    x.reserve(1000000);
    x.push_back(10);
    The above vector only has a size of 1, and rightly so since only 1 int is what I need at the moment.

    This is, frankly, much ado about nothing.

  5. #35
    Join Date
    Aug 2005
    Location
    Netherlands, The
    Posts
    2,184

    Re: array vs vector

    you know im not going to discuss anymore, if you guys dont even want to try think my position fine, stay one minded.

  6. #36
    Join Date
    Jun 2002
    Location
    Moscow, Russia.
    Posts
    2,176

    Re: array vs vector

    dcjr84
    I disagree with your reasoning. Having initialized values is important, but not because it allows sloppy code to work. In your case you better should have array initialized to something like minimum possible value of integer to show error right when it occurs. Having consistent result guarantees repeatability of error behaviour.
    "Programs must be written for people to read, and only incidentally for machines to execute."

  7. #37
    Join Date
    Sep 2005
    Location
    United States
    Posts
    799

    Re: array vs vector

    Vectors, and especially arrays, should always be
    initialized. period. Doesn't matter if its by the user or done
    automatically by it's implementation. The performance impact is
    negligible.

    And even if it did reduce performance; I would rather
    have a program that runs at 50% speed but is guaranteed to
    an extremely high percentage to never make a mistake
    in calculation because it might at some point use an unintialized
    garbage value from an array or vector, than have one that runs
    at 100% speed but the results might be incorrect at some point
    because there is the possibility it might use an uninitialized element
    from an array or vector.
    Last edited by dcjr84; May 10th, 2006 at 01:45 PM.
    Please rate my post if you felt it was helpful

  8. #38
    Join Date
    Aug 2005
    Location
    Netherlands, The
    Posts
    2,184

    Re: array vs vector

    Quote Originally Posted by RoboTact
    dcjr84
    I disagree with your reasoning. Having initialized values is important, but not because it allows sloppy code to work. In your case you better should have array initialized to something like minimum possible value of integer to show error right when it occurs. Having consistent result guarantees repeatability of error behaviour.
    yes i very agree. but in my opinion this is a user fault not an array object fault.

  9. #39
    Join Date
    Jun 2002
    Location
    Moscow, Russia.
    Posts
    2,176

    Re: array vs vector

    Quote Originally Posted by dcjr84
    Vectors, and especially arrays, should always be
    initialized. period.
    More generally, nondetermenistic behaviour shouldn't be exposed. If said uninitialized value lurks shot within object and isn't visible outside it, fine, as far as object itself works right.
    "Programs must be written for people to read, and only incidentally for machines to execute."

  10. #40
    Join Date
    Aug 2005
    Location
    Netherlands, The
    Posts
    2,184

    Re: array vs vector

    Quote Originally Posted by dcjr84
    And even if it did reduce performance; I would rather
    have a program that runs at 50% speed but is guaranteed to
    an extremely high percentage to never make a mistake
    in calculation, than have one that runs at 100% speed but
    the results might be incorrect at some point because there
    is the possible it might use an uninitialized element from an
    array or vector.
    This is an programmers fault in this case. And will not happen in good desgined software. Doesnt mather if it is a vector or not having 0's is still garbage and will get wrong calculations if software is not rightly designed. Besides arrays are almost never used for calculations, but mostly for storing objects for games. POD's for images, files etc.
    Using vector will only decrease performance, and if this is not visible, the cpu cycles will waiste energy, and decrease computer's life time.

  11. #41
    Join Date
    Jun 2002
    Location
    Moscow, Russia.
    Posts
    2,176

    Re: array vs vector

    Quote Originally Posted by Mitsukai
    yes i very agree. but in my opinion this is a user fault not an array object fault.
    There is no fault in question, is there?
    "Programs must be written for people to read, and only incidentally for machines to execute."

  12. #42
    Join Date
    Jun 2002
    Location
    Moscow, Russia.
    Posts
    2,176

    Re: array vs vector

    Quote Originally Posted by Mitsukai
    Besides arrays are almost never used for calculations, but mostly for storing objects for games. POD's for images, files etc.
    Using vector will only decrease performance, and if this is not visible, the cpu cycles will waiste energy, and decrease computer's life time.

    You should reconsider level of conviction in such assertions.
    "Programs must be written for people to read, and only incidentally for machines to execute."

  13. #43
    Join Date
    Oct 2002
    Location
    Tx, US
    Posts
    208

    Re: array vs vector

    I guess i late in game now
    The sicussion went somewhere else than original post. though first i agree with Paul we need to find out how these timing taken.

    >>yes i very agree. but in my opinion this is a user fault not an array object fault.

    The evolution of all programming language is based on easy to use & hard to make mistkes by "user". There no need to use C++. We can do all things in C & assembly. Lets imagine handling memory managment in C for all the objects than using C'tor & D'tor. "user" is more likely to make the mistakes in C. In think there is no harm in providing automatic initilation to some value rather than having uninitiliaze. I bet for most of the application the overhead is very very less.

    Vinod

  14. #44
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,470

    Re: array vs vector

    Quote Originally Posted by aewarnick
    There is absolutely no need for vector to initialize elements to 0. Now I'm REALLY glad I created and use my own Array class.
    But, if you use vector::reserve, then it does create an uninitialised array. It's just that you can't randomly access the values until you put something in it, and then only the values that you've added. This is a good thing. Plus, as has already been pointed out to you, your "vector replacement" is seriously flawed.
    Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
    --
    Sutter and Alexandrescu, C++ Coding Standards

    Programs must be written for people to read, and only incidentally for machines to execute.

    --
    Harold Abelson and Gerald Jay Sussman

    The cheapest, fastest and most reliable components of a computer system are those that aren't there.
    -- Gordon Bell


  15. #45
    Join Date
    Sep 2005
    Location
    United States
    Posts
    799

    Re: array vs vector

    Using vector will only decrease performance, and if this is not visible, the cpu cycles will waiste energy, and decrease computer's life time.
    Again, this is negligible. Hundreds if not thousands of cpu cycles are
    probably wasted every minute of every day by your computer for a
    multitude of reasons: bad CPU scheduler decisions, etc, etc. I highly
    doubt that using a vector is going to decrease your computers lifetime
    by so much that it would even be noticeable.
    Please rate my post if you felt it was helpful

Page 3 of 7 FirstFirst 123456 ... LastLast

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

Featured