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+中二维数组的动态分配+;_C++_Arrays_Multidimensional Array_Dynamic Allocation - Fatal编程技术网

C++ c+中二维数组的动态分配+;

C++ c+中二维数组的动态分配+;,c++,arrays,multidimensional-array,dynamic-allocation,C++,Arrays,Multidimensional Array,Dynamic Allocation,因此,我将指向n个整数的指针x定义为: int* x = new int [n]; x的值在循环中变化。在循环结束时,我将指针x存储在已初始化为以下内容的2D指针y中: int** y = new int* [n]; 我使用以下赋值表达式将指向x的指针存储在y的第I个元素中: 由于x指向内存的同一部分,但存储在x中的值不断变化,所以2D数组y中的所有元素将获得在循环最后一次执行期间存储在x中的最后一个值 我的问题: 我是否可以在每次将x的地址存储在y中时,将x从它一直指向的位置取消链接,使其

因此,我将指向n个整数的指针x定义为:

int* x = new int [n];
x的值在循环中变化。在循环结束时,我将指针x存储在已初始化为以下内容的2D指针y中:

int** y = new int* [n];
我使用以下赋值表达式将指向x的指针存储在y的第I个元素中:

由于x指向内存的同一部分,但存储在x中的值不断变化,所以2D数组y中的所有元素将获得在循环最后一次执行期间存储在x中的最后一个值

我的问题:


我是否可以在每次将x的地址存储在y中时,将x从它一直指向的位置取消链接,使其指向内存的新部分?通过这种方式,y将地址存储到内存的不同部分,因此可以检索存储在x中的答案的历史记录。另外,请让我知道这种方法(如果可能的话)是否比通过循环元素在每次迭代中用x填充y行更快。

至于什么更快,我不能说。。。我建议您尝试设置每种方法的原型。对于重新链接x,您可以使用:

x = new int[n];

我希望这有帮助

如果我的理解是正确的,您希望将数组x的所有值的历史记录保存在y中

如果不在每次迭代中用x填充y行,您的建议实际上是不可能的

你可以做的是预先分配,这样你就不会在循环中有新的分配,而是在循环之前有新的分配(你仍然会复制)

内存成本仍然是n^2,复杂性也是如此

  // without preallocation
  {
     int** y = new int*[n];
     int* oldx=0;
     for (int i=0;i<n;i++)
     {
        int* x = new int[n];
        // migrate old values in case you are modifying just a part of x
        if (oldx!=0)
        {
            memcpy(x,oldx,n*sizeof(int));
        }
        //... do some stuff with x;
        y[i] = x;
        // keep reference to copy in next iteration
        oldx=x;
     }
  }

  // with preallocation
  {
     int ** y = new int*[n];
     // preallocate memory
     for (int i=0;i<n;i++)
     {
         y[i] = new int[n];
     }
     int* oldx=0;
     for (int i=0;i<n;i++)
     {

         int* x =y[i];
         // migrate old values in case you are modifying just a part of x
         if (oldx!=0)
         {
             memcpy(x,oldx,n-sizeof(int));
         }
         // ... do stuff with x
         // keep reference to copy in next iteration
         oldx = x;
     }
  }
//不带预分配
{
整数**y=新整数*[n];
int*oldx=0;

对于(inti=0;iYou可以让x是一个新的std::数组,其中包含n个int,我想这会让您“取消链接”你可以在之后。你可以分配其中的一个来存储在另一个STD::数组Y。如果你想向C倾斜,你可以在每次准备另一个Y条目时只使用另一个X数组。但是我倾向于STD::Calpod。你使用C++来考虑使用<代码> STD::向量<代码> >代码> STD::矢量< /代码>类型。
  // without preallocation
  {
     int** y = new int*[n];
     int* oldx=0;
     for (int i=0;i<n;i++)
     {
        int* x = new int[n];
        // migrate old values in case you are modifying just a part of x
        if (oldx!=0)
        {
            memcpy(x,oldx,n*sizeof(int));
        }
        //... do some stuff with x;
        y[i] = x;
        // keep reference to copy in next iteration
        oldx=x;
     }
  }

  // with preallocation
  {
     int ** y = new int*[n];
     // preallocate memory
     for (int i=0;i<n;i++)
     {
         y[i] = new int[n];
     }
     int* oldx=0;
     for (int i=0;i<n;i++)
     {

         int* x =y[i];
         // migrate old values in case you are modifying just a part of x
         if (oldx!=0)
         {
             memcpy(x,oldx,n-sizeof(int));
         }
         // ... do stuff with x
         // keep reference to copy in next iteration
         oldx = x;
     }
  }