grow an array with new

G

Guest

If I allocate an array with
-----
a = new int(10);
-----
and after I want grow the array to 20 ints
I must allocate a 2nd array and copy 1st array to 2nd?
There is no "smaller-code" approach? (Something like realloc)

I dont want long answer. I want "YES" or "NO" ;-)

Thanks
 
?

=?iso-8859-1?Q?Juli=E1n?= Albo

If I allocate an array with

You are allocating a single int with value 10.

You probaly mean: a= new int [10];
-----
and after I want grow the array to 20 ints
I must allocate a 2nd array and copy 1st array to 2nd?
Yes.

There is no "smaller-code" approach? (Something like realloc)

Use std::vector.

Regards.
 
R

Rob Williscroft

If I allocate an array with
-----
a = new int(10);
-----
and after I want grow the array to 20 ints
I must allocate a 2nd array and copy 1st array to 2nd?
There is no "smaller-code" approach? (Something like realloc)

I dont want long answer. I want "YES" or "NO" ;-)

NO

Rob.
 
S

Sumit Rajan

Rob said:


I'm curious: can't we do something like this:

#include <iostream>

int main()
{
int* n = new int[10];
for(int i=0; i<10;i++)
n = i;

int* m = n+10;
m = new int[10];
for(int i=10; i<20;i++)
n = i;
for (int i=0; i<20; i++)
std::cout << n << ' ';
std::cout << '\n';
}

This gave me the following as output:
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19

Regards,
Sumit.
 
M

Mario

NO. You initialized m to be n+10; but in the very next line you point m to
something else. When you call operator new, it will allocate the required
block of memory (if there is free memory) and assign the address of the
beginning of that block to m again. No one guarantees that the bock new
reserves will have the address that m pointed to before. It is simply by
"accident" (or how the new algorithm works) that the second invocation of
new allocated memory right after the first block it allocated.

Also, you have a "memory leak" in your program. You never delete [] either
block you allocated.






Sumit Rajan said:
Rob said:


I'm curious: can't we do something like this:

#include <iostream>

int main()
{
int* n = new int[10];
for(int i=0; i<10;i++)
n = i;

int* m = n+10;
m = new int[10];
for(int i=10; i<20;i++)
n = i;
for (int i=0; i<20; i++)
std::cout << n << ' ';
std::cout << '\n';
}

This gave me the following as output:
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19

Regards,
Sumit.
 
S

Sumit Rajan

Mario said:
NO. You initialized m to be n+10; but in the very next line you point m to
something else. When you call operator new, it will allocate the required
block of memory (if there is free memory) and assign the address of the
beginning of that block to m again. No one guarantees that the bock new
reserves will have the address that m pointed to before. It is simply by
"accident" (or how the new algorithm works) that the second invocation of
new allocated memory right after the first block it allocated.

Thanks, Mario.
I had suspected that I was plain lucky!
Also, you have a "memory leak" in your program. You never delete [] either
block you allocated.


The memory leak was due to a really careless and hasty bit of programming. I
was just trying to see if the concept worked. Forgot to cross the t's and
dot the i's!
 
K

Kevin Goodsell

Mario wrote:

(Please don't top-post. See section 5 of the FAQ for posting guidelines:
http://www.parashift.com/c++-faq-lite/)
NO. You initialized m to be n+10; but in the very next line you point m to
something else. When you call operator new, it will allocate the required
block of memory (if there is free memory) and assign the address of the
beginning of that block to m again. No one guarantees that the bock new
reserves will have the address that m pointed to before.

All correct.
It is simply by
"accident" (or how the new algorithm works) that the second invocation of
new allocated memory right after the first block it allocated.

I think it's very unlikely that this happened at all. Look at the code
more closely. The memory pointed to by m was never used at all. The code
simply writes off the end of n.

-Kevin
 
S

Sumit Rajan

Kevin Goodsell wrote:


I think it's very unlikely that this happened at all. Look at the code
more closely. The memory pointed to by m was never used at all. The code
simply writes off the end of n.

-Kevin

Well, it did happen. I actually did try out that snippet.
I'm using g++ with RHL9.

Regards,
Sumit.
 
K

Kevin Goodsell

Sumit said:
Kevin Goodsell wrote:






Well, it did happen. I actually did try out that snippet.
I'm using g++ with RHL9.

The snippet does absolutely no verification that the arrays are
adjacent. In other words, testing that code proves absolutely nothing. I
think you misunderstood what I was saying. The code contained the
equivalent of:

int *n = new int[10];
int *m = new int[10];

for (int i=0; i<20; i++)
{
n = i;
}

for (int i=0; i<20; i++)
{
cout << n << endl;
}

This absolutely does not show that the memory pointed to by m comes
immediately after the memory pointed to by n. In fact, the vast majority
of dynamic memory implementations work in a way that would prohibit this.

-Kevin
 
S

Sumit Rajan

Kevin Goodsell wrote:

The snippet does absolutely no verification that the arrays are
adjacent. In other words, testing that code proves absolutely nothing. I
think you misunderstood what I was saying. The code contained the
equivalent of:

You're right - I misunderstood your reply. Please accept my apologies. I
thought you were questioning the fact that I got the output that I did.
int *n = new int[10];
int *m = new int[10];

for (int i=0; i<20; i++)
{
n = i;
}

for (int i=0; i<20; i++)
{
cout << n << endl;
}

This absolutely does not show that the memory pointed to by m comes
immediately after the memory pointed to by n. In fact, the vast majority
of dynamic memory implementations work in a way that would prohibit this.


As I said in one my previous posts (a reply to Mario), I was merely trying
to find out why this worked in the first case.

The replies that Mario and you posted have helped helped me resolve this.



Thank you.

Sumit.
 
L

llewelly

Julián Albo said:
Howard Hinnant escribió:


What?

realloc is typically used for arrays and buffers that need dynamic
resizing.

vector provides dynamic resizing, via resize(), push_back(), insert(),
etc.
 
?

=?iso-8859-1?Q?Juli=E1n?= Albo

llewelly escribió:
realloc is typically used for arrays and buffers that need dynamic
resizing.

vector provides dynamic resizing, via resize(), push_back(), insert(),
etc.

Yes, and an int typically is used for integer values.

Regards.
 
J

Jon Bell

Howard Hinnant escribió:


What?

#include <iostream>
#include <vector>

using namespace std;

int main ()
{
vector<int> foo(3);
foo[0] = 123;
foo[1] = 456;
foo[2] = 789;

for (int k = 0; k < foo.size(); ++k)
cout << foo[k] << " ";
cout << endl;

foo.resize(5);
foo[3] = 987;
foo[4] = 654;

for (int k = 0; k < foo.size(); ++k)
cout << foo[k] << " ";
cout << endl;

return 0;
}
 
?

=?iso-8859-1?Q?Juli=E1n?= Albo

Jon Bell escribió:
#include <iostream>
#include <vector>

using namespace std;

int main ()
{
vector<int> foo(3);
foo[0] = 123;
foo[1] = 456;
foo[2] = 789;

for (int k = 0; k < foo.size(); ++k)
cout << foo[k] << " ";
cout << endl;

foo.resize(5);
foo[3] = 987;
foo[4] = 654;

for (int k = 0; k < foo.size(); ++k)
cout << foo[k] << " ";
cout << endl;

return 0;
}

Nice code. Is an exercise?

Regards.
 

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

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,143
Latest member
DewittMill
Top