本文介绍了C99动态数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有谁知道C99动态数组是什么,以及它是否可以在C ++中使用



问候

Andy Little

Does anyone know what a C99 dynamic array is, and if it will be
useable in C++?

regards
Andy Little

推荐答案



一个可变长度的数组,即一个大小可以通过

运行时变量设置的数组。


< C>

#include< stdlib.h>


void f(size_t z){


typedef enum some_t {some_value} some_type;


some_type a [5] = {some_value}; //总是好的

// some_type b [z] = {some_value}; //总是非法的

some_type c [z]; //确定只有c99

}

< / C>

A variable-length array, i.e. an array whose size can be set by a
run-time variable.

<C>
#include <stdlib.h>

void f(size_t z) {

typedef enum some_t { some_value } some_type;

some_type a[5] = { some_value }; // always ok
//some_type b[z] = { some_value }; // always illegal
some_type c[z]; // ok in c99 only
}
</C>



不是AFAIK,尽管C ++ 0x将包含C99的一些功能。无论如何,

std :: vector在大多数方面都是首选。

向量的缺点是它使用动态存储,这可能比自动(基于堆栈)存储产生更多的性能开销。无论你做什么,std :: vector都是一个比原始阵列更好的选择。

Not AFAIK, although C++0x will incorporate some features of C99. The
std::vector is preferable in most respects, anyway. The down-side of
vector is that it uses dynamic storage, which may incur more performance
overhead than automatic (stack-based) storage. Chances are excellent
that whatever you''re doing, std::vector is a better choice than a raw array.




一个可变长度的数组,即一个大小可以通过
设置的数组
运行时变量。


< C>

#include< stdlib.h>


void f(size_t z){


* * * * typedef enum some_t {some_value} some_type;


* * * * some_type a [5] = {some_value}; //总是好的

// * * * some_type b [z] = {some_value}; //总是非法的

* * * * some_type c [z]; //仅限c99确定}


< / C>


A variable-length array, i.e. an array whose size can be set by a
run-time variable.

<C>
#include <stdlib.h>

void f(size_t z) {

* * * * typedef enum some_t { some_value } some_type;

* * * * some_type a[5] = { some_value }; // always ok
// * * *some_type b[z] = { some_value }; // always illegal
* * * * some_type c[z]; // ok in c99 only}

</C>



< ...>

<...>



啊哈,是的,但动态数组实际上看起来就像下面的

std :: vector(使用堆分配)或者是还有一些其他的

魔法。


它不是一种圣杯,你可以创建一个动态的b $ b大小的数组没有堆,所以如果C99动态数组确实使用另一个

方法来分配,会很有趣。


(我的猜测是它有相同的性能作为std :: vector。有

没有魔法)


问候

Andy Little

Ah ha yes, but does the dynamic array actually just look like a
std::vector underneath (use heap allocation) or is there some other
magic.

Its a sort of holy grail isnt it, that you can create a dynamically
sized array without the heap, so if C99 dynamic array does use another
method to allocate, would be interesting.

(my guess is that it woud have same performance as std::vector. There
is no "magic")

regards
Andy Little



一个可变长度的数组,即一个大小可以是s的数组等等运行时变量。

< C>
#include< stdlib.h>

void f(size_t z) {

typedef enum some_t {some_value} some_type;

some_type a [5] = {some_value}; //总是好的
// some_type b [z] = {some_value}; //总是非法的
some_type c [z]; //仅在c99中确定}

< / C>

A variable-length array, i.e. an array whose size can be set by a
run-time variable.

<C>
#include <stdlib.h>

void f(size_t z) {

typedef enum some_t { some_value } some_type;

some_type a[5] = { some_value }; // always ok
// some_type b[z] = { some_value }; // always illegal
some_type c[z]; // ok in c99 only}

</C>



< ...>


<...>



啊哈,是的,但动态数组实际上看起来就像下面的

std :: vector(使用堆分配)或者是还有一些其他的

魔法。


它不是一种圣杯,你可以创建一个动态的b $ b大小的数组没有堆,所以如果C99动态数组确实使用另一个

方法来分配,会很有趣。


(我的猜测是它有相同的性能作为std :: vector。有

不是魔术)


Ah ha yes, but does the dynamic array actually just look like a
std::vector underneath (use heap allocation) or is there some other
magic.

Its a sort of holy grail isnt it, that you can create a dynamically
sized array without the heap, so if C99 dynamic array does use another
method to allocate, would be interesting.

(my guess is that it woud have same performance as std::vector. There
is no "magic")



我相信VLA使用堆栈,就像传统阵列一样。 魔术

是编译器不知道函数的堆栈

帧的大小有多大;事实上,同一功能的多次调用可能需要不同大小的帧。知道这给编译器实现者带来了什么样的并发症,以及这些问题是如何解决的,这将是一件很有趣的事。

I believe VLAs use the stack, just like traditional arrays. The "magic"
is that the compiler doesn''t know a priori how big the function''s stack
frame will be; in fact, multiple invocations of the same function might
need frames of different sizes. It would be interesting to know what
kinds of complications this creates for compiler implementers, and how
those problems have been worked around.


这篇关于C99动态数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 12:45