Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/128.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++;_C++_Arrays_Dynamic - Fatal编程技术网

C++ 程序停止工作并给出垃圾值C++;

C++ 程序停止工作并给出垃圾值C++;,c++,arrays,dynamic,C++,Arrays,Dynamic,我正在为DynamicArray做一个程序,在这里我实现了三种方法。向后推送(参数…),向后弹出(参数…)和打印数组(参数…) 但是当我增加数组的大小时,它会在该索引处给出垃圾值,如果我为超过15个值运行代码,它就会停止工作 #include<iostream> using namespace std; void push_back(unsigned int value,unsigned int *array,int &arraySize,int &totalArr

我正在为DynamicArray做一个程序,在这里我实现了三种方法。向后推送(参数…),向后弹出(参数…)和打印数组(参数…)

但是当我增加数组的大小时,它会在该索引处给出垃圾值,如果我为超过15个值运行代码,它就会停止工作

#include<iostream>
using namespace std;

void push_back(unsigned int value,unsigned int *array,int &arraySize,int &totalArrayItems ){
  if(arraySize>totalArrayItems){
    array[totalArrayItems]=value;
    totalArrayItems=totalArrayItems+1;
    cout<<"Current size: "<<arraySize<<endl;
  }
  else if(arraySize==totalArrayItems){
      cout<<"Adding the Size of Array... \n";

    unsigned int *tempArray=new unsigned int[arraySize+5];//making new array as asked in requirements
    for(int i=0;i<arraySize;i++){

      tempArray[i]=array[i];
      cout<<"Value in Temp : "<<tempArray[i]<<endl;
    }
    unsigned int *temp=array;
    array=tempArray;
    arraySize=arraySize+5;
    cout<<"Saving "<<value<<" at Position: "<<totalArrayItems<<endl;
    array[totalArrayItems]=value;//adding the new value
    cout<<"Value in Temp "<<totalArrayItems<<" : "<<tempArray[totalArrayItems]<<endl;
    totalArrayItems=totalArrayItems+1;
    cout<<"Updated size: "<<arraySize<<"Current value: "<<value<<endl;
    delete []temp;//delete old array

  }


}
void pop_back(unsigned int *array,int &arraySize,int &totalArrayItems){
  if(totalArrayItems>0){
    totalArrayItems=totalArrayItems-1;
    if(totalArrayItems<(arraySize-5)){
        unsigned int *tempArray=new unsigned int[arraySize-5];
        for(int i=0;i <= totalArrayItems;i++){
          tempArray[i]=array[i];
        }
        unsigned int *temp=array;
        array=tempArray;
        delete[] temp;
        arraySize=arraySize-5;
    }
  }
}
  void printArray(unsigned int *array,int totalArrayItems){
    for(int i=0;i<totalArrayItems;i++){
      cout<<"Value at "<<i<<" : "<<array[i]<<endl;
    }
  }
#包括
使用名称空间std;
void push_back(无符号int值、无符号int*数组、int和arraySize、int和totalArrayItems){
如果(阵列大小>总阵列项目){
数组[totalArrayItems]=值;
totalArrayItems=totalArrayItems+1;

push_back
中的cout
unsigned int*array
是一个局部变量。当您为它指定一个新值(指向新内存位置的新指针)时,它不会以您从中调用它的任何方法更新
array
的值


如果您希望重新分配继续,请将指针传递到
int*
int**

您能将您的程序设置为一个,并清楚地说明预期的输出是什么以及它将变成什么吗?对不起,整段代码。问题是我动态分配内存并将该内存分配回同一个指针。这是错误的不在该索引处存储值并打印垃圾值。如果我运行此程序的值超过15个,它将停止工作。我确信这是您当前的分析,但如果您从程序中逐位删除,最终您将找到真正的原因,或者您需要更好地将其向下移动。我以前从未通过引用传递指针,因为我认为指针名称本身就是一个引用,但在这里我应该通过引用来传递它。我以前不知道它,谢谢你的提示,我会记住的。:)这对我来说是一个新的学习。谢谢:)非常感谢!它起了作用,我现在接受指针的引用,它起了作用。