how to double an array size?

This is a discussion on how to double an array size? within the C++ Programming forums, part of the General Programming Boards category; I was wondering how do i increase an array size. Example: string *array; array = new string[10]; and I want ...

  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
    21,922
    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?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    I was wondering how do i increase an array size
    use std::vector that does the work for you
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  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
    21,922
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    21,922
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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.

Page 1 of 6 123456 LastLast
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, 03:09 AM
  2. Replies: 16
    Last Post: 11-23-2007, 12:48 PM
  3. need some help with last part of arrays
    By Lince in forum C Programming
    Replies: 3
    Last Post: 11-18-2006, 08:13 AM
  4. Unknown Math Issues.
    By Sir Andus in forum C++ Programming
    Replies: 1
    Last Post: 03-06-2006, 05:54 PM
  5. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21