Cloning double value?!

Hi everyone,

I'm sure this is an absurdly simple problem, yet I'm having some trouble figuring this out... I have a two-dimensional double array that I've created, and have instantiated everything (i.e. double** m).

Now, this is contained within a class, and I have a function to get a specific value from the the array. The function is as follows
1
2
3
double myarray::get(int a, int b) const {
  return m[a][b];
}


My problem is that when I overload the equals ("=") operator to copy all the individual values of the array, the actual address of the newly instantiated object and its m array are different, but all the addresses of the doubles in the array are the same in the old and the new array.

How can I get the newly instantiated doubles to point to a clone / copy of the original doubles, rather than have them share the same address?

Thanks!
The get function has nothing to do with the assignment operator so it doesn't help much. Why not post the code for your operator= for the myarray class? It sounds like you are only allocating memory for the pointers in the new object but are not doing a true deep copy of the array.
Thanks so much for your reply! Here's the code for the = operator...

1
2
3
4
5
6
7
  m = new double *[sizeRows];
  for (int a = 0; a < sizeRows; a++) {
    m[a] = new double[sizeCols];
    for (int b = 0; b < sizeCols; b++) {
      m[a][b] = m1.get(a, b);
    }
  }


Where m1 the object is on the right-hand side, and m is the left-hand side object's two dimensional array.
The code looks correct and should perform a bitwise copy as expected.

I would change the innermost loop to this, though:
memcpy(m[a],m1.m[a],sizeCols);
memcpy() is faster than a for when copying arrays.
Last edited on
I would have to disagree with helios on the point about memcpy. I hate memcpy as a tool in an object oriented program unless it is absolutely necessary. Also, the class instances shouldn't have direct access to each others internal parts. He would have to code new interface functions to return const pointers to th start of every column array. I wouldn't worry about the for loop right now. I'd worry about the fact that there is no way that what was posted is the entire operator=. Where is the deallocation of original memory at? Where is the declaration of the function?


Also, how did you determine that the addresses for each element in the original and new array objects? Is the code that you posted the same code that was compiled and executed? Instead of having us ask questions that result in small, critical details being dished out why not just post the entire class declaration and definition so that we can try to look through it?

Also, let us see an example of how you are using the assignment operator. Are you sure that you are invoking it? you are not doing this are you?

// not sure what constructor looks like but if you did something like this:
myarray instance1; // construct an instance and fill it with data.

myarray instance2 = instance1; // invokes copy constructor not assignment operator.
Last edited on
I hate memcpy as a tool in an object oriented program unless it is absolutely necessary.
So it's okay to use C arrays, but not okay to use memcpy()? Where do you draw the line?
If you're going to do it a la C, you might as well go all the way.

Also, the class instances shouldn't have direct access to each others internal parts.
Um... What? It's an operator=(). It's supposed to duplicate the exact state of the parameter. What would be the point of encapsulating the class from itself?
Last edited on
Personally, I'd use a 1-D vector<> and write a function that converts a 2D "coordinate" to a 1-D index
index = row * NUM_COLS + col, for example. Then vector's copy constructor and assignment operator can be used.

What do c arrays have to do with memcpy? I never said anything about not using c arrays. Since the original question involves classes and assignment operators it obviously isn't a C program. It is a C++ program. This is all besides the point since not using memcpy has nothing to do with the problem. If you really wanted to avoid the extra for loop, I would use std::copy not memcpy. You are correct about the unnecessary function call to get though. The assignment operator does have direct access to the const m1 object's data.
I'm guessing that this is one of those assignments where a student has to write a dynamic array class for a teacher because otherwise I don't know what the point is of developing a wrapper class for a 2d array. I'd just use std::vector or some boost type if boost can be used. Or I would just use a c style array directly but that would be my last choice in a C++ program.


The point remains that without seeing an example that we can look at or compile that demonstrates the issue, I don't see how the original question can be answered. We still haven't seen a complete operator= function nor a copy constructor nor any code that demonstrates the problem.
Last edited on
What do c arrays have to do with memcpy?
new double[sizeCols];
That doesn't look like a vector at all.
Helios, I don't even know what you are talking about. The statement is nonsensical. That part of the discussion is over as far as I am concerned. If the OP wants to post follow-up great and if not then the thread should just die. have a nice day.
Topic archived. No new replies allowed.