Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > renew() an array?

Reply
Thread Tools

renew() an array?

 
 
pradeep
Guest
Posts: n/a
 
      04-25-2008
Hello friends:

Say I allocate an array with
a = new int(100);

Later I want to grow the array to size 200. Do I need to allocate
another array and copy the first one to it? Is there a simpler approach
(like realloc() - my compiler doesn't recognize renew() as a function).

Thanks.
 
Reply With Quote
 
 
 
 
Andrey Tarasevich
Guest
Posts: n/a
 
      04-25-2008
pradeep wrote:
> Say I allocate an array with
> a = new int(100);


That does not allocate an array. This does

a = new int[100];

> Later I want to grow the array to size 200. Do I need to allocate
> another array and copy the first one to it?


Well, yes, as long as you are insisting on using 'new' to allocate arrays.

> Is there a simpler approach
> (like realloc() - my compiler doesn't recognize renew() as a function).


The simpler approach would be to use 'std::vector' and leave the details
to the implementation.

--
Best regards,
Andrey Tarasevich
 
Reply With Quote
 
 
 
 
Antoninus Twink
Guest
Posts: n/a
 
      04-25-2008
On 25 Apr 2008 at 22:15, pradeep wrote:
> Hello friends:
>
> Say I allocate an array with
> a = new int(100);


I think you want square brackets.

> Later I want to grow the array to size 200. Do I need to allocate
> another array and copy the first one to it? Is there a simpler approach
> (like realloc() - my compiler doesn't recognize renew() as a function).


No.

Why not use std::vector, which provides dynamic resizing via resize(),
insert(), and the like?

 
Reply With Quote
 
santosh
Guest
Posts: n/a
 
      04-25-2008
pradeep wrote:

> Hello friends:
>
> Say I allocate an array with
> a = new int(100);
>
> Later I want to grow the array to size 200. Do I need to allocate
> another array and copy the first one to it? Is there a simpler
> approach (like realloc() - my compiler doesn't recognize renew() as a
> function).


This is a C++ question as far as I can see. Try comp.lang.c++ instead of
comp.lang.c.

 
Reply With Quote
 
Walter Roberson
Guest
Posts: n/a
 
      04-25-2008
In article <futlbk$sb3$>,
Andrey Tarasevich <> wrote:
>pradeep wrote:
>> Say I allocate an array with
>> a = new int(100);


>That does not allocate an array. This does


> a = new int[100];


This is comp.lang.c . The above is a syntax error in C.

To the original poster: 'new' is not part of C. Please consult
a newsgroup for whatever programming language you are using.
For example, if you are using C++ then comp.lang.c++ would be appropriate.
--
"The first draught serveth for health, the second for pleasure,
the third for shame, the fourth for madness." -- Sir Walter Raleigh
 
Reply With Quote
 
Martin Ambuhl
Guest
Posts: n/a
 
      04-26-2008
pradeep wrote:
> Hello friends:
>
> Say I allocate an array with
> a = new int(100);


Then you want a language other than C. As that line stands, it is a
syntax error.
>
> Later I want to grow the array to size 200.


Even in (shudder) the bloat-de-jure C++ language, that's the wrong way
to do it. Consider using the <off-topic> vector class </off-topic>.

> Do I need to allocate
> another array and copy the first one to it? Is there a simpler approach
> (like realloc() - my compiler doesn't recognize renew() as a function).


If it were a C compiler, it would have vomited at the earlier line of code,
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      04-26-2008
pradeep <> writes:
> Say I allocate an array with
> a = new int(100);
>
> Later I want to grow the array to size 200. Do I need to allocate
> another array and copy the first one to it? Is there a simpler
> approach (like realloc() - my compiler doesn't recognize renew() as a
> function).


This is a C++ question. It's also a frequently asked C++ question.
You'll likely find all the information you need in the C++ FAQ at
<http://www.parashift.com/c++-faq-lite/>, particularly section 16. If
you have further questions, please post to comp.lang.c++.

--
Keith Thompson (The_Other_Keith) <kst->
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
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




Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57