Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > new/ resize

Reply
Thread Tools

new/ resize

 
 
machine99
Guest
Posts: n/a
 
      10-20-2003
how do you resize an array allocated with new?


 
Reply With Quote
 
 
 
 
Rolf Magnus
Guest
Posts: n/a
 
      10-20-2003
machine99 wrote:

> how do you resize an array allocated with new?


You don't. Yo have to allocate a new block of memory of the target size
and then move the data there and deallocate the original array.
Any reason why you don't use std::vector, which handles resizing for
you?

 
Reply With Quote
 
 
 
 
machine99
Guest
Posts: n/a
 
      10-20-2003
> > how do you resize an array allocated with new?
>
> You don't. Yo have to allocate a new block of memory of the target size
> and then move the data there and deallocate the original array.
> Any reason why you don't use std::vector, which handles resizing for
> you?


I do use std::vector quite extensively but there will always be special
cases


 
Reply With Quote
 
Thomas Matthews
Guest
Posts: n/a
 
      10-20-2003
machine99 wrote:
>>>how do you resize an array allocated with new?

>>
>>You don't. Yo have to allocate a new block of memory of the target size
>>and then move the data there and deallocate the original array.
>>Any reason why you don't use std::vector, which handles resizing for
>>you?

>
>
> I do use std::vector quite extensively but there will always be special
> cases


Actually, the situations where an array is preferred or required
over a std::vector are few and don't require resizing.

Some of these situations are code / memory space and execution time.
However, time spent developing (and development cost) usually
outweighs any premature optimization issues and all problems
involved with an array when a std::vector should have been used.

The programming priorities (listed with highest priority first):
1. Quality & robustness
2. Development time (also related to cost).
3. User interaction or interaction with the environment.
4. Code space and execution time (i.e. optimization).
If a program can't correctly turn a motor, turning it faster
or using less code to turn it is a mute point.

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book
http://www.sgi.com/tech/stl -- Standard Template Library

 
Reply With Quote
 
Thomas Matthews
Guest
Posts: n/a
 
      10-20-2003
Thomas Matthews wrote:
> machine99 wrote:
>
>>>> how do you resize an array allocated with new?
>>>
>>>
>>> You don't. Yo have to allocate a new block of memory of the target size
>>> and then move the data there and deallocate the original array.
>>> Any reason why you don't use std::vector, which handles resizing for
>>> you?

>>
>>
>>
>> I do use std::vector quite extensively but there will always be special
>> cases

>
>
> Actually, the situations where an array is preferred or required
> over a std::vector are few and don't require resizing.
>
> Some of these situations are code / memory space and execution time.
> However, time spent developing (and development cost) usually
> outweighs any premature optimization issues and all problems
> involved with an array when a std::vector should have been used.
>
> The programming priorities (listed with highest priority first):
> 1. Quality & robustness
> 2. Development time (also related to cost).
> 3. User interaction or interaction with the environment.
> 4. Code space and execution time (i.e. optimization).
> If a program can't correctly turn a motor, turning it faster
> or using less code to turn it is a mute point.
>


Oops, that should be "moot point".

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book
http://www.sgi.com/tech/stl -- Standard Template Library

 
Reply With Quote
 
Howard
Guest
Posts: n/a
 
      10-20-2003

"Thomas Matthews" <(E-Mail Removed)> wrote in
message news:(E-Mail Removed)...
> machine99 wrote:
> >>>how do you resize an array allocated with new?
> >>
> >>You don't. Yo have to allocate a new block of memory of the target size
> >>and then move the data there and deallocate the original array.
> >>Any reason why you don't use std::vector, which handles resizing for
> >>you?

> >
> >
> > I do use std::vector quite extensively but there will always be special
> > cases

>
> Actually, the situations where an array is preferred or required
> over a std::vector are few and don't require resizing.
>


Why does the need to resize an array mean that it is no longer neccessary to
use an array instead of a vector?

> Some of these situations are code / memory space and execution time.
> However, time spent developing (and development cost) usually
> outweighs any premature optimization issues and all problems
> involved with an array when a std::vector should have been used.
>
> The programming priorities (listed with highest priority first):
> 1. Quality & robustness
> 2. Development time (also related to cost).
> 3. User interaction or interaction with the environment.
> 4. Code space and execution time (i.e. optimization).
> If a program can't correctly turn a motor, turning it faster
> or using less code to turn it is a mute point.
>
> --


