gpt4 book ai didi

c++ - 静态内存中数组的大小可以在 C++ 运行时更改吗?怎么来的?

转载 作者:太空狗 更新时间:2023-10-29 19:53:29 24 4
gpt4 key购买 nike

<分区>

我已经从这里阅读了这一段:http://www.cplusplus.com/doc/tutorial/dynamic/

You could be wondering the difference between declaring a normal array and assigning dynamic memory to a pointer, as we have just done. The most important difference is that the size of an array has to be a constant value, which limits its size to what we decide at the moment of designing the program, before its execution, whereas the dynamic memory allocation allows us to assign memory during the execution of the program (runtime) using any variable or constant value as its size.

但是我的这段代码工作得很好:

int number;
cin>>number;
int myArray[number];

cout<<sizeof(myArray)/sizeof(myArray[0])<<endl;
cout<<sizeof(myArray)<<endl;

这是否意味着数组是在动态内存中创建的?或者它是在静态内存中创建的,但它的大小仍然在运行时确定?

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com