hello, I have recently tried to create a function that sums two different arrays. For this, inside the function, I need to declare a double pointer (double **pout), but what if I am using the "new" command? As far as I know, every time you declare something with "new", you need to use "delete" at the end in order to erase that from the memory. My question is: If I'm declaring the variable as in the code below, do I need to use "delete" inside the function? I am just afraid that, for bigger matrices values, the memory can be "eaten" if this is not done, Thanks!
if you do find yourself in this situation, there are a number of ways to handle it.
1) old school, you simply document where the delete needs to happen. It is acceptable for the caller of a function to destroy the memory returned by the function, but this is not as common a practice as it was back when.
2) you can make a mini class or struct that destroys the memory for the object when it goes out of scope, and use the object instead of pointers as the types.
3) you can use one of the various smart pointer objects out there. Many of these are bloated, though.
4) here, as already said, you can use the c++ containers above instead of a pointer. This is the best way for your specific code. Options 2 and 3 may come up in other designs though. Option 1 you should understand in case you encounter it in legacy code.
you can also reshape the pointers but if you mess it up you have memory errors (like any other mangled pointer code, but this one is more prone to it if the coder does not understand it very well). I prefer the other direction, allocate single d and access as if 2-d with an inline accessor function. Are you able to use the containers, or confined to a pointer solution?
Matrix math specifically is a mess with 2-d pointer approach. For bigger matrices you can have page fault problems, and for a large # of common operations you end up having to copy and allocate the data structures excessively to do things like transpose, append X for RREF solve, multiplication, factorization, and more.
It may be that the appropriate solution for you depends on what you intend to do with your matrices. Once you start talking about "big" matrices an alarm bell starts ringing about sparse matrices, in which you only store the non-zero elements: often just 3, 5 or 7 non-zero bands for many physical problems, for example.
It's a personal preference but I rarely use multidimensional arrays directly, but, as @jonnin advises, use a 1-d array and access with conversion functions (n=MAXJ*i+j and its inverse) in places where it is needed. Adding 1-d arrays is, after all, rather easy.
I also concur with @jlborges about the use of valarray: it's nice (and I'm told it's efficient) to be able to write c = a + b to sum two entire arrays (with many more array-based mathematical functions as well). It's also how all arrays work in modern fortran.
So maybe we can request that you say what type of matrix you are working with and the intended use.
Thanks for the replies guys, I think I managed to overcome the confussion by just defining another argument in the function declaration, which is the result pointer. Then the pointer that is about to be converted can be declared inside the "main" function, therefore all "delete" happens at the end of the code, and no dynamical memory is left occupied. My next step is to build up a "matrix" class to englobe all possible matrix operations over matrix objects, but I am just starting to play with this :D !!