帮助在C++中声明和使用动态分配的数组
Help with declaring and using dynamically allocated Array in 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.
}
}
您现在得到的错误与您之前发布的错误完全不同。在决赛中 for 循环 -
maxc[i] = new int*[n];
应该是——
maxc[i] = new int[n];
在您之前发布的片段中,这是正确的。此外,您应该使用 delete[]
解除分配new[]
获取的资源,否则内存泄漏占上风。
您遇到的问题在您发布的代码的这一部分中:
int ** maxc = new int *[proc_num];
for (int i = 0; i < proc_num; i++)
{
maxc[i] = new int*[n];
}
在 for 循环中,您正在分配一个新的 int
指针数组,但 maxc
的原始类型是指针到指针的类型。 在此上下文中,也可以解释为maxc
是指向指针数组的指针。 这当然是在第一个作业中完成的,在该作业中,您从指向int*
数组的new
返回一个指针。 因此,for-loop中的语句为每个数组元素分配了错误的类型。 数组元素的类型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
指针数组的列调用 delete。 因此,释放所有分配的内存将如下所示:
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;
正确获取声明时遇到问题,但真正的问题不是使用标准容器为您完成工作。
此代码:
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
如果你想花哨,你可以查找 boost 多维数组,这可能会给你一些小的性能改进(如果你正在做矩阵乘法等)。
如果你真的想手工做,那么我会开始学习课程和 RAII。
这个:
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.
}
}
编译对我来说很好。
- 这是使用回溯的 nqueen 问题,但我使用了动态 2d 数组,我的程序编译良好,但不返回任何输出
- 动态更改数组的大小并读取值.(无矢量)
- 检测到堆损坏:在正常块 c++ 动态 2D 数组之后
- 动态维度数组的 C++ 别名指针
- C++ 在析构函数调用之前删除的动态成员数组
- 从类 c++ 动态创建数组
- 具有静态 std::array 的神经网络比使用动态 C 数组的神经网络慢
- "Error reading character of string" 为哈希动态增长数组的问题 (C++)
- 为什么我们在传递动态 2D 数组时不需要列数?
- 如何解决在使用动态 2D 数组进行矩阵乘法的 MPI 进行并行编程时的问题
- 使用动态布尔数组时出现问题
- C++ 在类中删除动态 2D 数组时不断"Aborted (core dumped)"
- 如何在C 中动态输入数组
- 关于删除动态对象数组
- 在C 中动态更改数组大小
- 使用动态指针数组进行动态对象分配 - 使用什么删除?
- 动态字符串数组分配错误
- 函数中的动态 2D 数组分配并将其返回给主数组
- c++动态字符串数组分配
- 使用字符串或字符动态分配(数组)