Thread: User input for array size.

  1. #16
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Because you also have to allocate memory for whatever variables a vector has internally.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  2. #17
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246
    It doesn't make program that slow.

    [edit]
    Does it? How many variables it allocates for each push_back()?
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  3. #18
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Everything depends on your usage, but vector is not generally slower by any significant amount than a dynamic array.

  4. #19
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    It doesn't make program that slow.
    I agree with you. I'm just suggesting reasons for the cannon statement.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #20
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    There are very few.

    If the code is too complex, vectors should be used to ease coding, debugging and maintenance.

    If the code is too simple, vectors should be used because they are still simpler than an array to implement and we already have a head start in case we need to upgrade the code.

    Arrays should be used on the internals of complex classes or hierarchies where the speed factor is critical. The differences in speed are minimal... So only when minimal has a real meaning, should they be used. And needless to say if we need some of the facilities provided by the algorithms, we have to at least be as good programmers as the library implementators to be able to code them with arrays in mind.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  6. #21
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    While I agree with you for the most part, the standard algorithms generally work with arrays, too.

  7. #22
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246
    I am comparing dynamically allocated memory to vectors. Static arrays are faster of course.
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  8. #23
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    A vector cannot be (if it is, the library implementator is deaf) implemented just to give access to its elements. It must do it in Constant Time ( O(1) ). Any performance hit can only be the result of the constraints derived from the fact a vector is indeed implemented on top of an array.

    But because it is still implemented in constant time and because the hit is very small, only with big amounts of data or with the need to traverse its contents repeatedly an incredible number of times can it become noticeable.

    And then probably the difference in time on my computer is not asnearly significant on yours, if your processor is just a nudge as faster than mine.

    In the end, really... no. They have the same speed. They run both at constant time

    EDIT: And you are right Daved. Forgot about the fact many of the algorithms can work with arrays.
    Last edited by Mario F.; 08-24-2006 at 06:01 PM.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  9. #24
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    Quote Originally Posted by Mario F.
    Arrays should be used on the internals of complex classes or hierarchies where the speed factor is critical. The differences in speed are minimal... So only when minimal has a real meaning, should they be used. And needless to say if we need some of the facilities provided by the algorithms, we have to at least be as good programmers as the library implementators to be able to code them with arrays in mind.
    The only other thing I use arrays for is buffers and the like which are filled by API function calls. Then I immediately create a vector (or string) and delete[] the buffer.

    I'm nervous about letting an API call directly copy memory into a vector. I know the standard guarantees it should work without problems, though, at least for vector and string (but none of the other containers). I always considered it bad form, though.
    Last edited by Cat; 08-24-2006 at 06:59 PM.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  10. #25
    Registered User
    Join Date
    Aug 2006
    Posts
    4
    Wow.

    You guys really know your stuff.

    Before yesterday I didn't even know what a pointer was, or a vector, or any of the things you're talking about. I actually did browse through some of those while trying to find a way for this to work on my own. But I get it better now having read this, and I don't think I could have figured out how to use them without this board.



    The program compiles now. But I have a new problem.


    Our CS department uses Linux computers. The logins from last year have been erased and the administrator hasn't given us new ones yet, so I wrote my program on a Windows computer in the school library on something called 'VisualStudio.net'. When I try to run my program I get an error message saying the debugger for Visual Studio wasn't properly installed, even if I tell it to start the program without debugging.

    So I have a program which compiles but I don't know if it actually does what I want. There are about six computers with Visual Studio on them. I'll try a few of those and see if they work instead.




    Thanks for all the help, by the way.


    If I posted

  11. #26
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> I'm nervous about letting an API call directly copy memory into a vector. I know the standard guarantees it should work without problems, though, at least for vector and string (but none of the other containers).

    Only for vector, string has no such guarantee.

    >> So I have a program which compiles but I don't know if it actually does what I want.

    You can try Ctrl-F5 to execute the program, or you can open a command prompt, find your exe, and execute it from there.

  12. #27
    Registered User
    Join Date
    May 2006
    Posts
    903
    Or compile in release mode to prevent it from inserting debug symbols in your app.

  13. #28
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    Quote Originally Posted by Daved
    Only for vector, string has no such guarantee.
    Huh. Guess I learned something new today.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 12
    Last Post: 04-12-2009, 05:49 PM
  2. timed user input
    By sainiabhishek in forum C Programming
    Replies: 4
    Last Post: 04-01-2009, 11:59 AM
  3. Truncating user input
    By CS_Student8337 in forum C Programming
    Replies: 10
    Last Post: 03-19-2009, 12:34 AM
  4. Replies: 4
    Last Post: 04-21-2004, 04:18 PM
  5. problem: reading user input into an array
    By alpha561 in forum C Programming
    Replies: 13
    Last Post: 05-24-2002, 07:23 PM