Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.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++ 使用动态分配的数组获取范围外错误_C++_Arrays - Fatal编程技术网

C++ 使用动态分配的数组获取范围外错误

C++ 使用动态分配的数组获取范围外错误,c++,arrays,C++,Arrays,当我尝试将第一个数组的内容复制到DMA数组中时,我的复制函数中出现范围外错误。try-catch块是必需的 #include <iostream> #include <cstdlib> using namespace std; void show( const int a[], unsigned elements ); int * copy( const int a[], unsigned els ); void die(const string & msg)

当我尝试将第一个数组的内容复制到DMA数组中时,我的复制函数中出现范围外错误。try-catch块是必需的

#include <iostream>
#include <cstdlib>

using namespace std;

void show( const int a[], unsigned elements );
int * copy( const int a[], unsigned els );
void die(const string & msg);

int main()
{


    int arr[4] = {4, 2, 3, 6};
    show(arr, 4);
    int * newArr = copy(arr, 4);

}

void show( const int a[], unsigned elements )
{

    for (int i = 0; i < elements; i++)
        cout << a[i] << endl;

}

int * copy( const int a[], unsigned els )
{

    try
    {
        int * newArr = new int[els];
    }
    catch(const bad_alloc &)
    {
        die("Alloc Failure");
    }

    for (int i = 0; i < els; i++)
        newArr[i] = a[i];

    return newArr;
}

void die(const string & msg)
{

    cerr << "Fatal error: " << msg << endl;
    exit(EXIT_FAILURE);

}
#包括
#包括
使用名称空间std;
void show(常量int a[],无符号元素);
int*复制(常数int a[],无符号els);
空模(常量字符串和消息);
int main()
{
int-arr[4]={4,2,3,6};
显示(arr,4);
int*newArr=复制(arr,4);
}
void show(常量int a[],无符号元素)
{
for(int i=0;icout如果在
try
块内声明变量,则只能在那里访问它。可以通过将声明移到块外来解决此问题

int *newArr;

try
{
    newArr = new int[els];
}
catch(const bad_alloc &)
{
    die("Alloc Failure");
}

for (int i = 0; i < els; i++)
    newArr[i] = a[i];

在try之前定义新数组,否则它只在try块中定义。

您可以将代码更改为

int * copy( const int a[], unsigned els ) {    
    int * newArr = nullptr;
    try {
        newArr = new int[els];
    }
    catch(const bad_alloc &) {
        die("Alloc Failure");
    }

    if(newArr) { 
        for (int i = 0; i < els; i++)
             newArr[i] = a[i];
    }
    return newArr;
}
int*copy(常量int a[],无符号els){
int*newArr=nullptr;
试一试{
newArr=新整数[els];
}
捕获(常量错误分配&){
模具(“合金失效”);
}
if(newArr){
对于(int i=0;i

为了解决您的问题,只需正确初始化
newArr

异常的全部意义在于,您不需要在所有可能发生的错误发生时就处理它们,而需要在您选择的能够有意义地响应它们的地方处理它们。因此,对
try
区块:

int * copy(const int a[], unsigned els)
{
    try
    {
        int * newArr = new int[els];

        for (int i = 0; i < els; i++)
            newArr[i] = a[i];

        return newArr;
    }
    catch (const std::bad_alloc &)
    {
        die("Alloc Failure");
    }
}
int*复制(常量int a[],无符号els)
{
尝试
{
int*newArr=newint[els];
对于(int i=0;i
我认为复制函数的问题在于,新数组是一个局部变量。当您将其返回给调用方时,指针地址不正确

您最好使用两个数组作为引用传递到copy函数中,如下所示:

int& copy(int& newArray, const int& oldArray) { ... }

这里,你可以在C++中引用引用,如果你不知道它们应该是什么:

只需将catch块后面的代码移到TIGER;你需要从catch中返回0 /null pTR,因为编译器不知道它是退出的。谢谢所有帮助过的人。我希望我能给出“最佳答案”。尽管如此,我还是给了所有参与的人+1分。
int& copy(int& newArray, const int& oldArray) { ... }