You're missing one of the most important priorities for many of us:
compatibility with existing code. We don't all get to choose how we would
write code. Much of it is forced upon us by what was written before, (or by
what our operating system or third-party API's require).

-Howard



 
Reply With Quote
 
Andreas =?ISO-8859-1?Q?M=FCller?=
Guest
Posts: n/a
 
      10-20-2003
"Howard" wrote:

>
> "Thomas Matthews" <(E-Mail Removed)>
> wrote in message news:(E-Mail Removed)...
> > machine99 wrote:

[snip]
> You're missing one of the most important priorities for many of us:
> compatibility with existing code. We don't all get to choose how we
> would write code. Much of it is forced upon us by what was written
> before, (or by what our operating system or third-party API's
> require).
>
> -Howard
>
>

When it comes to a point where compatibility to arrays comes into the
game, I usually stick with std::vector, as I can easily pass the content
of a vector into a legacy function (&vector[0]) and converting a
returned array into a vector normally doesn't affect performance in the
stuff that I do. (If it does and it was proven by a profiler, then that
is another story!)
--
To mail me directly, remove the NO*SPAM parts in
NO*(E-Mail Removed)*SPAM
 
Reply With Quote
 
Thomas Matthews
Guest
Posts: n/a
 
      10-20-2003
Howard wrote:

> "Thomas Matthews" <(E-Mail Removed)> wrote in
> message news:(E-Mail Removed)...
>
> Why does the need to resize an array mean that it is no longer neccessary to
> use an array instead of a vector?


IMHO, the primary difference between a vector and an array is the
resizing issue. By the C++ definition, an array is a static structure
in that its size doesn't change. If the array size changes, then a
vector is a better choice (since the code already exists for
resizing and has been tested). There are other qualities of a
vector, such as bounds checking, that are useful during the
development stage.

>
>
>>Some of these situations are code / memory space and execution time.
>>However, time spent developing (and development cost) usually
>>outweighs any premature optimization issues and all problems
>>involved with an array when a std::vector should have been used.
>>
>>The programming priorities (listed with highest priority first):
>> 1. Quality & robustness
>> 2. Development time (also related to cost).
>> 3. User interaction or interaction with the environment.
>> 4. Code space and execution time (i.e. optimization).
>>If a program can't correctly turn a motor, turning it faster
>>or using less code to turn it is a mute point.
>>
>>--

>
>
> You're missing one of the most important priorities for many of us:
> compatibility with existing code. We don't all get to choose how we would
> write code. Much of it is forced upon us by what was written before, (or by
> what our operating system or third-party API's require).
>
> -Howard


I still believe that quality and robustness is top priority especially
when maintaining legacy systems. I know from personal experience that
small changes can cause an existing system to blow up (primarily due
to undocumented dependencies).

Don't get me wrong, compatibility is an issue. A correct modification
that takes more time to develop is better than a quick and dirty fix
that isn't 100% correct. Also, if the new code is not compatible
with the existing code, it is worthless. I would place compatibility
under quality and robustness.

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book

 
Reply With Quote
 
machine99
Guest
Posts: n/a
 
      10-21-2003
How does std::vector resize then?

Perhaps I could access its memory functions/classes/whatever and use them
for my own purpose?


 
Reply With Quote
 
Christopher Benson-Manica
Guest
Posts: n/a
 
      10-21-2003
Rolf Magnus <(E-Mail Removed)> spoke thus:

> You don't. Yo have to allocate a new block of memory of the target size
> and then move the data there and deallocate the original array.
> Any reason why you don't use std::vector, which handles resizing for
> you?


Just for curiosity's sake, why isn't there something like realloc() for new?
Wouldn't that make situations like this one easier to manage?

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
FF extension to resize tabs? Teh Suck Firefox 2 12-28-2005 04:20 PM
How to resize all images sizes and coordinates of the images on resize browser rams.kakara@gmail.com ASP General 2 02-13-2005 09:03 AM
Phantom space and resize problem. Rod Billett ASP .Net 0 10-24-2003 08:35 PM
IMAGE RESIZE Alexandre Martins ASP .Net 0 08-13-2003 02:10 PM
RE: Web user control content doesn't resize in web form table Eldon Lei [MSFT] ASP .Net 0 07-09-2003 11:49 PM



Advertisments