在 C++ 中使用引用指针类型作为参数
using reference pointer type as Paramter in c++
我在使用引用指针类型作为参数时遇到了一些问题。
首先,这是我的代码。
#include<iostream>
using namespace std;
void test(int*& ptr);
int main(void)
{
int* ptr = new int[1];
ptr[0] = 100;
cout << ptr[0];
test(ptr);
cout << ptr[0] << '\t' << ptr[1];
}
void test(int*& ptr)
{
int* tmp = new int[2];
tmp[0] = 1;
tmp[1] = 2;
int* tmp2 = ptr;
ptr = tmp;
cout << ptr[0] << '\t' << ptr[1];
delete[]tmp2;
}
编译此代码时,输出为
100
1 2
这个输出是我想要得到的
但是当我调试这段代码时,
Exception thrown: read access violation.
发生此错误。
如何避免这个错误,我的错是什么? :(
reallcoate参数内存不使用引用类型怎么办?
引用不是问题。可怕的是你的动态内存管理被破坏了。
#include <iostream>
using namespace std;
void test(int *&ptr);
int main(void)
{
int *ptr = new int[1]; // allocates a sequence of 1
ptr[0] = 100;
cout << ptr[0];
test(ptr); // sends pointer to sequence by reference to test
cout << ptr[0] << '\t' << ptr[1];
}
void test(int *&ptr)
{
int *tmp = new int[2]; // allocates a sequence of size 2
tmp[0] = 1;
tmp[1] = 2;
int *tmp2 = ptr; // stores original passed-in pointer to tmp2
ptr = tmp; // assigns new sequence pointer to reference argument (leaks original)
cout << ptr[0] << '\t' << ptr[1];
delete[] ptr; // deletes the new sequence. (leaves dangling pointer)
}
您似乎试图做的是:
#include <iostream>
using namespace std;
void test(int *&ptr);
int main(void)
{
int *ptr = new int[1];
ptr[0] = 100;
cout << ptr[0] << '\n';
test(ptr);
cout << ptr[0] << '\t' << ptr[1] << '\n';
delete [] ptr;
}
void test(int *&ptr)
{
delete [] ptr; // delete original sequence
ptr = new int[2]; // create new sequence
ptr[0] = 1;
ptr[1] = 2;
}
停止使用原始指针
或者,使用智能指针来管理它。
#include <iostream>
#include <memory>
using namespace std;
void test(std::unique_ptr<int[]>& ptr);
int main(void)
{
std::unique_ptr<int[]> ptr = std::make_unique<int[]>(1);
ptr[0] = 100;
cout << ptr[0] << '\n';
test(ptr);
cout << ptr[0] << '\t' << ptr[1] << '\n';
}
void test(std::unique_ptr<int[]>& ptr)
{
ptr = std::make_unique<int[]>(2);
ptr[0] = 1;
ptr[1] = 2;
}
或者更好的是,只需使用 std::vector<int>
我在使用引用指针类型作为参数时遇到了一些问题。
首先,这是我的代码。
#include<iostream>
using namespace std;
void test(int*& ptr);
int main(void)
{
int* ptr = new int[1];
ptr[0] = 100;
cout << ptr[0];
test(ptr);
cout << ptr[0] << '\t' << ptr[1];
}
void test(int*& ptr)
{
int* tmp = new int[2];
tmp[0] = 1;
tmp[1] = 2;
int* tmp2 = ptr;
ptr = tmp;
cout << ptr[0] << '\t' << ptr[1];
delete[]tmp2;
}
编译此代码时,输出为
100
1 2
这个输出是我想要得到的
但是当我调试这段代码时,
Exception thrown: read access violation.
发生此错误。
如何避免这个错误,我的错是什么? :(
reallcoate参数内存不使用引用类型怎么办?
引用不是问题。可怕的是你的动态内存管理被破坏了。
#include <iostream>
using namespace std;
void test(int *&ptr);
int main(void)
{
int *ptr = new int[1]; // allocates a sequence of 1
ptr[0] = 100;
cout << ptr[0];
test(ptr); // sends pointer to sequence by reference to test
cout << ptr[0] << '\t' << ptr[1];
}
void test(int *&ptr)
{
int *tmp = new int[2]; // allocates a sequence of size 2
tmp[0] = 1;
tmp[1] = 2;
int *tmp2 = ptr; // stores original passed-in pointer to tmp2
ptr = tmp; // assigns new sequence pointer to reference argument (leaks original)
cout << ptr[0] << '\t' << ptr[1];
delete[] ptr; // deletes the new sequence. (leaves dangling pointer)
}
您似乎试图做的是:
#include <iostream>
using namespace std;
void test(int *&ptr);
int main(void)
{
int *ptr = new int[1];
ptr[0] = 100;
cout << ptr[0] << '\n';
test(ptr);
cout << ptr[0] << '\t' << ptr[1] << '\n';
delete [] ptr;
}
void test(int *&ptr)
{
delete [] ptr; // delete original sequence
ptr = new int[2]; // create new sequence
ptr[0] = 1;
ptr[1] = 2;
}
停止使用原始指针
或者,使用智能指针来管理它。
#include <iostream>
#include <memory>
using namespace std;
void test(std::unique_ptr<int[]>& ptr);
int main(void)
{
std::unique_ptr<int[]> ptr = std::make_unique<int[]>(1);
ptr[0] = 100;
cout << ptr[0] << '\n';
test(ptr);
cout << ptr[0] << '\t' << ptr[1] << '\n';
}
void test(std::unique_ptr<int[]>& ptr)
{
ptr = std::make_unique<int[]>(2);
ptr[0] = 1;
ptr[1] = 2;
}
或者更好的是,只需使用 std::vector<int>