Sign in to follow this  
Aileen99

re-size method

Recommended Posts

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

Share this post


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

If you have:
[code]int array[10];
[/code]
You can't resize the array. It's fixed.

If you have:
[code]int* array = new int[10];
[/code]
It can be resized by doing:
[code]delete [] array;
array = new int[100];
[/code]

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

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
.[CODE]
void resize(){
int *ptr = new int[size+1];
for(int i=0;i<size;i++)
ptr[i]=Array[i];
size++;
Array = ptr;
delete[]ptr; }
[/CODE]

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:
[code]Array = ptr;
delete [] ptr;[/code]
? 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
[left][font="arial, sans-serif"][size="3"][color="#4d90f0"]actually this what I'm taking in my collage...[/color][/size][/font][color=#4D90F0][font=arial, sans-serif][size=1] [/size][/font][/color][font="arial, sans-serif"][size="3"][color="#4d90f0"][img]http://public.gamedev.net//public/style_emoticons/default/wink.png[/img][/color][/size][/font][/left]

Share this post


Link to post
Share on other sites
As Cornstalks already said, please use the default font, and at least [i]attempt[/i] 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, [i]college[/i].

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this