• Advertisement
Sign in to follow this  

re-size method

This topic is 2186 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

[font=comic sans ms,cursive]

Hi
how i can use [color=#000000] re-size method with Array in class??!
[color=#000000]anyone write a function plz..[/font]

Share this post


Link to post
Share on other sites
Advertisement
Depends on what you mean...

If you have:
int array[10];

You can't resize the array. It's fixed.

If you have:
int* array = new int[10];

It can be resized by doing:
delete [] array;
array = new int[100];


If you're using C++ containers (which I hope you are), you can use[font=courier new,courier,monospace] std::vector[/font]:
std::vector<int> v(10, 0); // creates a vector with 10 elements, with each element set to 0
And it can be resized by doing:
v.resize(100, 5); // resizes to 100, sets the *new* element to 5 (the original first 10 elements keep their value)

Just a note: try using the default font when you post. It's hard to take non-standard fonts seriously for me (personally). Also, you should post what you've tried and what hasn't worked, and try to describe what it is you're trying to do with greater clarity. Edited by Cornstalks

Share this post


Link to post
Share on other sites
thnx,,
I write the code can you check it Plz
.
void resize(){
int *ptr = new int[size+1];
for(int i=0;i<size;i++)
ptr=Array;
size++;
Array = ptr;
delete[]ptr; }

Share this post


Link to post
Share on other sites
I'll ask questions so you can tell me what you think is happening here:

What happens if [font=courier new,courier,monospace]Array [/font]has already been allocated here? That is, if [font=courier new,courier,monospace]Array [/font]has a chunk of memory allocated for it, what happens to that chunk of memory when you call that method? Does it get forgotten and leaked? Does it get cleaned up and deleted?

What happens when you say:
Array = ptr;
delete [] ptr;

? When you say [font=courier new,courier,monospace]Array = ptr;[/font] what is the computer actually doing? What happens to the memory for [font=courier new,courier,monospace]ptr [/font]when you say [font=courier new,courier,monospace]delete [] ptr[/font]? Does this affect [font=courier new,courier,monospace]Array[/font]'s memory?

I want you to seriously try to answer these questions (because either you will find the answer and learn something new, or you will find out what you don't know and then learn something new). You can learn a lot here from this small example.

Aside from the actual code you posted, it looks a lot like you're just re-writing [font=courier new,courier,monospace]std::vector[/font]. What exactly are you trying to accomplish here with this resize method? Why aren't you using [font=courier new,courier,monospace]std::vector[/font]?

Share this post


Link to post
Share on other sites

[font="arial, sans-serif"][size="3"][color="#4d90f0"]actually this what I'm taking in my collage...[/font][color=#4D90F0][font=arial, sans-serif][size=1] [/font][font="arial, sans-serif"][size="3"][color="#4d90f0"]wink.png[/font]

Share this post


Link to post
Share on other sites
As Cornstalks already said, please use the default font, and at least attempt to write professionally. It is difficult if not impossible to take you seriously if you are going to continue writing in such a style. Also, college.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement