0

My question is not about the concept but about the use of these words.

Assuming this case:

int *a=new int[3];
int *b=a; // It is called shallow copy

My question: Is it right to call it a shallow copy? I can not see any shallow copy. int* is a pointer and it is all about storing some address. So, in the previous example every thing about it was totally copied because the actual data is not part of it. So, why it is called shallow not deep?

For more clarification, I see it like if someone do this:

std::string file_path="C:/file.txt";
std::string another_path=file path;

and called a shallow copy because the file was not really copied.

BTW, are deep/shallow words well-defined in the C++ standard?

Humam Helfawi
  • 19,566
  • 15
  • 85
  • 160
  • 2
    What do you actually mean _"because the file was not really copied."_? The only thing in question here is `file_path` being copied. – πάντα ῥεῖ Jan 23 '16 at 19:52
  • 2
    The C++ standard does not use, or need to use, the terms "deep copy" or "shallow copy". – Nicol Bolas Jan 23 '16 at 19:55
  • I give a strange example. I meant to say that calling a copy of a pointer a shallow copy is exactly like calling the string copy a shallow copy because no file was copied on the hard disk. The string copy in my example is a deep copy indeed. in the same concept the pointer copy should be named a deep copy – Humam Helfawi Jan 23 '16 at 19:55
  • Common does it seem duplicated of that question! – Humam Helfawi Jan 23 '16 at 19:57
  • 1
    @HumamHelfawi: The difference is obvious. A shallow pointer copy is *always* shallow, no matter the concept. It is always not "deep". Whereas your string example is only shallow because of the *data* that it stores. The fact that it's storing a string path, a conceptual reference. Copying a string may or may not be conceptually shallow. Copying a pointer *always* is shallow, whether conceptually or in terms of memory operations. – Nicol Bolas Jan 23 '16 at 19:57
  • @HumamHelfawi it's a shallow copy of the array, but a simple/full copy of the pointer if you want to consider only the pointer object. – Ilya Jan 23 '16 at 20:02
  • @NicolBolas: I mean that data that the pointer points to is not part of it. It could be placed very far from it (physically or logically). If we see the struct of a pointer (or even a class with pointers to some data like std::vector) we will not see anything about the data itself. So when copying a pointer, we actually copy everything about it – Humam Helfawi Jan 23 '16 at 20:03
  • @Ilya: exactly.. my problem is why calling it a shallow copy of the pointer. It is not. It's a COPY of the pointer and NOTHING to the data. – Humam Helfawi Jan 23 '16 at 20:04
  • @HumamHelfawi Yes. It's just that usually we're not interested in the pointer alone, so calling it a shallow copy of the (pointer + data) set. Calling it a shallow copy of the pointer is a language shortcut. – Ilya Jan 23 '16 at 20:11
  • @Ilya: It seems more logical now... – Humam Helfawi Jan 23 '16 at 20:15

0 Answers0