0

if I use member initialization list in my constructor of a class that has char* in it, does it copy the value to a new allocated memory or just points the pointer on the same value?

ex.

MyString::MyString(const MyString & other) : m_str(other.m_str) {}

will this be fine or should I run cell by cell to copy the info?

EDIT: just to clarify the question I changed a line.

Ziezi
  • 6,375
  • 3
  • 39
  • 49
Ofek .T.
  • 741
  • 3
  • 10
  • 29
  • You may want to see: [What is The Rule of Three?](http://stackoverflow.com/questions/4172722/what-is-the-rule-of-three) – NathanOliver Jan 25 '16 at 14:20
  • It is called *initialization list* because it is a list of initializations. It would do a similar thing to `char *m_str = other.m_str;` – M.M Jan 25 '16 at 14:23
  • Please provide at least a relevant skeleton definition of your class. – Kerrek SB Jan 25 '16 at 15:03
  • When you say "does it copy the value", a) does "it" refer to "the constructor" (if it does, please say so and don't play the pronoun game), and b) does "copy the value" mean "copy the string which the pointer points to", rather than "copy the value of the pointer"? – Kerrek SB Jan 25 '16 at 15:05
  • _"will this be fine or should I run cell by cell to copy the info?"_ makes that pretty clear, I'd say. – Lightness Races in Orbit Jan 25 '16 at 15:05
  • Please clarify what "will be fine" means. Please show the destructor of your class if you have defined one. – Kerrek SB Jan 25 '16 at 15:06
  • I already got my answer so you can just forget about it. I dont need to show you the destructor anyone because it's a general question,I just put the code for an example of the IL call. I wondered if that would duplicate the pointer that points to the char[] or will it realy deep copy the char[] and make a pointer for that, I already assumed it would not deep copy it but asked to be on the safe side. So, I got my answer as that in IL it just flat-out copies what it gets - as if it's a pointer it would just copy the pointer itself. Thank you. Am I clear now and no need for further questions? – Ofek .T. Jan 25 '16 at 23:51

3 Answers3

2

I just copies the pointer, so they will both point to the same string.

This will likely cause you problems if you later want to delete the pointed-to string.

Bo Persson
  • 90,663
  • 31
  • 146
  • 203
0

It does both:

  • It copies the pointer.

  • The copy of the pointer points to the same thing as the original.

Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
  • This is not "both" in the sense that the OP described. – Lightness Races in Orbit Jan 25 '16 at 14:29
  • @LightnessRacesinOrbit: Hmm... maybe with a lot of interpretative wiggling... but as written, the OP says, "my class has a char pointer in it; does the copy constructor copy the value", which it does. – Kerrek SB Jan 25 '16 at 14:37
  • 1
    I don't think "not ignoring the next five words" counts as "a lot of interpretative wiggling", frankly. _"does it copy the value to a new alocated memory"_ is pretty clear that the OP meant a deep copy here. – Lightness Races in Orbit Jan 25 '16 at 14:39
  • @LightnessRacesinOrbit: The pointer *is* copied to newly allocated memory, namely the memory allocated for the new `MyString` instance. I thought carefully about whether the omission would affect the meaning and decided it wouldn't. – Kerrek SB Jan 25 '16 at 14:40
  • Factually? No, it wouldn't. But we're not talking about what _is_; we're talking about what the OP means by the question, and I applied careful heuristics to determine that it was more likely the OP was asking about deep vs shallow copy, than that the OP was asking about shallow vs shallow copy (???). – Lightness Races in Orbit Jan 25 '16 at 14:54
  • @LightnessRacesinOrbit: I get all that. The problem is just that the OP is horrendously confusing about the meaning of almost every word in the question. I think it would be much better to fix the question first, and then answer a clear question. – Kerrek SB Jan 25 '16 at 15:03
  • Me too! You'll note that, unlike you, I haven't answered it ;) xxx – Lightness Races in Orbit Jan 25 '16 at 15:04
  • @LightnessRacesinOrbit: Noted :-) And embarked on a series of comments requesting the question to be fixed. – Kerrek SB Jan 25 '16 at 15:05
  • I wish you well in your quest! – Lightness Races in Orbit Jan 25 '16 at 15:06
-2

A pointer variable is (in a very simplistic way of looking at it) basically nothing more than an integer variable that is handled specially by the compiler. The contents of a (initialized) pointer variable is the address to where it points in memory.

If you initialize one pointer variable to the contents of another pointer variable, you copy the contents, leading to two different variables both having the same content and both pointing to the same memory.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • Disappointed to see a smart guy like you propagating this "pointers are addresses" nonsense! – Lightness Races in Orbit Jan 25 '16 at 14:30
  • @Lightness Races in Orbit Could you add a link or an explanation to why pointers should not be regarded as _"address-holders"_ – Ziezi Jan 25 '16 at 14:42
  • 1
    @simplicisveritatis the ping syntax has no spaces in it (e.g. see what I did in this message). If you start typing the name and press Tab it will autocomplete to the correct syntax. I suspect LRiO meant to refer to the "pointers are integers" nonsense... otherwise I have no idea what he is talking about – M.M Jan 25 '16 at 14:52