Thread: how to double an array size?

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    20

    how to double an array size?

    I was wondering how do i increase an array size.

    Example:

    string *array;
    array = new string[10];

    and I want to increase the elements in that array to 20 how can i go by attacking this problem?
    I want to only use arrays and not vectors so could someone please help me out thanks.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    If no one here answered your question, what would you do?

    What I'm trying to do is teach you to think for yourself.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    20
    well, the only idea that came to my head is to reinitialize the array.

    array = new string[20];
    but i am not sure if that works.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    well, the only idea that came to my head is to reinitialize the array.
    Elaborate on your idea, you sound like you are on the right track (for a simple/naive but workable implementation, anyway).

    array = new string[20];
    but i am not sure if that works.
    Ah, that makes array point to a new dynamically allocated array of double the size... but what happens to the elements that were previously stored?
    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

  5. #5
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    I was wondering how do i increase an array size
    use std::vector that does the work for you
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  6. #6
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    You could also use the placement new operator and the C memory management functions, but vectors are a wise choice. I think you'd be amazed at how much info you can find about these types of questions by simply googling (and I say that not to be condescending so much as for your benefit of not having to read snide remarks about what you are asking not to mention the length of time that sometimes lapses between asking and a response).

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by vart View Post
    use std::vector that does the work for you
    Except the original post says "do not wish to use vector". We'll assume this is for example because it's an assignment or such.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by master5001 View Post
    You could also use the placement new operator and the C memory management functions, but vectors are a wise choice. I think you'd be amazed at how much info you can find about these types of questions by simply googling (and I say that not to be condescending so much as for your benefit of not having to read snide remarks about what you are asking not to mention the length of time that sometimes lapses between asking and a response).
    Whilst that is a clever solution, I'm 99% sure that's not really the solution the original poster was looking for.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #9
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    In that case I will allow the original poster an opportunity to ponder the one and only solution left that the instructor is looking for them to be implementing. Though if its an assignment, surely the placement new operator trick would impress the teacher.

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by master5001 View Post
    Though if its an assignment, surely the placement new operator trick would impress the teacher.
    Or, knowing the quality of some teachers, confuse the teacher.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  11. #11
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Hahaha! No kidding!

    F Assignment
    Code:
    char buffer[02000];  // 1k buffer for static storage
    char *ptr = new(buffer) char[50];
    Given zero credit due to free thinking and denoting one kilobyte in the most confusing notation possible.

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Given zero credit due to free thinking and denoting one kilobyte in the most confusing notation possible.
    Actually, when I saw your cute snippet, the first thing that came to mind was "not general enough", since the capacity is fixed, at least from what I understand. I also did not recall an example where the buffer was not a dynamic array... and this guy claims that Placement-New Requires Heap-Allocated Buffers, but personally I cannot parse the relevant text of the C++ Standard to determine either way.
    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

  13. #13
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Well I mostly trying to demonstrate an unusual method more than a realistic one. The *alloc() solution I pitched earlier is really more what the placement operator was designed for (in concept not in practice... so I refuse to reply to anyone who says "they were created for custom memory manager and/or separate heap allocation"). After writing that I realized how infrequently any of us actually use octal number strings.

  14. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Well I mostly trying to demonstrate an unusual method more than a realistic one.
    Fair enough. Still, do you agree with Danny Kalev, or is there some flaw in his reasoning?

    The *alloc() solution I pitched earlier is really more what the placement operator was designed for (in concept not in practice... so I refuse to reply to anyone who says "they were created for custom memory manager and/or separate heap allocation").
    Allocators and the like are still deep magic to me, but that is what I had in mind when I implied that there was a non-naive implementation to what dotz02x was starting to get at. My understanding is drawn from Allocating Arrays Using Placement new (also by Kalev, does that guy have a fetish with placement new or what?), which I once linked to someone who asked how std::vector might implement the constructor that takes an iterator pair or the constructor that takes a size and initial value.

    After writing that I realized how infrequently any of us actually use octal number strings.
    Or rather, numbers in octal representation. But yeah, I parse decimal and hexadecimal representations far more easily.
    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

  15. #15
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by laserlight View Post
    Actually, when I saw your cute snippet, the first thing that came to mind was "not general enough", since the capacity is fixed, at least from what I understand. I also did not recall an example where the buffer was not a dynamic array... and this guy claims that Placement-New Requires Heap-Allocated Buffers, but personally I cannot parse the relevant text of the C++ Standard to determine either way.
    Strictly speaking, I think that link is correct, but the requirement isn't that it needs to be heap allocated as such - it just needs to be aligned correctly - for a char array, that would no big deal, and as long as your "placement" array is based on an element that is of equal or larger size than the largest single element of whatever you are trying to "place" there, then it will be fine (so for example, placing a short on top of an int array should be fine, but an int on top of a short array won't necessarily). If you use a char array, and "oversize" by 1 alignment size and manually align it, it will also work just fine.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Testing some code, lots of errors...
    By Sparrowhawk in forum C Programming
    Replies: 48
    Last Post: 12-15-2008, 04:09 AM
  2. Replies: 16
    Last Post: 11-23-2007, 01:48 PM
  3. need some help with last part of arrays
    By Lince in forum C Programming
    Replies: 3
    Last Post: 11-18-2006, 09:13 AM
  4. Unknown Math Issues.
    By Sir Andus in forum C++ Programming
    Replies: 1
    Last Post: 03-06-2006, 06:54 PM
  5. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM