Thread: Making the size of an array variable

  1. #31
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Ask your question in its own thread, with code, example input and expected output.

  2. #32
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    Quote Originally Posted by ChaosEngine
    The reason C++ has a bad reputation for productivity is that developers spend too much time worry about stupid cr@p like matching new and deletes...
    There is no place in modern C++ where a dynamically allocated array is a better solution then a container.
    am I the only one that sees this as the equivalent of "Why learn long division when we have calculators?"
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  3. #33
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    am I the only one that sees this as the equivalent of "Why learn long division when we have calculators?"
    Learn long division and how to use calculators, but use calculators (unless you need long division, or can do the calculation mentally or on paper).
    Learn dynamic arrays with new[]/delete[] and how to use vectors and other containers, but use vectors and other containers (unless you need dynamic arrays due to some exceptional reason).

    I think the reason why it is good to learn both is that abstractions leak, but as long they dont, it makes more sense to use the abstraction.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #34
    semi-colon generator ChaosEngine's Avatar
    Join Date
    Sep 2005
    Location
    Chch, NZ
    Posts
    597
    Quote Originally Posted by major_small
    am I the only one that sees this as the equivalent of "Why learn long division when we have calculators?"
    Work out the following to as many decimal places as you can:

    123.45/234.567
    645647.654/4353

    Are you seriously telling me you're going to sit down and work them out by hand or are you going to use the tools available to you?

    Of course you should learn and understand long division, but to suggest that you shouldn't automate it is just plain stupid.

    We're computer programmers!!! The whole point of writing computer programs is to automate tasks, that are boring/error-prone/slow. Yet for some reason C/C++ programmers have this insane irrational fear that anything in a library must be inefficient. This is why people who write in other languages laugh at C/C++ programmers. This is why a script kiddie writing in c# can produce code faster than a typical C++ programmer with 3 or 4 years experience.

    If you are writing code professionally, you better have a damn good reason for using a dynamic array over a vector, otherwise you are wasting your employers time/money.

    Sorry, but this issue really annoys me.
    "I saw a sign that said 'Drink Canada Dry', so I started"
    -- Brendan Behan

    Free Compiler: Visual C++ 2005 Express
    If you program in C++, you need Boost. You should also know how to use the Standard Library (STL). Want to make games? After reading this, I don't like WxWidgets anymore. Want to add some scripting to your App?

  5. #35
    Registered User
    Join Date
    Jun 2004
    Posts
    201
    About the only use for hand written arrays in C++ is when you have to communicate with older C libraries. for instance windows API

    char *text = new char[128];

    GetWindowText(hwnd, text, 128);

    delete[] text;

    other than that, best to use std::vector. The optimization argument can easily be wrong too these days, because inlining and the vector template fitting better into the C++ libraries can make vectors faster than C style arrays.

  6. #36
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Actually, you can and should use vector in that instance as well:
    Code:
    vector<char> text(128);
    GetWindowText(hwnd, &text[0], 128);
    // no delete necessary

  7. #37
    Registered User
    Join Date
    Jun 2004
    Posts
    201
    Quote Originally Posted by Daved
    Actually, you can and should use vector in that instance as well:
    Code:
    vector<char> text(128);
    GetWindowText(hwnd, &text[0], 128);
    // no delete necessary
    ow maybe you can give the definitive answer then. We had a discussion once if you could do this. It comes across kinda hackish because access to the internal mechanism of vector is given. But this would be perfectly safe?

    You couldnt do this with std::string for example because the length of the string wont necessarily be updated so you'd better do
    std::string text = buf; and then delete buf after the call to GetWindowText

  8. #38
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    This is one instance where the internal representation is guaranteed by the standard, and it is done just for this purpose. In the 1998 standard it was somewhat ambiguous, but they corrected that recently and all major standard library implementations follow the practice anyway. The vector must hold its contents in contiguous memory, so that &v[0] can be used for backwards compatibility with functions taking C style arrays.

    You couldn't do this with std::string because it's memory isn't guaranteed to be contiguous. The issue isn't with the length not being updated, because the length of the vector or the length of the character array aren't updated either. It is just that GetWindowText expects a contiguous array of modifiable characters that std::string cannot provide.

    I would use vector, and then assign &v[0] to the string (since that would be the first character of a null terminated string). Using a local buffer is also fine, but I wouldn't use new[]/delete[] in this instance since the dynamic allocation is unnecessary.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  2. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  3. Replies: 6
    Last Post: 11-09-2006, 03:28 AM
  4. help on variable size array
    By 1qaz1234 in forum C++ Programming
    Replies: 7
    Last Post: 02-23-2005, 12:02 PM
  5. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM