C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

How to delete an array?

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
felixnielsen@hotmail.com
Guest





PostPosted: Sun Jan 22, 2006 5:40 pm    Post subject: How to delete an array? Reply with quote



Hi, im pretty new to this so plz cut me some slag ;-)

anyhow, i need to delete an array, or what i really need is to increase
the array size, i was think i could be done by copying the array into a
temorary array of the same size, then delete the original, initiate the
old array again, but now with more spaces/cell or what u call it, then
copy the teporary array into the on just created and last delete the
temp array.
a bit complicated, but im told its the only way to do it...

Back to top
TB
Guest





PostPosted: Sun Jan 22, 2006 5:55 pm    Post subject: Re: How to delete an array? Reply with quote



[email]felixnielsen (AT) hotmail (DOT) com[/email] sade:
Quote:
Hi, im pretty new to this so plz cut me some slag ;-)

anyhow, i need to delete an array, or what i really need is to increase
the array size, i was think i could be done by copying the array into a
temorary array of the same size, then delete the original, initiate the
old array again, but now with more spaces/cell or what u call it, then
copy the teporary array into the on just created and last delete the
temp array.
a bit complicated, but im told its the only way to do it...


Here you go:

int main(int argc, char* argv[])
{
// The array which to extend
int * iptr = new int[5];

// Allocate new array
int * temp = new int[8];
// Copy iptr to nptr
for(unsigned i = 0; i < 5; ++i) {
temp[i] = iptr[i];
}
// Delete old array
delete[] iptr;
// Repoint iptr to new array
iptr = temp;
temp = 0;

// Delete array
delete[] iptr;
return 0;
}

Or just use std::vector instead and you won't have to deal with the above.

--
TB @ SWEDEN

Back to top
Ralph
Guest





PostPosted: Sun Jan 22, 2006 6:04 pm    Post subject: Re: How to delete an array? Reply with quote




<felixnielsen (AT) hotmail (DOT) com> wrote

Quote:
Hi, im pretty new to this so plz cut me some slag ;-)

anyhow, i need to delete an array, or what i really need is to increase
the array size, i was think i could be done by copying the array into a
temorary array of the same size, then delete the original, initiate the
old array again, but now with more spaces/cell or what u call it, then
copy the teporary array into the on just created and last delete the
temp array.
a bit complicated, but im told its the only way to do it...


Take a look at the Standard C++ library -
vector::resize()

There are reasons C++ provides sequence containers.

-ralph



Back to top
felixnielsen@hotmail.com
Guest





PostPosted: Sun Jan 22, 2006 6:36 pm    Post subject: Re: How to delete an array? Reply with quote

is i wrote, im pwer new to this and i must admit that i dont have a
complete understanding of all the above exprissions, howefer thanks for
the answer ill take a look at it and return in case i cant get it to
work.

Back to top
Mike Wahler
Guest





PostPosted: Sun Jan 22, 2006 7:23 pm    Post subject: Re: How to delete an array? Reply with quote


<felixnielsen (AT) hotmail (DOT) com> wrote

Quote:
Hi, im pretty new to this so plz cut me some slag ;-)

anyhow, i need to delete an array, or what i really need is to increase
the array size, i was think i could be done by copying the array into a
temorary array of the same size, then delete the original, initiate the
old array again, but now with more spaces/cell or what u call it, then
copy the teporary array into the on just created and last delete the
temp array.
a bit complicated, but im told its the only way to do it...

Eliminate complications by not using an array, use
a std::vector instead. It can grow and shrink in
size automatically, relieving you of all memory
management issues.

-Mike



Back to top
felixnielsen@hotmail.com
Guest





PostPosted: Sun Jan 22, 2006 7:34 pm    Post subject: Re: How to delete an array? Reply with quote

yes, very neat, i just read a little guide about it, really very simple
endless theres something i have missunderstod.

there is one thing though.
With arrays you can assign a value to af "cell" that doesnt excist, not
very smart i know, however you can do the same with vector i found out,
is there any complications i should know about?

Back to top
Mike Wahler
Guest





PostPosted: Sun Jan 22, 2006 7:50 pm    Post subject: Re: How to delete an array? Reply with quote


<felixnielsen (AT) hotmail (DOT) com> wrote

Quote:
yes, very neat, i just read a little guide about it, really very simple
endless theres something i have missunderstod.

there is one thing though.
With arrays you can assign a value to af "cell" that doesnt excist,

You can indeed write syntactically correct code to do this,
but its behavior is undefined (i.e. anything might happen,
from 'seems to work', to a crash, or anything else.)

Quote:
not
very smart i know,

Nope.

Quote:
however you can do the same with vector i found out,

No you cannot. Undefined behavior will also be the result.
If you want to put additional elements in a vector,
use its 'insert' or 'push_back' member functions (or
'resize()', which (if the specified size is greater than
existing size), will append the necessary number of default-
initialized elements to bring the size to that specified..

Quote:
is there any complications i should know about?

C++ is rather complicated. I suggest the best way
to deal with that is to get some good books. See
www.accu.org for suggestions.

-Mike



Back to top
felixnielsen@hotmail.com
Guest





PostPosted: Sun Jan 22, 2006 9:50 pm    Post subject: Re: How to delete an array? Reply with quote

well, i got it working, however i am having trouble using vectors in
functions, probably because the vector is declaret the wrong place or
something like that, but is it at all posible to declare global vectors?

Back to top
Gavin Deane
Guest





PostPosted: Sun Jan 22, 2006 10:09 pm    Post subject: Re: How to delete an array? Reply with quote


[email]felixnielsen (AT) hotmail (DOT) com[/email] wrote:
Quote:
well, i got it working, however i am having trouble using vectors in
functions, probably because the vector is declaret the wrong place or
something like that, but is it at all posible to declare global vectors?

It is possible, but it's not good design to use global variables.
Passing vectors to functions is not difficult and is a much better
idea. You've obviously had a go at writing the code but you're having
problems. Post the code here and people will be able to show you what's
wrong. Note these guidelines for posting code:

http://www.parashift.com/c++-faq-lite/how-to-post.html#faq-5.8

If you follow the advice there, so that people can copy and paste
directly from your message into their compiler, you should get the help
you need.

Gavin Deane


Back to top
Luke Meyers
Guest





PostPosted: Sun Jan 22, 2006 10:17 pm    Post subject: Re: How to delete an array? Reply with quote

[email]felixnielsen (AT) hotmail (DOT) com[/email] wrote:
Quote:
well, i got it working, however i am having trouble using vectors in
functions, probably because the vector is declaret the wrong place or
something like that, but is it at all posible to declare global vectors?

Yes, but don't do that. The Right Way to make a variable (such as a
vector) visible to a function is to pass it in (by value, or by
reference -- learn the difference, it's important!) as an argument.
Or, where appropriate to the design, the vector could be member data,
and the function a member function, of some class which encapsulates
the logical entity whose behavior and state are being represented.
Member functions automatically have access to member data. It's nifty
like that.

Don't be afraid to post code if you get confused. The STL is freakin'
rad and all, but as with any powerful tool (e.g. tools involving sharp
metal moving at high speed), there is a learning curve, and some
guidance is generally advisable.

Luke


Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.