0
string *level_two; 

I got this array declared in the class. I allocated its size dynamically in a class function.

level_two = new string [size];

I need to change its size mainly increase the size retaining the contents, like if size was 5 and my array is declared dynamically as size of 5 and has some string at location 2 and location 3 I want to increase the size of it to 10 depending upon change in size where the strings at location 2 and 3 should be there too without change.

How can I achieve that?

PART II:

int temp=0;
                temp= final_hash_index_one; 

                final_hash_index_one = sum % number_of_keys_to_be_hashed;
                sum = 0;
                cout<<"final_hash_index_one: "<<final_hash_index_one<<endl;
                if(final_hash_index_one>temp)
                {
                    string *tmp = new string[final_hash_index_one+1];
                    std::copy(level_two, tmp, final_hash_index_one+1);
                    delete [] level_two;
                    level_two = tmp;
                }
                if(temp>final_hash_index_one)
                {
                    string *tmp = new string[temp+1];
                    std::copy(level_two, tmp, final_hash_index_one+1);
                    delete [] level_two;
                    level_two = tmp;
                }


                level_two = new string [final_hash_index_one+1];
                level_two[final_hash_index_one] = file_se_uthao;
                tab_obj[return_structure_index].z = level_two;    
3
  • what difference will it make ?? Commented Mar 27, 2012 at 11:09
  • @Mat who says it's not std::string? Besides, you do have to worry about memory allocation when using arrays of std::string. Commented Mar 27, 2012 at 11:36
  • With arrays, you can't increase the size, because arrays have to be stored in adjacent memory spaces, so you can only set its size at creation with new. As @Oli answered, you'd be better with std containers. Commented Mar 27, 2012 at 11:38

1 Answer 1

7

Canonical C++ answer:

Use a std::vector<string>1 rather than doing manual memory management (i.e. don't use new and delete). e.g.:

std::vector<std::string> level_two(size);

...

level_two[0] = some_string;

...

level_two.resize(different_size);

You can also avoid explicit sizing entirely:

std::vector<std::string> level_two;

...

level_two.push_back(some_string);
level_two.push_back(other_string);
level_two.push_back(another_string);
...

C-with-classes answer:

You will need to create a new array, transfer the contents, and then delete the old one:

string *tmp = new string[different_size];
std::copy(level_two, level_two+size, tmp);
delete [] level_two;
level_two = tmp;

1. Or a std::list or whatever else, depending on your requirements.

Sign up to request clarification or add additional context in comments.

14 Comments

@HarisRiaz: level_two is just a pointer. So level_two = tmp just changes where the pointer is pointing to.
@Haris No, the newly created array, tmp is already sized at creation. level_two = tmp just makes level_two point to the newly created array.
@HarisRiaz: Don't use realloc in C++; it's incompatible with new and delete, and it certainly doesn't play well with non-trivial user-defined types.
@HarisRiaz: There are no ints in my code above! Why not add your new code to your question, and I'll take a look.
@HarisRiaz: Ah, I see. It turns out the code snippet in my answer wasn't correct. I've updated it now (notice the arguments to std::copy have changed). For more info, see e.g. en.cppreference.com/w/cpp/algorithm/copy.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.