帮助在 C++ 中声明和使用动态分配的数组
我需要声明一个数组数组,因此使用下面的代码来完成此操作:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
现在您得到的错误与您之前发布的错误完全不同。在最后的 for 循环中 -
应该是 -
在您之前发布的代码片段中,它是正确的。另外,您应该使用
delete[]
释放由new[]
获取的资源,否则会出现内存泄漏。What the error now you get is entirely different from what you posted earlier. In the final for loop -
should be -
In the snippet you earlier posted, it was correct. Also, you should deallocate resources acquired by
new[]
usingdelete[]
, else memory leak prevails.您遇到的问题是在您发布的代码的这一部分中:
在 for 循环内,您分配了一个新的
int
指针数组,但分配了maxc< 的原始类型/code> 是指针到指针类型。在这种情况下,也可以解释为 maxc 是一个指向指针数组的指针。这当然是在第一个赋值中完成的,您从 new 返回一个指向 int* 数组的指针。因此,for 循环内的语句为每个数组元素分配了错误的类型。数组元素
再次,这是不正确的。因此,您应该将循环内部更改为:maxc[x]
的类型是int
指针,这意味着它们必须指向一个或多个int
类型的对象,而不是int*
类型的元素。但是循环中的 new 运算符正在尝试分配另一个 int 指针数组,并返回指向该分配数组的指针,这意味着返回的类型是 int* *我注意到您发布的第一段代码实际上是这样做的,所以也许您遇到了您错过的简单拼写错误?
最后,当您删除此内存时,由于每一行都是使用
new
创建的,因此您必须对表示int
数组的每一行调用delete
> 的。接下来,您必须对表示指向int
数组的int
指针数组的列调用删除。因此释放所有分配的内存将如下所示:The problem you're encountering is in this section of the code you posted:
Inside the for-loop, you're allocating a new array of
int
pointers, but the original type formaxc
was of type pointer-to-pointer. In this context that could also be construed to saymaxc
is a pointer that points to an array of pointers. That of course accomplished on the first assignment, where you return a pointer fromnew
that is pointing to an array ofint*
. Therefore the statement inside the for-loop is assigning the wrong type to each array element. The type for the array elementsmaxc[x]
areint
pointers, which means that they must point to either one or more objects of typeint
, not elements of typeint*
. But yournew
operator in the loop is trying to allocate another array ofint
pointers, and return a pointer to that allocated array, meaning the type returned isint**
again, and that's not correct. So you should change the inside of your loop to read: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 calldelete
on each row representing the array ofint
's. Next, you will have to call delete on the column representing the array ofint
pointers that were pointing to the array ofint
's. So freeing all the memory allocated would look like the following:您可能在正确声明方面遇到问题,但您真正的问题不是使用标准容器来为您完成工作。
此代码:
可以对此进行更正以使其编译。
但这仍然不是好的代码,因为您没有考虑异常(如果任何分配失败,此代码将会泄漏)。在使用过程中,如果您的代码生成异常,您将泄漏所有内存。
更好的解决方案是:
如果您想要奇特,请查找 增强多维数组 这可能会给你带来一些小的性能改进(如果你正在进行矩阵乘法等)。
如果你真的想手动完成,那么我会开始学习类和 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:
Can be corrected to this to make it compile.
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:
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.
这:
对我来说编译得很好。
This:
compiles just fine for me.