Why is that the both are working, but in first its giving some type of problem and in second it doesn't?

huangapple go评论70阅读模式
英文:

Why is that the both are working, but in first its giving some type of problem and in second it doesn't?

问题

Program 1 is giving an error because it's trying to create a 2D array with dimensions specified by variables "m" and "n," which is not allowed in standard C++. To dynamically allocate a 2D array, you should use Program 2. Program 2 uses pointers to create a dynamic 2D array, which is the correct way to allocate memory for a variable-sized array.

英文:

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

得分: 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;
}
英文:

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 &lt;iostream&gt;
#include &lt;vector&gt;

// using namespace std; NO unlearn this 

int main()
{
    int rows, colums;
    std::cout &lt;&lt; &quot;enter the no of rows and column for dynamic array:&quot;;
    std::cin &gt;&gt; rows &gt;&gt; colums;
    int initial_value{ 3 };
    std::vector&lt;std::vector&lt;int&gt;&gt; values(rows, std::vector&lt;int&gt;(colums,initial_value));

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

    return 0;
}

huangapple
  • 本文由 发表于 2023年4月20日 02:03:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/76057589.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定