帮助在 C++ 中声明和使用动态分配的数组

发布于 2024-11-06 06:02:21 字数 2446 浏览 0 评论 0原文

我需要声明一个数组数组,因此使用下面的代码来完成此操作:

int **  maxc = new int *[proc_num];//memory allocated for elements of rows
      for (int i = 0; i < proc_num; i++)
      {
            maxc[i] = new int[n];//memory allocated for  elements of each column.
      }

问题是,上面的代码似乎无法编译。我收到以下编译错误:

A value of type "int *" cannot be assigned to an entity of type "int"

这是完整的代码:

//#include<math.h>
#include "stdafx.h"
#include <string>
#include <iostream>
#include <vector>
using namespace std;
/*
Banker's algorithm Implementation
*/
 int main(void)
 {
      int n=0;//number of resources we will  be dealing with 
      int proc_num =0;//Total number of processes to share available resources
      int* a = NULL;  // pointer to an int with initial set to point to nothing
     // int* maxc = NULL;
      int* maxR=NULL;
      int* avail = NULL;
      int* avail_temp = NULL;
      //int** alloc = NULL;
      int* unalloc = NULL;

      std::cout<<endl;
      std::cout <<" What is number of resources to be shared? :";
      cin >> n;
      std::cout<<endl;
      while(std::cin.fail())
      {
            std::cout<< " Error Please provide valid number !" <<endl;
            std::cin.clear() ;
            std::cout<<endl;
            std::cout <<" What is number of resources to be shared? :";
            cin >> n;
            std::cout<<endl;
      }

      maxR = new int[n];  // Allocate n ints and save ptr in maxc -- holds the max resources available.
      //get the maximum number of each Resources/ Ie Total Resources Available
      for(int i =0; i < n; i++)
      {
            int maxcin=0;
            std::cout << i;
            std::cout<< ". How many of resource #";
            std::cout<< i;
            std::cout<< " do you need to share ?"; 
            cin>>maxcin;
            maxR[i] = maxcin;
      }
      //<8,7,5,9>");

      std::cout<<endl;
      std::cout << "How many processes to share available resources?";
      cin>>proc_num;
      std::cout<<endl;

      int **  maxc = new int *[proc_num];//memory allocated for elements of rows
      for (int i = 0; i < proc_num; i++)
      {
            maxc[i] = new int*[n];//memory allocated for  elements of each column.
      }
}

I need to declare an array of arrays and so using the code below to accomplish that:

int **  maxc = new int *[proc_num];//memory allocated for elements of rows
      for (int i = 0; i < proc_num; i++)
      {
            maxc[i] = new int[n];//memory allocated for  elements of each column.
      }

The problem is, the code above does not seem to compile. I get the following compilation error:

A value of type "int *" cannot be assigned to an entity of type "int"

Here's the full code:

