简体   繁体   English

资深C ++程序员需要一些帮助

[英]Fellow C++ programmer needs some help

 // rememb-o-matic
 #include <iostream>
 #include <new>
 using namespace std;

 int main ()

 {
   int i,n;
   int * p;
   cout << "How many numbers would you like to type? ";
   cin >> i;
   p= new (nothrow) int [i];
   if (p == 0)
     cout << "Error: memory could not be allocated";
   else
   {
     for (n=0; n<i; n++)
     {
       cout << "Enter number: ";
       cin >> p[n];
     }
     cout << "You have entered: ";
     for (n=0; n<i; n++)
       cout << p[n] << ", ";
     delete[] p;
   }
   return 0;
 }

Now, 现在,

why is their a bracket enclosed in variable i? 为什么将他们的括号括在变量i中?

 p= new (nothrow) int [i];

why are their 2 for statements and what does a for statement do exactly? 为什么他们的2个for语句以及for语句到底做什么?

Why is it deleting []p instead of the variable p ? 为什么要删除[] p而不是变量p

why is their a bracket enclosed in variable i? 为什么将他们的括号括在变量i中? &
Why is it deleting []p instead of the variable p? 为什么删除[] p而不是变量p?

 p= new (nothrow) int [i];

Dynamically Allocates an int array of i elements . 动态分配i元素的int数组。

 delete []p;

Deletes the dynamically allocated array. 删除动态分配的数组。


Basic Rules of dynamic allocation in C++ are: C ++中动态分配的基本规则是:

  • If you use new to dynamically allocate memory, Use delete to deallocate it. 如果使用new动态分配内存,请使用delete取消分配内存。
  • If you use new [] to dynamically allocate memory, Use delete [] to deallocate it. 如果使用new []动态分配内存,请使用delete []取消分配内存。

Also, note that you are using nothrow version of the new operator, it basically returns a null if some error condition and does not throw an exception, this allows the C++ code to be compatible with the legacy c code which uses null check after memory allocations(malloc returns null on failure). 另外,请注意,您使用的是new操作符的nothrow版本,如果出现某些错误情况,它基本上会返回null且不会引发异常,这使C ++代码与在内存分配后使用null检查的传统c代码兼容。 (失败时,malloc返回null)。


what does a for statement do exactly? for语句的作用是什么?

A for statement is a conditional loop construct , It keeps on executing the loop untill the condition remains true. for语句是一个条件循环构造 ,它会继续执行循环,直到condition成立为止。 The basic syntax of for loop is: for循环的基本语法为:

for(;condition;)
{ 
    //doSomething
}

why are their 2 for statements? 他们的2为什么要声明?
The first for loop in your code gets the input from the user. 代码中的第一个for loop从用户那里获取输入。 It takes the input for i times. 它需要输入i次。

The second for loop prints out the contents of the array. 第二个for loop打印出数组的内容。


Suggest you to pick up a good C++ book and read through the basics to understand more. 建议您选择一本不错的C ++书籍,并通读基础知识以了解更多信息。

delete []表示您正在将数组释放回系统,if语句的无括号是可选的,仅表示如果为true,则将执行第一行,我认为您确实需要学习C ++和编程的一些基础知识总的来说,买一本好书或谷歌教程有很多好书

It is just C++ syntax. 这只是C ++语法。

  1. new int [i] means that we want to allocate memory to store i integers. new int [i]表示我们要分配内存以存储i个整数。
  2. delete [] p deletes memory bind to p. delete [] p删除绑定到p的内存。 If [] are omitted, compiler assumes that p points to single instance of *p type. 如果省略[] ,则编译器假定p指向*p类型的单个实例。 If instruction includes [] compiler tries to remove array of elements. 如果指令包含[]编译器将尝试删除元素数组。

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

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