Matrix Multiplication for any proper order with dynamic memory allocation. The output for same orders (2x2 or 3x3) for both matrices result in proper computation, but the orders like mat1 2x3 & mat2 3x2.give segmentation fault. I am not able to ascertain how am I accessing any illegal memory when I am doing the memory allocation beforehand.
I have a class of type say
which is a member of another class as a pointer
My question is that when I create a Sample ObjectSample obj;and want to initialize the Property* prop;
//add stuff to mapis doing this and then calling the individual set functions a good way of doing allocation?
or I should declare a Property Object first and then do the complete initialization before dynamically allocating memory?Declare an object
Note the above is just incomplete pseudo code.
1 Answer
This is quite an interesting question you have, with quite a simple answer.In your current situation a copy constructor would be no different than a default constructor when it comes to whether or not the compiler will accept it.So, to first judge whether you should use a copy constructor or a default constructor we need to take into account 2 factors, performance and memory.
Since the copy constructor and default constructor have barely any difference when it comes to performance, we will skip analysing performance and get to the defining factor, memory.
As TheUndeadFish has stated in the comments, using a copy constructor would be inefficient compared to the default constructor because you would have to stack allocate a whole new, useless object for every time you want to allocate that object in the heap. With a default constructor, you only allocate the object once, then you assign the properties after allocation, which is much more efficient than using a copy constructor.
So, in conclusion, I would personally suggest using a default constructor in your situation instead of a copy constructor.
Not the answer you're looking for? Browse other questions tagged c++ or ask your own question.
I am trying to perform matrix multiplication(dynamic memory allocation) where that the user can enter any valid order for matrix multiplication (i.e. column1=row2). The output for same orders (2x2 or 3x3) for both matrices result in proper computation, but the orders like mat1 2x3 & mat2 3x2..give segmentation fault. I am not able to ascertain how am I accessing any illegal memory when I am doing the memory allocation beforehand.
kindly advise, pls forgive me if I am making some silly mistake...
following is the complete code:
4 Answers
You use c1/2 in your for
instead of r1/2.
If r1 < c1, you end up outside of the memory you allocated.
If r1 > c1, you end up with uninitialized pointers.
Not related to the issue but you should write int main()
instead of main()
, the second one is accepted but the first one easier to read.
Here is the code for any valid matrix multiplication....feel free for 'Queries'....
I attached bellow the code for Matrix Multiplication for any proper order with dynamic memory allocation
For completeness I used 3 different methods for matrix multiplication: one function double** multMatrixpf
(see equivalent function Fortran/Pascal) and two subroutine/procedure(Fortran/Pascal like), where by first void multMatrixp
you need to allocate_mem(&c,ro1,co2)
outside and in second subroutine void multMatrixpp
the matrix c1
is allocated inside the subroutine. All of these three methods give the same result.
As well I used different methods for initialising arrays.
where as input matrix we have in.txt
in unix like cmmd line execute the command:
$ time ./Matmult < in.txt > out.txt
and you get the output
out.txt
While computing the matrix multiplication you have to run the k loop from 0 to c2, and not from 0 to c1.