Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 变量,用于在对象设置为null后声明对象的堆大小设置为null?_C++ - Fatal编程技术网

C++ 变量,用于在对象设置为null后声明对象的堆大小设置为null?

C++ 变量,用于在对象设置为null后声明对象的堆大小设置为null?,c++,C++,我有一个变量,它是堆上的数组。我在堆上声明了这个数组,使用和一个我设置的整数值乘以对象的大小。我在代码中遇到问题,当我将对象设置为null时,用于为其创建空间的整数设置为0 int numOfNodes; int numOfEdges; FILE *file; file = fopen("input.txt", "r"); fscanf(file, "%d", &numOfNodes); //numOfNodes value is read in correct here

我有一个变量,它是堆上的数组。我在堆上声明了这个数组,使用和一个我设置的整数值乘以对象的大小。我在代码中遇到问题,当我将对象设置为null时,用于为其创建空间的整数设置为0

int numOfNodes;
int numOfEdges;

FILE *file;
file = fopen("input.txt", "r");
fscanf(file, "%d", &numOfNodes); //numOfNodes value is read in correct here             
fscanf(file, "%d", &numOfEdges);//numOfEdges value is read in correct here

ELEMENT **T = (ELEMENT**) new ELEMENT()//setting space on the heap for an array of linked lists 


for(int i=0; i<numOfNodes; i++){//This line is supposed to initialize all the Linked List inside the array to NULL. However on the second iteration the value for int numOfNodes changes to 0.
T[i]=NULL}


不确定发生了什么,因为链表数组和int numOfNodes之间没有任何关系,除了numOfNodes被用作一个数字以在堆上分配适当的空间之外

编辑:使用new而不是malloc,但仍然遇到相同的问题

int numOfNodes= 0; // good practice: always initialize
int numOfEdges= 0; // ditto

FILE* file= fopen("input.txt", "r"); // ditto
// you should check fscanf return value
fscanf(file, "%d", &numOfNodes); //numOfNodes value is read in correct here             
fscanf(file, "%d", &numOfEdges); //numOfEdges value is read in correct here
您还应该检查这些数字是否大于0

这很糟糕,请不要执行以下操作:

ELEMENT **T = (ELEMENT**) new ELEMENT()
你只需要一个元素的内存, 你得到的内存是ELEMENT*。 通过强制将强制转换为元素**,您不仅 做错事,但告诉编译器‘相信我,我知道我在做什么’。 作为C++的一般规则, 如果你在选演员,你就做了坏事。 通常有更好的方法。 不要使用老式的演员阵容,最好 重新解释投射、静态投射、动态投射和常量投射

如果需要的是元素*的数组, 那么你应该问:

typedef ELEMENT* pElement;
ELEMENT** T= new pElement[numOfNodes]; // delete with delete[] T;
// or:
ELEMENT** T= malloc(sizeof(ELEMENT*) * numOfNodes); // delete with free(T);
现在您可以执行以下操作:

for (int i= 0; i < numOfNodes; i++) {
  // This line is supposed to initialize all the Linked List inside the array to NULL.
  // However on the second iteration the value for int numOfNodes changes to 0.
  T[i]= NULL; // nullptr on C++11
}
您还可以使用自动:

请注意,您不仅要删除显式强制转换 但也包括隐式类型转换

您也可以在C中使用++

std::vector, which grows as needed
std::vector<ELEMENT*> T(numOfNodes, NULL);
更好的是,在现代C++中:

std::vector<unique_ptr<ELEMENT> > T(numOfNodes);
使用2初始化,并正确删除使用1初始化的旧元素

T[10]->doSomething();

将正常工作。

因为这是C++不使用MALOC,使用新的。您有一个双指针,但您没有为指针分配空间,而是为结构本身分配空间。如果您使用new,并且会得到正确的错误消息,那么这将不起作用。malloc的结果就是ELEMENT*。它是一个struct元素数组,因此一个简单指针就足够了。使用简单指针,您必须逐个初始化元素结构的所有字段。只使用std::数组或std::vector会简单得多。
T[10]= new ELEMENT();
std::vector<unique_ptr<ELEMENT> > T(numOfNodes);
T[10].reset(new ELEMENT(1)); // will set the pointer to a new element initialized with 1
T[10].reset(new ELEMENT(2)); // will set the pointer to a another new element
T[10]->doSomething();