写入位置0x00000000时发生访问冲突.指针问题
Access violation writing location 0x00000000. problems with pointers
我在编写一些赋值代码时遇到问题。该项目基于大学生在线竞赛问题档案。
这是头文件:
#include <iostream>
#include <cmath>
using namespace std;
class Vito
{
public:
Vito(int relative_count); //constructor
~Vito(); //destructor
int min(); //finds minimum "distance" between vito's relatives
private:
int *streets; //pointer to array of street numbers
int sum(int index); //generates sum of distances for each of vito's relatives
void getStreetNums(); //retrieves street numbers from user
int relatives; //used globaly to set number of repititions
};
Vito::Vito(int relative_count = 0)
{
int *streets = new int[relative_count]; //allocates memory for array streets
relatives = relative_count;
getStreetNums();
}
Vito::~Vito()
{ delete [] streets; } //releases memory used by class
void Vito::getStreetNums()
{
cout << "Enter all street numbers, seperated by a space: ";
int street_num;
for (int i = 0; i < relatives; i++)
{
cin >> street_num;
streets[i] = street_num;
}
}
int Vito::min()
{
int MIN = 65546, test_distance; //initialized to maximum possible value for an integer in C++
for (int i = 0; i < relatives; i++)
{
test_distance = sum(i);
if( MIN > test_distance )
{ MIN = test_distance; }
}
return MIN;
}
int Vito::sum(int index)
{
int SUM = 0, street_num;
street_num = *(streets+index); //set value for determining distances between one house and the others
for (int i = 0; i < relatives; i++)
{ SUM += abs( street_num - streets[i] ); }
return SUM;
}
主要内容如下:
#include <iostream>
#include "proj_02.h"
using namespace std;
int main()
{
int relatives, tests;
cout << "This program will supply a minimum distance between homes based on a given number of relatives and street numbers their homes are on. All entered values must be integers." << endl << endl;
cout << "Enter how many tests will be run: ";
cin >> tests;
for (int i = 0; i < tests; i++)
{
cout << "Enter how many relatives will be used in test " << i+1 << ": ";
cin >> relatives;
Vito family(relatives);
cout << "For this case, the minimum distance between each relatives house compared to each other is: " << family.min() << endl << endl;
}
}
执行后,我收到一条错误消息,上面写着"0xC0000005:写入位置0x00000000的访问违规。"
void Vito::getStreetNums()
{
cout << "Enter all street numbers, seperated by a space: ";
int street_num;
for (int i = 0; i < relatives; i++)
{
cin >> street_num;
streets[i] = street_num;
}
}
调试显示为街道设置了一个空内存地址,但我在构造函数的前面为它分配了内存。有人能解释一下这里发生了什么吗?
Vito::Vito(int relative_count = 0)
{
int *streets = new int[relative_count]; //allocates memory for array streets
在构造函数中,您定义了一个局部变量streets
,并为其分配内存。局部变量streets
隐藏类成员streets
。更改为:
Vito::Vito(int relative_count = 0)
{
streets = new int[relative_count]; //allocates memory for array streets
相关文章:
- C++尝试深度复制唯一指针时出现内存访问冲突
- 尝试通过共享指针使用变量时读取访问冲突
- 使用智能指针读取访问冲突
- C 指针转换会导致内存访问冲突
- C++:读取 Lambda 捕获的指针时出现访问冲突
- 初始化指针时出现写入访问冲突
- 不能对基于数组的指针使用数组运算符(读取访问冲突)
- C++ - 智能指针 - 访问冲突读取位置0xDDDDDDDD
- 调用指针时违反 C++ Typedef 访问冲突
- 为什么未初始化的指针会导致接近 0 的 mem 访问冲突
- 执行指针时发生C++访问冲突
- C++:使用std::ifstream读取二进制文件后删除缓冲区/指针时发生访问冲突
- 指针未指向 NULL 时的访问冲突
- 指针导致访问冲突
- 在标头中声明指针会导致访问冲突
- 字符双指针程序中的访问冲突
- 使用智能指针时发生访问冲突
- 使用指向对象实例的指针列表的访问冲突
- 访问冲突:指针错误
- 写入位置0x00000000时发生访问冲突.指针问题