指向 VLA 的指针
Pointers to VLA's
如您所知,VLA's haves pros and cons 它们在 C11 中是可选的。
我认为将 VLA 设为可选的主要原因是:"the stack can blow up":
int arr[n]; /* where n = 1024 * 1024 * 1024 */
但是指向 VLA 的指针呢?
int m, n;
scanf("%d %d", &m, &n);
int (*ptr)[n] = malloc(sizeof(int [m][n]));
在这种情况下,没有炸毁堆栈的风险,IMO 它们非常有用。
我的问题是:
委员会是否可以保留指向 VLA 的指针,使指向非指针类型的 VLA 成为可选的?
或者一件事意味着另一件事?
(请原谅我糟糕的英语)
保留指向可变修改类型的指针需要实现支持大约 90% 的 VLA 规范。原因是有效类型规则:
6.5 Expressions
¶6 The effective type of an object for an access to its stored value
is the declared type of the object, if any. If a value is stored into
an object having no declared type through an lvalue having a type that
is not a character type, then the type of the lvalue becomes the
effective type of the object for that access and for subsequent
accesses that do not modify the stored value. If a value is copied
into an object having no declared type using memcpy or memmove, or is
copied as an array of character type, then the effective type of the
modified object for that access and for subsequent accesses that do
not modify the value is the effective type of the object from which
the value is copied, if it has one. For all other accesses to an
object having no declared type, the effective type of the object is
simply the type of the lvalue used for the access.
通过ptr
访问malloc
ed内存后,对象的有效类型是VLA类型。因此,实现将需要正确支持这些语义。唯一可以留下的东西 "optional" 是声明具有自动存储持续时间的 VLA 的能力...
int boo[n];
...这有点傻。如果一个实现支持动态分配对象的 most VLA 语义,它也可以允许将它们声明为具有自动存储持续时间的对象。委员会希望它真正是可选的,所以这意味着指向 VLA 类型的指针也必须消失。
如您所知,VLA's haves pros and cons 它们在 C11 中是可选的。
我认为将 VLA 设为可选的主要原因是:"the stack can blow up":
int arr[n]; /* where n = 1024 * 1024 * 1024 */
但是指向 VLA 的指针呢?
int m, n;
scanf("%d %d", &m, &n);
int (*ptr)[n] = malloc(sizeof(int [m][n]));
在这种情况下,没有炸毁堆栈的风险,IMO 它们非常有用。
我的问题是:
委员会是否可以保留指向 VLA 的指针,使指向非指针类型的 VLA 成为可选的?
或者一件事意味着另一件事?
(请原谅我糟糕的英语)
保留指向可变修改类型的指针需要实现支持大约 90% 的 VLA 规范。原因是有效类型规则:
6.5 Expressions
¶6 The effective type of an object for an access to its stored value is the declared type of the object, if any. If a value is stored into an object having no declared type through an lvalue having a type that is not a character type, then the type of the lvalue becomes the effective type of the object for that access and for subsequent accesses that do not modify the stored value. If a value is copied into an object having no declared type using memcpy or memmove, or is copied as an array of character type, then the effective type of the modified object for that access and for subsequent accesses that do not modify the value is the effective type of the object from which the value is copied, if it has one. For all other accesses to an object having no declared type, the effective type of the object is simply the type of the lvalue used for the access.
通过ptr
访问malloc
ed内存后,对象的有效类型是VLA类型。因此,实现将需要正确支持这些语义。唯一可以留下的东西 "optional" 是声明具有自动存储持续时间的 VLA 的能力...
int boo[n];
...这有点傻。如果一个实现支持动态分配对象的 most VLA 语义,它也可以允许将它们声明为具有自动存储持续时间的对象。委员会希望它真正是可选的,所以这意味着指向 VLA 类型的指针也必须消失。