简体   繁体   English

动态内存分配-C ++

[英]Dynamic memory allocation - C++

I'm in the process of teaching myself C++ and am currently learning about dynamically allocating memory. 我正在自学C ++,目前正在学习动态分配内存。 Here's the code that I'm currently working with: 这是我目前正在使用的代码:

#include <iostream>

using namespace std;

int *memAdd(int* dyn_Point, int *lenPoint){
   int *new_Dyn_Point = new int[*lenPoint * 2];
   *lenPoint = *lenPoint * 2;
   for(int i = 0; i < *lenPoint; i++){
       new_Dyn_Point[i] = dyn_Point[i];
   }

   delete lenPoint;
   delete[] dyn_Point;
   return new_Dyn_Point;

}

int main(){

   int len = 2;
   int *lenPoint = &len;
   int current = 0;
   int val;
   int *dyn_Point = new int[len];


   cout << "Input a value for point 1: ";
   cin >> val;
   dyn_Point[current] = val;


   while(val > 0){
      current++;

      cout << "Input a value for point " << current+1 <<" (0 to exit): ";
      cin >> val;

      if(current+1 == len){
         *dyn_Point = *memAdd(dyn_Point, lenPoint);
         cout << len;
      }

      dyn_Point[current] = val;


   }

   for(int i = 0; i < len; i++){
     cout << &dyn_Point[i] << "\n";
     cout << dyn_Point[i] << "\n\n";

 }
 delete[] dyn_Point;

} }

My Question: When adding more memory does it have to increment by a certain value? 我的问题:添加更多内存时,它必须增加一定值吗?

Whenever I start with a value in my "len" variable that's not 2 my program will crash either as soon as I try and allocate more memory or after more memory has been allocated and even more has to be added a second time. 每当我在“ len”变量中的值不为2开头时,程序将在我尝试分配更多的内存后立即崩溃,或者在分配了更多的内存后甚至第二次添加更多的内存时崩溃。

Is this how it's supposed to be or am I missing something entirely here? 这是应该的样子还是我在这里完全错过了什么?

Your while loop need a break 您的while循环需要break

while() {

    //do your steps
    break;
}

In function memAdd following changes required : memAdd函数中, memAdd以下更改:

// *lenPoint = *lenPoint * 2; 

// above line need to be commented else it will trouble the loop condition by causing over flow: //上面的行需要注释,否则会引起溢出,从而给循环条件带来麻烦:

    for(int i = 0; i < (*lenPoint-1); i++){

// for loop is corrected to resolve the over flow //纠正for循环以解决溢出问题

Below delete is unnecessary since you didn't allocate memory using this variable 下面的删除是不必要的,因为您没有使用此变量分配内存

// delete lenPoint;

For your question: When adding more memory does it have to increment by a certain value? 对于您的问题:添加更多内存时,是否必须增加一定值?

There is no hard and hard fast rule on this regard. 在这方面没有硬性规定。 std::vector<> double its size( memory allocation) whenever it is needed more memory. 每当需要更多内存时,std :: vector <>的大小都会增加一倍(内存分配)。 It is slightly different than your approach. 它与您的方法略有不同。 You are doubling memory before reaching the allocated upper limit. 在达到分配的上限之前,您正在加倍内存。

**Edit**

Compiled full code as OPs request 根据OP要求编译完整代码

#include <iostream>

using namespace std;

int *memAdd(int* dyn_Point, int *lenPoint){
   int *new_Dyn_Point = new int[*lenPoint * 2];
  // *lenPoint = *lenPoint * 2;
   for(int i = 0; i < (*lenPoint-1); i++){
       new_Dyn_Point[i] = dyn_Point[i];
   }

   //delete lenPoint;
   delete[] dyn_Point;
   return new_Dyn_Point;

}

int main(){

   int len = 2;
   int *lenPoint = &len;
   int current = 0;
   int val;
   int *dyn_Point = new int[len];


   cout << "Input a value for point 1: ";
   cin >> val;
   dyn_Point[current] = val;


   while(val > 0){
      current++;

      cout << "Input a value for point " << current+1 <<" (0 to exit): ";
      cin >> val;

      if(current+1 == len){
         *dyn_Point = *memAdd(dyn_Point, lenPoint);
         cout << len<<"\n";
      }

      dyn_Point[current] = val;
     break;

   }

   for(int i = 0; i < len; i++){
     cout << dyn_Point[i] << "\n";
     cout << &dyn_Point[i] << "\n\n";

 }
 delete[] dyn_Point;

}

"My Question: When adding more memory does it have to increment by a certain value?" “我的问题:添加更多内存时,它必须增加一定值吗?”

Of course you have to manage your memory allocations using a design like this. 当然,您必须使用这样的设计来管理内存分配。

In particular you should obey the Rule of Three (Five) , and to copy all of the existing elements, when reallocating memory to increase to the necessary amount. 特别是,在重新分配内存以增加到必要的数量时,应遵循“三(五)规则”并复制所有现有元素。


The much better choice than doing it all yourself (which is likely to be error prone), is to use a 比自己动手做的更好的选择(这很容易出错)是使用

std::vector<int> dyn_point;

and/or alike in your class. 和/或同班同学。

Memory management is taken care of in the container implementations , and you don't need to bother about it. 内存管理是在容器实现中进行的 ,您无需为之烦恼。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM