c語言100位整數變量聲明
Here, we will learn how we can declare an integer variable dynamically and how to print address of declared memory block?
在這里,我們將學習如何動態聲明整數變量,以及如何打印聲明的內存塊的地址?
In C++ programming, we can declare memory at run time for any type variable like char, int, float, integer array etc. To declare memory at run time (dynamically), we use new operator. I would recommend reading about new operator first.
在C ++編程中,我們可以在運行時為char,int,float,integer array等任何類型的變量聲明內存。要在運行時(動態)聲明內存,請使用new運算符。 我建議先閱讀有關新運算符的信息 。
Consider the following program:
考慮以下程序:
#include <iostream>
using namespace std;
int main()
{
//integer pointer declartion
//It will contain the address of dynamically created
//memory blocks for an integer variable
int *ptr;
//new will create memory blocks at run time
//for an integer variable
ptr=new int;
cout<<"Address of ptr: "<<(&ptr)<<endl;
cout<<"Address that ptr contains: "<<ptr<<endl;
//again assigning it run time
ptr=new int;
cout<<"Address of ptr: "<<(&ptr)<<endl;
cout<<"Address that ptr contains: "<<ptr<<endl;
//deallocating...
delete (ptr);
return 0;
}
輸出量 (Output)
Address of ptr: 0x7ffebe87bb98Address that ptr contains: 0x2b31e4097c20Address of ptr: 0x7ffebe87bb98Address that ptr contains: 0x2b31e4098c50
Here we are declaring an integer pointer ptr and printing the address of ptr by using &ptr along with stored dynamically allocated memory block.
在這里,我們聲明的整數指針PTR以及通過使用&PTR與存儲動態分配的存儲塊沿著打印PTR的地址。
After that we are declaring memory for integer again and printing the same.
之后,我們再次聲明整數的內存并打印該整數。
From this example - we can understand memory blocks are allocating dynamically, each time different memory blocks are storing in the pointer ptr.
從這個例子中,我們可以理解每次將不同的存儲塊存儲在指針ptr中時,存儲塊都是動態分配的。
翻譯自: https://www.includehelp.com/code-snippets/cpp-declare-integer-variable-dynamically-print-the-memory-addresses.aspx
c語言100位整數變量聲明