-2

i want to dynamically allocate the array. program 1:

#include <iostream>
using namespace std;
int main()
{
    int m,n;
    cout<<"enter the no of rows and column for dynamic array:";
    cin>>m>>n;
    int ptr[m][n];
}

program 2:

#include <iostream>
using namespace std;

int main()
{
    int m,n;
    cout<<"enter the no of rows and column for dynamic array:";
    cin>>m>>n;
    int **ptr;
    ptr=new int *[m];
    for(int i=0;i<m;i++)
    {
        ptr[i]=new int [n];
    }
}

its giving problem in the program 1: expression must have a constant value. the value of variable "m" cannot be used as a constant. the value of variable "n" cannot be used as a constant.

I was using that type of program before but it did not gave me this problem before. I am using g++ compiler. I tried changing the compiler and reinstalling mingw

  • 1
    `cin>>m>>n; int ptr[m][n];` is not legal C++, so you should get an error. GCC has an extension that allows this non-standard code, but you should avoid using it. Compiling with `-pedantic-errors` will disable the extension in GCC. – NathanOliver Apr 19 '23 at 18:05
  • 1
    The title makes no sense. How can both work with no problems but the first one has a problem? – user4581301 Apr 19 '23 at 18:17
  • I meant to say they are dynamically allocating memory but when I run this code in vs code it's showing some problem in the problem tab but not in the terminal. – CYBA STEPHEN Apr 19 '23 at 18:21
  • @CYBASTEPHEN as mentioned the 1st version can't work, since it's not legal c++. Anyways if you see it _"working"_ (as in "there are no compiler errors), it's still possible that values provided for `m` and `n` may overflow your available local memory ("stack"). – πάντα ῥεῖ Apr 19 '23 at 19:35

1 Answers1

1

Use std::vector for dynamic memory allocation. That way you will not have memory leaks (like in your second example). See : https://en.cppreference.com/w/cpp/container/vector

#include <iostream>
#include <vector>

// using namespace std; NO unlearn this 

int main()
{
    int rows, colums;
    std::cout << "enter the no of rows and column for dynamic array:";
    std::cin >> rows >> colums;
    int initial_value{ 3 };
    std::vector<std::vector<int>> values(rows, std::vector<int>(colums,initial_value));

    // https://en.cppreference.com/w/cpp/language/range-for
    for (const auto& row : values)
    {
        for (const auto value : row)
        {
            std::cout << value << " ";
        }
        std::cout << "\n";
    }

    return 0;
}
Pepijn Kramer
  • 9,356
  • 2
  • 8
  • 19
  • Efficiency note: a `vector` is basically a pointer to an array of data and some book keeping. So a `vector` of `vector`s is a pointer to an array of `vector`s that each point to their own array of data. This results in the data potentially being scattered throughout memory and generally not being very cache friendly. It is often more efficient to make one one `vector` of size `m`x`n` and perform the indexing math yourself. [Example](https://stackoverflow.com/a/36123944/4581301). – user4581301 Apr 19 '23 at 18:24
  • @user4581301 If performance is an issue I totally agree. – Pepijn Kramer Apr 19 '23 at 18:25