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 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.
<- 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/
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.
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.
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!
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.
In article <3F***************@terra.es>,
Julián Albo <JU********@terra.es> wrote: Use std::vector.
Bravo! std::vector *is* re-new.
-Howard
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.
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.
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.
Howard Hinnant escribió: Use std::vector. Bravo! std::vector *is* re-new.
What?
Regards.
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.
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.
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
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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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,...
|
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, "$#,###") & " / " &...
|
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...
|
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...
|
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...
|
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)....
|
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...
|
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...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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,...
|
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...
|
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...
|
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...
|
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,...
| |