//#include<math.h>
#include "stdafx.h"
#include <string>
#include <iostream>
#include <vector>
using namespace std;
/*
Banker's algorithm Implementation
*/
 int main(void)
 {
      int n=0;//number of resources we will  be dealing with 
      int proc_num =0;//Total number of processes to share available resources
      int* a = NULL;  // pointer to an int with initial set to point to nothing
     // int* maxc = NULL;
      int* maxR=NULL;
      int* avail = NULL;
      int* avail_temp = NULL;
      //int** alloc = NULL;
      int* unalloc = NULL;

      std::cout<<endl;
      std::cout <<" What is number of resources to be shared? :";
      cin >> n;
      std::cout<<endl;
      while(std::cin.fail())
      {
            std::cout<< " Error Please provide valid number !" <<endl;
            std::cin.clear() ;
            std::cout<<endl;
            std::cout <<" What is number of resources to be shared? :";
            cin >> n;
            std::cout<<endl;
      }

      maxR = new int[n];  // Allocate n ints and save ptr in maxc -- holds the max resources available.
      //get the maximum number of each Resources/ Ie Total Resources Available
      for(int i =0; i < n; i++)
      {
            int maxcin=0;
            std::cout << i;
            std::cout<< ". How many of resource #";
            std::cout<< i;
            std::cout<< " do you need to share ?"; 
            cin>>maxcin;
            maxR[i] = maxcin;
      }
      //<8,7,5,9>");

      std::cout<<endl;
      std::cout << "How many processes to share available resources?";
      cin>>proc_num;
      std::cout<<endl;

      int **  maxc = new int *[proc_num];//memory allocated for elements of rows
      for (int i = 0; i < proc_num; i++)
      {
            maxc[i] = new int*[n];//memory allocated for  elements of each column.
      }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

烛影斜 2024-11-13 06:02:21

现在您得到的错误与您之前发布的错误完全不同。在最后的 for 循环中 -

maxc[i] = new int*[n];

应该是 -

maxc[i] = new int[n];

在您之前发布的代码片段中,它是正确的。另外,您应该使用 delete[] 释放由 new[] 获取的资源,否则会出现内存泄漏。

What the error now you get is entirely different from what you posted earlier. In the final for loop -

maxc[i] = new int*[n];

should be -

maxc[i] = new int[n];

In the snippet you earlier posted, it was correct. Also, you should deallocate resources acquired by new[] using delete[], else memory leak prevails.

北城孤痞 2024-11-13 06:02:21

您遇到的问题是在您发布的代码的这一部分中:

int **  maxc = new int *[proc_num];
for (int i = 0; i < proc_num; i++)
{
    maxc[i] = new int*[n];
}

在 for 循环内,您分配了一个新的 int 指针数组,但分配了 maxc< 的原始类型/code> 是指针到指针类型。在这种情况下,也可以解释为 maxc 是一个指向指针数组的指针。这当然是在第一个赋值中完成的,您从 new 返回一个指向 int* 数组的指针。因此,for 循环内的语句为每个数组元素分配了错误的类型。数组元素 maxc[x] 的类型是 int 指针,这意味着它们必须指向一个或多个 int 类型的对象,而不是 int* 类型的元素。但是循环中的 new 运算符正在尝试分配另一个 int 指针数组,并返回指向该分配数组的指针,这意味着返回的类型是 int* * 再次,这是不正确的。因此,您应该将循环内部更改为:

maxc[i] = new int[n]; //allocate an array of int's, and return an int pointer

我注意到您发布的第一段代码实际上是这样做的,所以也许您遇到了您错过的简单拼写错误?

最后,当您删除此内存时,由于每一行都是使用 new 创建的,因此您必须对表示 int 数组的每一行调用 delete > 的。接下来,您必须对表示指向 int 数组的 int 指针数组的列调用删除。因此释放所有分配的内存将如下所示:

for (int i=0; i < proc_num; i++)
{
    //call delete on each pointer in each column of the array that is pointing to a
    //row array of int's
    delete [] maxc[i];  
}

//call delete on the pointer pointing to the column of 
//the array that contains the original int pointers that were
//pointing to each of the row arrays of int's
delete [] maxc;

The problem you're encountering is in this section of the code you posted:

int **  maxc = new int *[proc_num];
for (int i = 0; i < proc_num; i++)
{
    maxc[i] = new int*[n];
}

Inside the for-loop, you're allocating a new array of int pointers, but the original type for maxc was of type pointer-to-pointer. In this context that could also be construed to say maxc is a pointer that points to an array of pointers. That of course accomplished on the first assignment, where you return a pointer from new that is pointing to an array of int*. Therefore the statement inside the for-loop is assigning the wrong type to each array element. The type for the array elements maxc[x] are int pointers, which means that they must point to either one or more objects of type int, not elements of type int*. But your new operator in the loop is trying to allocate another array of int pointers, and return a pointer to that allocated array, meaning the type returned is int** again, and that's not correct. So you should change the inside of your loop to read:

maxc[i] = new int[n]; //allocate an array of int's, and return an int pointer

I noticed the first segment of code you posted actually does this, so maybe you're encountering a simple typo you missed?

Finally, when you delete this memory, since each row was created using new, you will have to call delete on each row representing the array of int's. Next, you will have to call delete on the column representing the array of int pointers that were pointing to the array of int's. So freeing all the memory allocated would look like the following:

for (int i=0; i < proc_num; i++)
{
    //call delete on each pointer in each column of the array that is pointing to a
    //row array of int's
    delete [] maxc[i];  
}

//call delete on the pointer pointing to the column of 
//the array that contains the original int pointers that were
//pointing to each of the row arrays of int's
delete [] maxc;
此刻的回忆 2024-11-13 06:02:21

您可能在正确声明方面遇到问题,但您真正的问题不是使用标准容器来为您完成工作。

此代码:

  int **  maxc = new int *[proc_num];//memory allocated for elements of rows
  for (int i = 0; i < proc_num; i++)
  {
        maxc[i] = new int*[n];//memory allocated for  elements of each column.
  }

可以对此进行更正以使其编译。

  int **  maxc = new int *[proc_num];
  for (int i = 0; i < proc_num; i++)
  {
        maxc[i] = new int[n];
             //     ^^^^ Note no star here.
  }

但这仍然不是好的代码,因为您没有考虑异常(如果任何分配失败,此代码将会泄漏)。在使用过程中,如果您的代码生成异常,您将泄漏所有内存。

更好的解决方案是:

std::vector<std::vector<int> >  maxc(proc_num, std::vector<int>(n, 0)); // even initializes all elements to 0

如果您想要奇特,请查找 增强多维数组 这可能会给你带来一些小的性能改进(如果你正在进行矩阵乘法等)。

如果你真的想手动完成,那么我会开始学习类和 RAII。

You may have problems with getting the declarations correct, but your real problem is not using standard containers to do the work for you.

This code:

  int **  maxc = new int *[proc_num];//memory allocated for elements of rows
  for (int i = 0; i < proc_num; i++)
  {
        maxc[i] = new int*[n];//memory allocated for  elements of each column.
  }

Can be corrected to this to make it compile.

  int **  maxc = new int *[proc_num];
  for (int i = 0; i < proc_num; i++)
  {
        maxc[i] = new int[n];
             //     ^^^^ Note no star here.
  }

But it is still not good code as you are not taking into account exceptions (this code will leak if any allocation fails). During use if your code generates an exception you are going to leak all the memory.

A better solution would be:

std::vector<std::vector<int> >  maxc(proc_num, std::vector<int>(n, 0)); // even initializes all elements to 0

If you want to get fancy you look up the boost multi-dimensional array this may give you some small performance improvements (if you are doing matrix multiplication etc).

if you really want to do it by hand then I would start learning about classes and RAII.

青朷 2024-11-13 06:02:21

这:

int main() {
    int proc_num = 100;
    int n = 42;
    int **  maxc = new int *[proc_num];//memory allocated for elements of rows
    for (int i = 0; i < proc_num; i++) {
            maxc[i] = new int[n];//memory allocated for  elements of each column.
    }
}

对我来说编译得很好。

This:

int main() {
    int proc_num = 100;
    int n = 42;
    int **  maxc = new int *[proc_num];//memory allocated for elements of rows
    for (int i = 0; i < proc_num; i++) {
            maxc[i] = new int[n];//memory allocated for  elements of each column.
    }
}

compiles just fine for me.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文