我试图在C++中实现一个intvector,并得到一个“分段错误: 11”错误。我知道这与内存管理有关,考虑到我对C++来说有多新,这肯定是一个相当小的错误。我对代码进行了val差制调试,并收到了如下消息:
大小8的未初始化值的使用、大小4的无效读取、条件跳转或移动取决于未初始化的值。
我最好的猜测是,这与我如何实现数组有关。最初,我将数组存储在堆中,但将其更改为堆栈,但仍然得到了相同的错误。我已经在java中实现了一个intvector,所以我尝试在这里使用类似的逻辑,这可能是问题的一部分。
#include <iostream>
#include "IntVector.h"
#include <cmath>
using namespace std;
int num_elements = 0;
int array_size = 0;
int expansion_factor;
void IntVector::expandArray(){
int tempArr[array_size*2];
for(int i =0;i<array_size;i++){
tempArr[i] = array[i];
}
array = tempArr;
array_size = array_size * 2;
}
void IntVector::add(int val){
int tempArr[array_size];
if(array_size == num_elements){
expandArray();
array[num_elements] = val;
}
else{
for(int i = 0;i<array_size;i++){
tempArr[i] = array[i];
}
tempArr[num_elements] = val;
array = tempArr;
}
num_elements++;
}
void IntVector::remove(int index){
}
int IntVector::get(int index) const{
return index;
}
void IntVector::removeLast(){
}
void IntVector::set(int index, int val){
}
std::string IntVector::toString()const {
return "";
}
IntVector::IntVector(int initial_size){
int* array = new int[initial_size];
}
IntVector:: ~IntVector(){
delete[] array;
}
int main(){
IntVector v(0);
v.add(5);
}#ifndef INTVECTOR_H_
#define INTVECTOR_H_
using std::cout;
class IntVector {
private:
int* array;
int num_elements;
int array_size;
int expansion_factor;
void expandArray();
public:
void add(int val);
void remove(int index);
int get(int index) const;
void removeLast();
void set(int index, int val);
std::string toString() const;
IntVector(int initial_size);
~IntVector();
};
#endif 发布于 2020-04-12 02:15:59
正如评论中提到的,您对C++的理解肯定存在一些漏洞。实际上,在处理头文件时,您应该有一个main.cpp,一些其他文件。h,someotherfile.cpp。这只是避免重新定义错误的最佳实践。
访问私有变量的方式有很多错误。如果一个类有一个私有变量(甚至是公共变量),那么您不必每次更改它的值时都重新声明它。
你扩展矢量的方式有一两个主要的缺陷。如果将向量大小初始化为0,那么0*2仍然是0,因此您从未实际增加过大小。其次,当您将原始数组=设置为新数组时,新数组只是一个本地数组。这意味着,一旦函数结束,内存实际上不会被永久分配,temparr就被销毁了。
我知道这可能是很多,但如果你有任何问题,请随便问。
main.cpp
#include "IntVector.h"
int main()
{
IntVector v;
IntVector x(10);
v.push(5);
v.push(5);
v.push(5);
v.push(5);
v.push(5);
v.print();
cout << endl;
x.push(5);
x.push(5);
x.push(5);
x.push(5);
x.push(5);
x.print();
return 0;
}IntVector.h
#include <string>
#include <iostream>
using namespace std;
class IntVector {
private:
int *array;
int num_elements;
int array_size;
//int expansion_factor =; you would only need this if you plan on more than double the vector size
void expandArray(); //normally c++ array double in size each time they expand
public:
//Constructors
IntVector(); //this is a contructor for if nothing is called
IntVector(int initial_size);
//setters
void push(int val); //add
void pop(); //removelast
void remove(int index); //remove
void at(int index, int val); //set
//Getters
int at(int index);
//std::string toString(); I'm changing this to print
void print(); //will print the contents to the terminal
//Deconstructor
~IntVector();
};IntVector.cpp
#include "IntVector.h"
//constructors
IntVector::IntVector() //no arguments given
{
array = new int[0];
num_elements = 0;
array_size = 0;
}
IntVector::IntVector(int initial_size)
{
array = new int[initial_size];
num_elements = 0;
array_size = initial_size;
}
void IntVector::expandArray()
{
int *tempArr;
if(array_size == 0){
array_size = 1;
tempArr = new int[1];
} else {
//make sure to allocate new memory
//you were creating a local array which was destroy after the function was completed
//using new will allow the array to exist outside the function
tempArr = new int[array_size * 2];
}
for (int i = 0; i < array_size; i++)
{
tempArr[i] = array[i];
}
//make sure to delete the old array otherwise there is a memory leak.
//c++ doesn't have a garbage collector
delete[] array;
array = tempArr;
array_size = array_size * 2;
}
void IntVector::push(int val)
{
num_elements++;
//checking if vector needs to increase
if (array_size <= num_elements)
{
expandArray();
array[num_elements-1] = val;
}
else
{
array[num_elements-1] = val;
}
}
void IntVector::remove(int index)
{
//not sure how to implment this becuase each element has to be a number.
}
int IntVector::at(int index)
{
return array[index];
}
void IntVector::pop()
{
num_elements = num_elements-1; //not really removing it from the "vector" but it won't print out again
}
void IntVector::at(int index, int val)
{
array[index] = val;
}
void IntVector::print()
{
for (int i = 0 ; i < num_elements; i++)
{
cout << array[i] << " ";
}
cout << endl;
}
IntVector::~IntVector()
{
delete[] array;
}输出
5 5 5 5 5
5 5 5 5 5希望这些评论能有所帮助。我更改了函数的名称,以更好地匹配C++中已经存在的实际向量类。我认为将已经定义好的函数分离开来是很好的,因为您可以更好地理解它们是如何工作的,而不仅仅是如何使用它们。
如果你有任何问题,请留言
https://stackoverflow.com/questions/61165048
复制相似问题