Memory Location Issues.

Hello, everyone. :)

I have a question about a program I am making; a short and long version are provided for your convenience. :D

Short version: Is there any way to take a variable (int or string) in RAM and copy it over to a different memory address?

More detail:

I'm having a problem with a C++ program that I am making. Mainly with the scope of a class. Here is the code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
class myClass {
  private:
    string *names;
    int arrSize;
    //Accessors for both variables are assumed.
  public:
    ~myClass() {
      delete[] names;
    }

    void copy(myClass *cl) {
      names = new string[cl->getArrSize()];
      for(unsigned int i=0;i<cl->getArrSize();i++)
        names[i] = cl->getName(i); //Assigns names[i] to cl.names[i]
    }

    string toString() {
      string result = "";
      for(int i=0;i<arrSize;i++)
        result += names[i] + " ";
      return result;
    }
}

myClass getClassFromFile(string filePath) {
  //reading from file, storing in to class.
}

int main(int argc, char* argv[]) {
  myClass cla;
  cla.copy(&getClassFromFile("C:\Documents and Settings\Administrator\b.txt");
  cout << cla.toString();
  system("PAUSE");
}


My problem is with the scope of the reference from getClassFromFile. It correctly places every array element in to cla with the copy() method (already verified with debug statements), reads from the file, etc.

However, as soon as the myClass instantiation from getClassFromFile disappears, it destroys the memory in cla's students array. (All of the strings in that array have the same reference as the strings in the getClassFromFile instantiation).

Thus, the automatic destructor call is ruining my cla class.

Is there any way to change my current code so that copies of 'names', but with different memory locations, are placed in to cla's students array instead of the originals which are soon after deleted?

Thanks. :)
~Yuan
Last edited on
myClass getClassFromFile(string filePath) {

Having a function return an instance of the class creates a copy of the class. When the copy is destroyed, it is calling your dtor which is delete[]ing your array.

When you have dynamically allocated stuff like this in your class, you need to write your own copy constructor and assignment operator, because the ones C++ gives you by default simply copy the pointer, and not the data the pointer points to.

- OR -

you could use a vector<string> instead of allocating by hand with new

- OR -

you could not return the value, but instead pass an object by reference so no copy occurs:

1
2
void getClassFromFile( myClass& cla_out, string filePath )
{ }


(although writing a copy ctor and assignment operator would still be advised.


ALSO
you REALLY should have a default ctor for your class, which at least zeros the pointer so you don't attempt to delete[] something that hasn't been allocated. I also don't know how getClassFromFile is filling your myClass... but it shouldn't be allocating the buffer (myClass should be in charge of all of that).

But I'm going off on a tangent here. The solution to your problem is to write a copy constructor:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class myClass
{
 // ...
public:
  // ...

  // copy ctor
  myClass( const myClass& rhs )
  {
    // << copy rhs to 'this' here >>
  }

  // assignment operator
  myClass& operator = ( const myClass& rhs )
  {
    //  << destroy 'this's current data, and replace it with rhs's data here >>

    return *this;
  }
};


I'd also get rid of your copy() function, as it's somewhat flawed in that it fails to clean up any existing buffer, and it kind of poinless if you have proper copy ctors and assignment operators.

a.copy(&b); would be replaced with: a = b;
Hello Disch,

Thank you very much for all of your help. It fixed my problem (yay!).

I will also look in to the last revision that you suggested for my code.

Thanks again,
Yuan
Topic archived. No new replies allowed.