-1

I had some issues when working with new. I was hoping that you can point out what Im doing wrong. Here's my code sample:

unsigned ** Create_Matrix(const unsigned &n) {
    unsigned **matrix=new unsigned*[n];
    for (unsigned i = 0; i < n; ++i)
        matrix[i]=new unsigned[n];
    return matrix;
}
int main() {
    unsigned n;
    std::cin>>n;
    unsigned** matrix=Create_Matrix(n);
    return 0;
}
V Mircan
  • 203
  • 5
  • 13
  • 1
    You may have an easier time by using a Matrix library or writing your own Matrix class. – Thomas Matthews Mar 02 '18 at 20:29
  • I don't see any issue in the code you have.. It allocates fine but you don't free the memory when you are finished with it.. What exactly is the issue you are seeing? – Brandon Mar 02 '18 at 20:30
  • You may want to clarify your `unsigned` with the kind of `unsigned`, such as `unsigned char`, `unsigned int`, `unsigned long`, or `unsigned short`. – Thomas Matthews Mar 02 '18 at 20:30
  • Thanks for the tips. It seems i made an error somewhere else in the program after i changed some things around and i think it just loaded the previous successful build. Thats why i got confused. The allocation seems to be working fine now. – V Mircan Mar 02 '18 at 20:33
  • Tactical note: The array of arrays approach can be brutally slow due for a number of reasons. Consider a single array of size `n*n` and a helper function or class wrapper to perform the 2D to 1D mapping. Example: https://isocpp.org/wiki/faq/operator-overloading#matrix-subscript-op . As Jive points out in his answer, use `vector` where possible as it makes the memory management utterly trivial for virtually no cost. – user4581301 Mar 02 '18 at 20:50

1 Answers1

0

Try to forget you ever knew about new and new[]. Really. I mean it.

#include <vector>
#include <iostream>

using value_type = double; // Or whatever
using row = std::vector<value_type>;
using matrix = std::vector<row> ;

matrix create_matrix(unsigned n) {
    matrix mat(n);
    for (auto& r : mat) {
        r.resize(n);
    }
    return mat;
}
int main() {
    unsigned n;
    std::cin >> n;
    auto mat = create_matrix(n);
    return 0;
}
Jive Dadson
  • 16,680
  • 9
  • 52
  • 65
  • 1
    This looks so clean. Im though that working without the library would help me learn but i see it might not be the case. – V Mircan Mar 02 '18 at 20:42
  • The opener's a bit extreme. if someone forgets everything they'll have to relearn why they shouldn't use `new` all over again. – user4581301 Mar 02 '18 at 20:43
  • 1
    Libraries like STL are what makes C++ C++. Many of the features of C++ are designed for writing libraries and generic code. The source is strong with this one. – Jive Dadson Mar 02 '18 at 20:46
  • @VMircan if your instructor hasn't covered the concept, read up on [Resource Allocation Is Initialization](https://stackoverflow.com/questions/2321511/what-is-meant-by-resource-acquisition-is-initialization-raii) to see why Jive's suggestion is the right way to go. `vector` is one of the best examples out there of RAII in action. If your work will not allow you to use `vector` seriously consider writing your own. It's a great learning experience and will save you a lot of time and trouble until your instructor gets their head out of the sand. – user4581301 Mar 02 '18 at 20:54
  • @user4581301 I pimp this every chance I get: https://youtu.be/YnWhqhNdYyk I wish she would hurry up and write an intro to C++. While she dawdles, a million pointers hang and leak. – Jive Dadson Mar 02 '18 at 20:58
  • 1
    I try to reserve my use of for occasions that really need it so as to not reduce its impact. I don't even have to click the link to know that goes to Kate Gregory's Stop Teaching C. – user4581301 Mar 02 '18 at 20:58
  • @user4581301 Very well. That will cost you one up-vote. – Jive Dadson Mar 02 '18 at 20:59