数组静态绑定和动态绑定的区别
我刚刚阅读了有关我现在在 stackoverflow 中询问的同一主题的所有搜索结果,它并没有真正满足我的好奇心。但这就是事情。
问题
1.)据我所知,静态绑定意味着它在编译时设置并且在运行时存在,而动态绑定意味着它在运行时设置。
2.)所以我读的书介绍了动态数组,它提到动态数组大小可以在运行时设置。这是通过这种方式完成的。
代码
int size;
cin >> size;
int * pz = new int [size]; // dynamic binding, size set at run time
delete [] pz; // free memory when finished
3.)在这段代码中,书中提到动态数组大小可以在运行时设置。出于好奇,我尝试了这个。
代码
int size;
cin >> size;
int array[size];
//After the array declaraction i assign value to it to check whether it works or not.
4.)上面的代码也可以工作,所以我只是好奇动态数组有什么特别之处,因为普通的静态数组可以做同样的工作。
5.)是否是因为动态数组可以在运行时释放其内存,而静态数组不能,这就是它如此特别的原因?
感谢您花时间阅读我的问题,请指出我犯的任何错误。
I've just read through all the search result about the same topic I'm asking right now in stackoverflow and it's not really answer my curiosity.But here's the thing.
The Question
1.)From what i know , static binding means it's set during compile time and it's there during runtime whereas for dynamic binding means it's set during runtime.
2.)So the book i read introduce about dynamic array , it mentions dynamic array size could be set during runtime.Which is done in this way.
The Code
int size;
cin >> size;
int * pz = new int [size]; // dynamic binding, size set at run time
delete [] pz; // free memory when finished
3.)In this code the book mention dynamic array size could be set during runtime.So out out curiosity i try this.
The Code
int size;
cin >> size;
int array[size];
//After the array declaraction i assign value to it to check whether it works or not.
4.)The code above works too , so i just curious what so special about dynamic array since normal static array could did the same job.
5.)Is it because of dynamic array could free its memory during runtime whereas static can't that's what makes it so special??
Thanks for spending time reading my question , do point out any mistake made by me.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
具有动态大小的静态数组(称为可变长度数组,简称 VLA)只能通过编译器中的语言扩展来工作。它是 C99 的东西,不包含在 C++ 标准中,这意味着它不可移植。
另一个明显的区别是,您可以将指针传递到其他地方的动态数组,将其保存在某个地方,从函数返回它,或者换句话说,让它超出其创建的范围。你不能用静态数组来做到这一点,因为它们在作用域结束时被销毁。
Your static array with dynamic size (called a variable length array, short VLA) only works thanks to a language extension in your compiler. It's a C99 thing, which isn't contained in the C++ standard, meaning it won't be portable.
The other obvious difference is that you can pass the pointer to the dynamic array somewhere else, save it somewhere, return it from a function, or in other words, have it outlive the scope it was created in. You can't do that with static arrays, as they are destroyed at the end of their scope.
将抛出编译时失败,指出 size 不是编译时常量或预期常量表达式。
您可以像这样
int array[5]
或
在您提前知道数组大小时声明数组。
否则,您将使用新建和删除[]方法。我建议完全避免这种构造,转而使用 std::vector
will throw a compile time failures saying size is not a compile time constant or expected constant expression.
You declare arrays like these
int array[5]
or
when you know the array size well ahead of time.
Otherwise you use the new and delete [] approach. I would recommended avoiding this construct altogether in favor of std::vector
早期绑定:-大多数绑定是在翻译过程中进行的语言,在程序处理的早期阶段被称为具有早期绑定。
后期绑定:-具有后期绑定的语言会将大多数绑定延迟到 l 执行时间。
早期绑定:-灵活性较差。
后期绑定:-它具有更高的编程灵活性。
早期绑定:-此数据仅在声明数据类型时被接受
后期绑定:-在此变量中可以接受任何类型的数据。
早期绑定:-检测到分配的正确类型。
后期绑定:-在这种不正确类型的右侧赋值中,未检测到或出错。
早期绑定:-类型检查必须在编译时完成。
后期绑定:-类型检查必须在运行时完成。
早期绑定:-它由编译器或解释器使用。
后期绑定:-具有变量后期绑定的语言通常使用纯解释器而不是编译器来实现。
early binding:- A language in which most binding are made during translation,early in the processing of a program is said to have early binding.
Late binding:- language with late binding delay most binding until l execution time.
early binding:- It is less flexible.
Late binding:- It has more programming flexibility.
early binding:- In this data is accept in only in declare data type
Late binding:- In this variable can accept any type of data.
early binding:- It is detect in correct type of the assignment.
Late binding:- In this incorrect type of right side of assignment are not detect or error.
early binding:- Type checking must be done at compile time.
Late binding:- Type checking must be done at run time.
early binding:- It user compiler or interpreter.
Late binding:- Language that have late binding for variable are usually implemented using pure interpreter rather than compiler.