Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c++ > #14218 > unrolled thread
| Started by | Stanley Rice <heconghui@gmail.com> |
|---|---|
| First post | 2012-02-17 19:03 -0800 |
| Last post | 2012-02-18 03:05 -0600 |
| Articles | 2 — 2 participants |
Back to article view | Back to comp.lang.c++
use unintialized_copy where is initialized Stanley Rice <heconghui@gmail.com> - 2012-02-17 19:03 -0800
Re: use unintialized_copy where is initialized Paavo Helde <myfirstname@osa.pri.ee> - 2012-02-18 03:05 -0600
| From | Stanley Rice <heconghui@gmail.com> |
|---|---|
| Date | 2012-02-17 19:03 -0800 |
| Subject | use unintialized_copy where is initialized |
| Message-ID | <6193789.0.1329534189450.JavaMail.geo-discussion-forums@pbie8> |
`the difference between *uninitialized_copy* function and *copy* is that the
*uninitialized_copy* function could copy the object at the location where
is uninitialized. For example:
int ia[5] = {1, 2, 3, 4, 5};
int n = sizeof ia / sizeof *ia;
int *ib = (int *)malloc(n * sizeof *ia);
uninitialized_copy(ia, ia + n, ib);
My question is, what if I copy the initialized memory by calling the
uninitialized_copy function. Instead of allocatiing the memory by calling
malloc, I use new.
int *ib = new int[n];
uninitialized_copy(ia, ia + n, ib);
I works too. But am i correct? If so, what's the difference between copy
and uninitialized_copy?
Thanks.
[toc] | [next] | [standalone]
| From | Paavo Helde <myfirstname@osa.pri.ee> |
|---|---|
| Date | 2012-02-18 03:05 -0600 |
| Message-ID | <Xns9FFD70C6FA72Amyfirstnameosapriee@216.196.109.131> |
| In reply to | #14218 |
Stanley Rice <heconghui@gmail.com> wrote in
news:6193789.0.1329534189450.JavaMail.geo-discussion-forums@pbie8:
> `the difference between *uninitialized_copy* function and *copy* is
> that the *uninitialized_copy* function could copy the object at the
> location where is uninitialized. For example:
>
> int ia[5] = {1, 2, 3, 4, 5};
> int n = sizeof ia / sizeof *ia;
> int *ib = (int *)malloc(n * sizeof *ia);
> uninitialized_copy(ia, ia + n, ib);
>
> My question is, what if I copy the initialized memory by calling the
> uninitialized_copy function.
You are overwriting the currently existing objects, so that they have no
chance to clean up any resources they are managing. For example, if there
was an object managing a resource handle or pointer to some other
dynamically allocated memory (and properly closing/freeing it in its
destructor and assignment operator), then just blindly overwriting the
object would cause a resource or memory leak.
> Instead of allocatiing the memory by
> calling malloc, I use new.
> int *ib = new int[n];
> uninitialized_copy(ia, ia + n, ib);
>
> I works too. But am i correct? If so, what's the difference between
> copy and uninitialized_copy?
In the case of int, there is no difference in practice as ints do not
have assignment operators or destructors. You will have trouble only with
objects with non-trivial assignment operators or copy constructors:
copy: calls assignment operator on each old object in the array:
uninitialized_copy: calls copy constructor for each new object in the
array.
In theory I guess also getting it wrong for primitive types like int is
UB, as some hypothetical hardware may have trapping representations for
ints and may require different handling of memory locations depending on
if they are known to contain a valid int value or not.
Cheers
Paavo
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.c++
csiph-web