renew() an array?

P

pradeep

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.
 
A

Andrey Tarasevich

pradeep said:
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.
 
A

Antoninus Twink

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?
 
S

santosh

pradeep said:
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.
 
W

Walter Roberson

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.
 
M

Martin Ambuhl

pradeep said:
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 said:
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,
 
K

Keith Thompson

pradeep said:
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++.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top