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; well this is the code i got going: Code: string *temparray = new string[max_size*2]; for(int i = 0; i < ...

  1. #31
    Registered User
    Join Date
    Apr 2008
    Posts
    20
    well this is the code i got going:
    Code:
    string *temparray = new string[max_size*2];
    for(int i = 0; i < max_size; i++
    {
    temparray[i] = sentence[i];
    }
    delete[] sentence;
    sentence = temparray;
    delete[] temparray;
    max_size = max_size*2;

  2. #32
    Registered User
    Join Date
    Apr 2008
    Posts
    20
    the code is set to a function so it can be called more than once.

  3. #33
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    So, why delete[] temparray? You still need it, after all, since that's where everything is. Once you do sentence = temparray, the two are forever joined (that is, they currently point at the same piece of memory) and so deleting one will in fact delete both.

  4. #34
    Registered User
    Join Date
    Apr 2008
    Posts
    20
    well the function is part of a class and sentence is a private data

  5. #35
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,662
    Yes, tabstop's point is that:
    Code:
    delete[] sentence;
    sentence = temparray;
    delete[] temparray;
    is equivalent to:
    Code:
    delete[] sentence;
    sentence = temparray;
    delete[] sentence;
    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

  6. #36
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by dotz02x View Post
    well the function is part of a class and sentence is a private data
    You say that as though that is in any way relevant.

  7. #37
    Registered User
    Join Date
    Apr 2008
    Posts
    20
    but dont i have to deallocate all memory that has been allocated? or can i do that at the end of my program?

  8. #38
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    You are misunderstand though.

    Example
    Code:
    int *x, *y;
    
    x = new int[5];
    y = x;
    
    delete[] y;
    Does that illustrate tabstop's point more clearly? A pointer is referring to data at a given address. Think of it as if I told you where the mall is. Then I get hit by a bus. The knowledge of where the mall exists still exists through you regardless of my state.

    So as seen above, deleting y also deletes x.

  9. #39
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,662
    but dont i have to deallocate all memory that has been allocated? or can i do that at the end of my program?
    You would have a final delete[] sentence in the destructor.
    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

  10. #40
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Quote Originally Posted by dotz02x View Post
    but dont i have to deallocate all memory that has been allocated? or can i do that at the end of my program?
    At some point, yes. This should be a function that returns temparray.

  11. #41
    Registered User
    Join Date
    Apr 2008
    Posts
    20
    well okay i figured out that problem but... i have another problem and that is now my sentence array contains no data because the function is in the private section of a class.

    If i copy the array over instead of setting sentence to point to temparray will this work?

  12. #42
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,662
    i have another problem and that is now my sentence array contains no data because the function is in the private section of a class.
    You can use private member functions from within any member function of the class, so I am not sure what you are getting at.
    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. #43
    Registered User
    Join Date
    Apr 2008
    Posts
    20
    when i try to cout << sentence[i]; in a loop to output each of the words for some reason it displays nothing now....

  14. #44
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    And where do you do that? Since sentence is private, it can _only_ be accessed by member functions. If you are in a member function, then post your code so we have some idea what you're talking about.

  15. #45

    Join Date
    Apr 2008
    Location
    USA
    Posts
    76
    Attempting to access private data outside of a class definition would produce a compile error.

Page 3 of 6 FirstFirst 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