Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/141.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++_Pointers_Dynamic Arrays - Fatal编程技术网

C++ 使用指针复制和增长现有数组

C++ 使用指针复制和增长现有数组,c++,pointers,dynamic-arrays,C++,Pointers,Dynamic Arrays,在测试“成长”和“子阵列”函数时,我未能达到预期输出。我尝试在函数和main()中来回解引用。我想知道是不是我的内存分配有什么问题导致了这个错误。我非常困惑,希望有人能看到我遗漏的东西,谢谢 #include <iostream> #include <iomanip> using namespace std; bool isSorted(int *arr, int size){ for(int index = 0; index < size - 1; in

在测试“成长”和“子阵列”函数时,我未能达到预期输出。我尝试在函数和main()中来回解引用。我想知道是不是我的内存分配有什么问题导致了这个错误。我非常困惑,希望有人能看到我遗漏的东西,谢谢

#include <iostream>
#include <iomanip>
using namespace std;

bool isSorted(int *arr, int size){
    for(int index = 0; index < size - 1; index ++){
        if(*(arr + index) > *(arr + index + 1)){
            return false;
        }

    }
    return true;
}


double chain (int totalInches, int *feet, int *inches){
    *feet = totalInches/12;
    *inches = totalInches%12;
    return *(feet)*3.49 + *(inches)*.30;
}

int *grow (int *arr, int size){
    int *newArray;
    newArray = new int[size*2]; //alocate new array
    for(int i = 0; i < size*2; i+=2){
        *(newArray + i) = *(arr+i);
        *(newArray + i + 1) = *(arr+i);    
    }
    return newArray;

}

int *duplicateArray (int *array, int size) {

    int *newArray;
    if (size <= 0)
    return NULL;
    newArray = new int [size]; //allocate new array
    for (int index = 0; index < size; index++){
        newArray[index] = array[index]; //copy to new array
    }
    return newArray;
}

int *subArray( int *array, int start, int length){
    int *result = duplicateArray(array,5);
    return result;
}

void showArray( int *arr, int size){
    for(int  i = 0; i < size; i ++)
    {
        cout << *(arr + i) << " ";
    }
}
int main(){

    int size = 8;
    int testArray[] = {1,2,3,4,5,6,7,8};
    cout << "testing isSorted: " << endl;
    cout << "test data array 1: ";
    showArray(testArray, size);
    cout << endl;
    cout << "Expected result: true" << endl;
    cout << "Actual result: " << boolalpha << isSorted(testArray, size);
    cout << endl;
    int testArray2[]= {8,7,6,5,4,3,2,1};
    cout << "test data array 2: ";
    showArray(testArray2, size);
    cout << endl;
    cout << "Expected result: false" << endl;
    cout << "Actual result: " << boolalpha << isSorted(testArray2, size);
    cout << endl << endl << endl;
    int chainTest = 53;
    cout << "Checking chain for 53 inches: " << endl;
    cout << "Expected result: 15.46 " << "  " << "feet: 4 " <<
    "  " << "inches: 5"<< endl;
    int in;
    int ft;
    cout << "Actual results : " << chain(chainTest,&ft,&in);
    cout << "   " << "feet: " << ft << "   " << "inches: " << in << endl;
    cout << endl << endl;
    cout << "testing grow: " << endl;
    cout << "test data 1: ";
    showArray(testArray, size);
    cout << endl;
    cout << "Expected result: 1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 " << endl;
    cout << "Actual results: " << *(grow(testArray, size));
    cout << endl << endl;
    cout << "testing subArray:" << endl;
    cout << "test data: ";
    showArray(testArray, size);
    cout << endl;
    int start = 5;
    int length = 3;
    cout << "start: " << start << " " << "length: " << length << endl;
    cout << "Expected result: " << "6 7 8" << endl;
    cout << "Actual result:   " << *(subArray(testArray, start, length));
    cout << endl;

    return 0;
}
#包括
#包括
使用名称空间std;
布尔ISORTED(整数*arr,整数大小){
对于(int index=0;index*(arr+索引+1)){
返回false;
}
}
返回true;
}
双链(整数总英寸,整数*英尺,整数*英寸){
*英尺=总英寸/12;
*英寸=总英寸%12;
返回*(英尺)*3.49+*(英寸)*.30;
}
int*grow(int*arr,int size){
int*newArray;
newArray=newint[size*2];//计算新数组
对于(int i=0;i如果(size)p>一次循环两个是很好的,但是它会阻止你选择原始数组中的每个元素,使你跳过偶数。检查你的循环并考虑使用两个计数器,或者如果你想修改for循环到

for(int i = 0; i < size*2; i+=2){
    *(newArray + i) = *(arr+i/2);
    *(newArray + i + 1) = *(arr+i/2);    
}

实际上,您正在将数组加倍,但只打印第一个元素,因为您正在取消对int*的引用。要打印所有元素,请编写一个循环并打印所有元素


这里也有太多内存泄漏。请在使用完内存后释放内存。您已经了解了删除[]运算符。

谢谢!说得好。我实际上没有注意到我没有实际输出整个阵列。难怪!就子阵列而言,它是提供的。所以,没有改变。我调用了这两个函数,让它们完成工作,然后是显示函数。我得到了一个更好看的输出,但还没有完全达到。我在grow()函数中似乎没有按预期复制t。离题:代码泄漏内存。在grow中分配并返回的缓冲区从未分配给任何对象,因此您几乎不可能再次找到它并
delete[]
it。非常感谢各位!我也一定会处理泄漏问题。谢谢,先生!我一定会处理内存泄漏问题。
showArray(grow(testArray, size),size*2);