473,396 Members | 2,147 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

grow an array with new

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
Jul 19 '05 #1
15 6557
"<- Chameleon ->" escribió:
If I allocate an array with
-----
a = new int(10);
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.
Jul 19 '05 #2
<- Chameleon -> wrote in news:bj**********@nic.grnet.gr:
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.
--
http://www.victim-prime.dsl.pipex.com/
Jul 19 '05 #3
Rob Williscroft wrote:
<- Chameleon -> wrote in news:bj**********@nic.grnet.gr:
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.

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] = i;

int* m = n+10;
m = new int[10];
for(int i=10; i<20;i++)
n[i] = i;
for (int i=0; i<20; i++)
std::cout << n[i] << ' ';
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.
Jul 19 '05 #4
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" <su********@myrealbox.com> wrote in message
news:bj************@ID-206022.news.uni-berlin.de...
Rob Williscroft wrote:
<- Chameleon -> wrote in news:bj**********@nic.grnet.gr:
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.

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] = i;

int* m = n+10;
m = new int[10];
for(int i=10; i<20;i++)
n[i] = i;
for (int i=0; i<20; i++)
std::cout << n[i] << ' ';
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.

Jul 19 '05 #5
Mario wrote:
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!
Jul 19 '05 #6
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
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.

Jul 19 '05 #7
In article <3F***************@terra.es>,
Julián Albo <JU********@terra.es> wrote:
Use std::vector.


Bravo! std::vector *is* re-new.

-Howard
Jul 19 '05 #8
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.
Jul 19 '05 #9
Sumit Rajan wrote:
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.


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] = i;
}

for (int i=0; i<20; i++)
{
cout << n[i] << 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
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.

Jul 19 '05 #10
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] = i;
}

for (int i=0; i<20; i++)
{
cout << n[i] << 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.



Jul 19 '05 #11
Howard Hinnant escribió:
Use std::vector.

Bravo! std::vector *is* re-new.


What?

Regards.
Jul 19 '05 #12
Julián Albo <JU********@terra.es> writes:
Howard Hinnant escribió:
> Use std::vector.

Bravo! std::vector *is* re-new.


What?


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

vector provides dynamic resizing, via resize(), push_back(), insert(),
etc.
Jul 19 '05 #13
llewelly escribió:
> Use std::vector.
Bravo! std::vector *is* re-new.


What?


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.
Jul 19 '05 #14
In article <3F***************@terra.es>,
Julián Albo <JU********@terra.es> wrote:
Howard Hinnant escribió:
> Use std::vector.

Bravo! std::vector *is* re-new.


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;
}

--
Jon Bell <jt*******@presby.edu> Presbyterian College
Dept. of Physics and Computer Science Clinton, South Carolina USA
Jul 19 '05 #15
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.
Jul 19 '05 #16

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Ellen K. | last post by:
I made a database to hold recordings of calls made to our customers. When I made it I set the size of the primary datafile to 18GB. It's been running flawlessly for over 10 months. A few days...
2
by: Stewart | last post by:
Originally posted in comp.lang.javascript: Newsgroups: comp.lang.javascript From: "Stewart" Date: 23 Aug 2005 02:50:04 -0700 Local: Tues, Aug 23 2005 10:50 am Subject: FireFox, RemoveChild,...
2
by: Derek Erb | last post by:
I have a field in a report which is filled by a function. This is because I want the values displayed in a particular way. The field is filled with: strVal = Format(ValLow, "$#,###") & " / " &...
4
by: Mal | last post by:
I have an ACC 2000 database that has a strange behaviour I have a small table, with just a few fields... My report has very simple grouping and sorting, no code bar a NODATA event. I have a...
1
by: welles.lo | last post by:
Hi all, I found that the default "can grow" in Crystal Reports 10 is different from the one in Crystal Report 9. In version 9, when "can grow" is disable, the words in the field are chopped...
11
by: Edson Peacock | last post by:
I have a report with sub reports, one of the subreports have 12 text boxes that are 2" high and I want them all to grow if one goes to 3" high. If anyone has any suggestions they are very much...
1
by: Ronen Yacov | last post by:
Body: Hi Everyone, I have a problem with can grow fields on the data report. I have some fields aligned in a line which are "Can grow" enabled ( their "can grow" property is set to true)....
1
by: franc sutherland | last post by:
Hi, I am using Access 2003. I have set up an invoice in a report. I have set up the name and address as text boxes in the Page Header which I've made as small as possible then set the Can...
4
by: Sheldon | last post by:
Hi, I have a unique case where I need an array of structs that grows and within this array is another struct that grows in some cases. I'm having trouble allocating memory. Since I have never...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.