Pointers to VLA's











up vote
3
down vote

favorite
1












As you may know, VLA's haves pros and cons and they are optional in C11.



I suppose that the main reason to make VLA's optional is: "the stack can blow up":



int arr[n]; /* where n = 1024 * 1024 * 1024 */


but what about pointer to VLA's?



int m, n;

scanf("%d %d", &m, &n);

int (*ptr)[n] = malloc(sizeof(int [m][n]));


In this case, there is no risk to blow up the stack, and IMO they are extremely useful.



My question is:



Could the committee have preserved pointers to VLA's, making the VLA's to non-pointer types optional?



Or one thing implies the other?



(Excuse my poor english)










share|improve this question


















  • 3




    It's not PC, but I think the main reason to make VLA's optional is so that MS could pretend they have a standard compliant compiler
    – StoryTeller
    Nov 18 at 9:02










  • int (*ptr)[n] is just a pointer to an array of size n. The n here can be completely ignored by the compiler as it has no purpose. int *ptr; is exactly the same.
    – Paul Ogilvie
    Nov 18 at 9:03








  • 3




    @PaulOgilvie - The n affects how the pointer arithmetic is done on ptr. It most certainly cannot be ignored.
    – StoryTeller
    Nov 18 at 9:13






  • 2




    ptr + x must be equal to (char*)ptr + x * (sizeof(int[n])) - usual pointer arithmetic in arrays. Same as if the row size was a constant expression. In this case it just isn't.
    – StoryTeller
    Nov 18 at 9:23








  • 1




    All clear. Many thanks.
    – Paul Ogilvie
    Nov 18 at 9:33















up vote
3
down vote

favorite
1












As you may know, VLA's haves pros and cons and they are optional in C11.



I suppose that the main reason to make VLA's optional is: "the stack can blow up":



int arr[n]; /* where n = 1024 * 1024 * 1024 */


but what about pointer to VLA's?



int m, n;

scanf("%d %d", &m, &n);

int (*ptr)[n] = malloc(sizeof(int [m][n]));


In this case, there is no risk to blow up the stack, and IMO they are extremely useful.



My question is:



Could the committee have preserved pointers to VLA's, making the VLA's to non-pointer types optional?



Or one thing implies the other?



(Excuse my poor english)










share|improve this question


















  • 3




    It's not PC, but I think the main reason to make VLA's optional is so that MS could pretend they have a standard compliant compiler
    – StoryTeller
    Nov 18 at 9:02










  • int (*ptr)[n] is just a pointer to an array of size n. The n here can be completely ignored by the compiler as it has no purpose. int *ptr; is exactly the same.
    – Paul Ogilvie
    Nov 18 at 9:03








  • 3




    @PaulOgilvie - The n affects how the pointer arithmetic is done on ptr. It most certainly cannot be ignored.
    – StoryTeller
    Nov 18 at 9:13






  • 2




    ptr + x must be equal to (char*)ptr + x * (sizeof(int[n])) - usual pointer arithmetic in arrays. Same as if the row size was a constant expression. In this case it just isn't.
    – StoryTeller
    Nov 18 at 9:23








  • 1




    All clear. Many thanks.
    – Paul Ogilvie
    Nov 18 at 9:33













up vote
3
down vote

favorite
1









up vote
3
down vote

favorite
1






1





As you may know, VLA's haves pros and cons and they are optional in C11.



I suppose that the main reason to make VLA's optional is: "the stack can blow up":



int arr[n]; /* where n = 1024 * 1024 * 1024 */


but what about pointer to VLA's?



int m, n;

scanf("%d %d", &m, &n);

int (*ptr)[n] = malloc(sizeof(int [m][n]));


In this case, there is no risk to blow up the stack, and IMO they are extremely useful.



My question is:



Could the committee have preserved pointers to VLA's, making the VLA's to non-pointer types optional?



Or one thing implies the other?



(Excuse my poor english)










share|improve this question













As you may know, VLA's haves pros and cons and they are optional in C11.



I suppose that the main reason to make VLA's optional is: "the stack can blow up":



int arr[n]; /* where n = 1024 * 1024 * 1024 */


but what about pointer to VLA's?



int m, n;

scanf("%d %d", &m, &n);

int (*ptr)[n] = malloc(sizeof(int [m][n]));


In this case, there is no risk to blow up the stack, and IMO they are extremely useful.



My question is:



Could the committee have preserved pointers to VLA's, making the VLA's to non-pointer types optional?



Or one thing implies the other?



(Excuse my poor english)







c variable-length-array






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 18 at 9:00









Keine Lust

26.3k43362




26.3k43362








  • 3




    It's not PC, but I think the main reason to make VLA's optional is so that MS could pretend they have a standard compliant compiler
    – StoryTeller
    Nov 18 at 9:02










  • int (*ptr)[n] is just a pointer to an array of size n. The n here can be completely ignored by the compiler as it has no purpose. int *ptr; is exactly the same.
    – Paul Ogilvie
    Nov 18 at 9:03








  • 3




    @PaulOgilvie - The n affects how the pointer arithmetic is done on ptr. It most certainly cannot be ignored.
    – StoryTeller
    Nov 18 at 9:13






  • 2




    ptr + x must be equal to (char*)ptr + x * (sizeof(int[n])) - usual pointer arithmetic in arrays. Same as if the row size was a constant expression. In this case it just isn't.
    – StoryTeller
    Nov 18 at 9:23








  • 1




    All clear. Many thanks.
    – Paul Ogilvie
    Nov 18 at 9:33














  • 3




    It's not PC, but I think the main reason to make VLA's optional is so that MS could pretend they have a standard compliant compiler
    – StoryTeller
    Nov 18 at 9:02










  • int (*ptr)[n] is just a pointer to an array of size n. The n here can be completely ignored by the compiler as it has no purpose. int *ptr; is exactly the same.
    – Paul Ogilvie
    Nov 18 at 9:03








  • 3




    @PaulOgilvie - The n affects how the pointer arithmetic is done on ptr. It most certainly cannot be ignored.
    – StoryTeller
    Nov 18 at 9:13






  • 2




    ptr + x must be equal to (char*)ptr + x * (sizeof(int[n])) - usual pointer arithmetic in arrays. Same as if the row size was a constant expression. In this case it just isn't.
    – StoryTeller
    Nov 18 at 9:23








  • 1




    All clear. Many thanks.
    – Paul Ogilvie
    Nov 18 at 9:33








3




3




It's not PC, but I think the main reason to make VLA's optional is so that MS could pretend they have a standard compliant compiler
– StoryTeller
Nov 18 at 9:02




It's not PC, but I think the main reason to make VLA's optional is so that MS could pretend they have a standard compliant compiler
– StoryTeller
Nov 18 at 9:02












int (*ptr)[n] is just a pointer to an array of size n. The n here can be completely ignored by the compiler as it has no purpose. int *ptr; is exactly the same.
– Paul Ogilvie
Nov 18 at 9:03






int (*ptr)[n] is just a pointer to an array of size n. The n here can be completely ignored by the compiler as it has no purpose. int *ptr; is exactly the same.
– Paul Ogilvie
Nov 18 at 9:03






3




3




@PaulOgilvie - The n affects how the pointer arithmetic is done on ptr. It most certainly cannot be ignored.
– StoryTeller
Nov 18 at 9:13




@PaulOgilvie - The n affects how the pointer arithmetic is done on ptr. It most certainly cannot be ignored.
– StoryTeller
Nov 18 at 9:13




2




2




ptr + x must be equal to (char*)ptr + x * (sizeof(int[n])) - usual pointer arithmetic in arrays. Same as if the row size was a constant expression. In this case it just isn't.
– StoryTeller
Nov 18 at 9:23






ptr + x must be equal to (char*)ptr + x * (sizeof(int[n])) - usual pointer arithmetic in arrays. Same as if the row size was a constant expression. In this case it just isn't.
– StoryTeller
Nov 18 at 9:23






1




1




All clear. Many thanks.
– Paul Ogilvie
Nov 18 at 9:33




All clear. Many thanks.
– Paul Ogilvie
Nov 18 at 9:33












1 Answer
1






active

oldest

votes

















up vote
2
down vote



accepted










Preserving pointers to variably modifiable types would require an implementation to support about 90% of the VLA specification. The reason is the effective type rule:




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.




After an access via ptr to the malloced memory, the effective type of the object is a VLA type. So an implementation will need to support those semantics correctly. The only thing that can be left "optional" is the ability to declare VLA's with automatic storage duration...



int boo[n];


... which is kinda silly. If an implementation supports most of VLA semantics for dynamically allocated objects, it may as well allow declaring them as objects with automatic storage duration. The committee wanted it to be truly optional, so that means pointers to VLA types had to go too.






share|improve this answer























  • I don't think this answer is correct. There is no work at all in implementing effective type rules except to perform optimization based on non-aliasing, and an implementor can omit whichever rules they like, because the rules apply to conforming applications not implementations. On the other hand, automatic storage VLAs necessarily interact with low-level backend stuff.
    – R..
    Nov 18 at 15:26










  • @R.. - The question was about the standard's pov. The amount of specification that will need to be hacked and slashed around the effective type rule looks non-trivial to me. Of course implementations can implement whatever.
    – StoryTeller
    Nov 18 at 15:28












  • The quote is mostly irrelevant as you can never store a value with an array type into an object -- you can only store values with the array's element type into elements of the array. Similarly, you can never access a value with an array type -- only elements of the array. I think StoryTeller's comment identifies the main reason.
    – Chris Dodd
    Nov 18 at 20:28













Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53359275%2fpointers-to-vlas%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
2
down vote



accepted










Preserving pointers to variably modifiable types would require an implementation to support about 90% of the VLA specification. The reason is the effective type rule:




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.




After an access via ptr to the malloced memory, the effective type of the object is a VLA type. So an implementation will need to support those semantics correctly. The only thing that can be left "optional" is the ability to declare VLA's with automatic storage duration...



int boo[n];


... which is kinda silly. If an implementation supports most of VLA semantics for dynamically allocated objects, it may as well allow declaring them as objects with automatic storage duration. The committee wanted it to be truly optional, so that means pointers to VLA types had to go too.






share|improve this answer























  • I don't think this answer is correct. There is no work at all in implementing effective type rules except to perform optimization based on non-aliasing, and an implementor can omit whichever rules they like, because the rules apply to conforming applications not implementations. On the other hand, automatic storage VLAs necessarily interact with low-level backend stuff.
    – R..
    Nov 18 at 15:26










  • @R.. - The question was about the standard's pov. The amount of specification that will need to be hacked and slashed around the effective type rule looks non-trivial to me. Of course implementations can implement whatever.
    – StoryTeller
    Nov 18 at 15:28












  • The quote is mostly irrelevant as you can never store a value with an array type into an object -- you can only store values with the array's element type into elements of the array. Similarly, you can never access a value with an array type -- only elements of the array. I think StoryTeller's comment identifies the main reason.
    – Chris Dodd
    Nov 18 at 20:28

















up vote
2
down vote



accepted










Preserving pointers to variably modifiable types would require an implementation to support about 90% of the VLA specification. The reason is the effective type rule:




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.




After an access via ptr to the malloced memory, the effective type of the object is a VLA type. So an implementation will need to support those semantics correctly. The only thing that can be left "optional" is the ability to declare VLA's with automatic storage duration...



int boo[n];


... which is kinda silly. If an implementation supports most of VLA semantics for dynamically allocated objects, it may as well allow declaring them as objects with automatic storage duration. The committee wanted it to be truly optional, so that means pointers to VLA types had to go too.






share|improve this answer























  • I don't think this answer is correct. There is no work at all in implementing effective type rules except to perform optimization based on non-aliasing, and an implementor can omit whichever rules they like, because the rules apply to conforming applications not implementations. On the other hand, automatic storage VLAs necessarily interact with low-level backend stuff.
    – R..
    Nov 18 at 15:26










  • @R.. - The question was about the standard's pov. The amount of specification that will need to be hacked and slashed around the effective type rule looks non-trivial to me. Of course implementations can implement whatever.
    – StoryTeller
    Nov 18 at 15:28












  • The quote is mostly irrelevant as you can never store a value with an array type into an object -- you can only store values with the array's element type into elements of the array. Similarly, you can never access a value with an array type -- only elements of the array. I think StoryTeller's comment identifies the main reason.
    – Chris Dodd
    Nov 18 at 20:28















up vote
2
down vote



accepted







up vote
2
down vote



accepted






Preserving pointers to variably modifiable types would require an implementation to support about 90% of the VLA specification. The reason is the effective type rule:




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.




After an access via ptr to the malloced memory, the effective type of the object is a VLA type. So an implementation will need to support those semantics correctly. The only thing that can be left "optional" is the ability to declare VLA's with automatic storage duration...



int boo[n];


... which is kinda silly. If an implementation supports most of VLA semantics for dynamically allocated objects, it may as well allow declaring them as objects with automatic storage duration. The committee wanted it to be truly optional, so that means pointers to VLA types had to go too.






share|improve this answer














Preserving pointers to variably modifiable types would require an implementation to support about 90% of the VLA specification. The reason is the effective type rule:




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.




After an access via ptr to the malloced memory, the effective type of the object is a VLA type. So an implementation will need to support those semantics correctly. The only thing that can be left "optional" is the ability to declare VLA's with automatic storage duration...



int boo[n];


... which is kinda silly. If an implementation supports most of VLA semantics for dynamically allocated objects, it may as well allow declaring them as objects with automatic storage duration. The committee wanted it to be truly optional, so that means pointers to VLA types had to go too.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 18 at 9:45

























answered Nov 18 at 9:40









StoryTeller

89.7k12180246




89.7k12180246












  • I don't think this answer is correct. There is no work at all in implementing effective type rules except to perform optimization based on non-aliasing, and an implementor can omit whichever rules they like, because the rules apply to conforming applications not implementations. On the other hand, automatic storage VLAs necessarily interact with low-level backend stuff.
    – R..
    Nov 18 at 15:26










  • @R.. - The question was about the standard's pov. The amount of specification that will need to be hacked and slashed around the effective type rule looks non-trivial to me. Of course implementations can implement whatever.
    – StoryTeller
    Nov 18 at 15:28












  • The quote is mostly irrelevant as you can never store a value with an array type into an object -- you can only store values with the array's element type into elements of the array. Similarly, you can never access a value with an array type -- only elements of the array. I think StoryTeller's comment identifies the main reason.
    – Chris Dodd
    Nov 18 at 20:28




















  • I don't think this answer is correct. There is no work at all in implementing effective type rules except to perform optimization based on non-aliasing, and an implementor can omit whichever rules they like, because the rules apply to conforming applications not implementations. On the other hand, automatic storage VLAs necessarily interact with low-level backend stuff.
    – R..
    Nov 18 at 15:26










  • @R.. - The question was about the standard's pov. The amount of specification that will need to be hacked and slashed around the effective type rule looks non-trivial to me. Of course implementations can implement whatever.
    – StoryTeller
    Nov 18 at 15:28












  • The quote is mostly irrelevant as you can never store a value with an array type into an object -- you can only store values with the array's element type into elements of the array. Similarly, you can never access a value with an array type -- only elements of the array. I think StoryTeller's comment identifies the main reason.
    – Chris Dodd
    Nov 18 at 20:28


















I don't think this answer is correct. There is no work at all in implementing effective type rules except to perform optimization based on non-aliasing, and an implementor can omit whichever rules they like, because the rules apply to conforming applications not implementations. On the other hand, automatic storage VLAs necessarily interact with low-level backend stuff.
– R..
Nov 18 at 15:26




I don't think this answer is correct. There is no work at all in implementing effective type rules except to perform optimization based on non-aliasing, and an implementor can omit whichever rules they like, because the rules apply to conforming applications not implementations. On the other hand, automatic storage VLAs necessarily interact with low-level backend stuff.
– R..
Nov 18 at 15:26












@R.. - The question was about the standard's pov. The amount of specification that will need to be hacked and slashed around the effective type rule looks non-trivial to me. Of course implementations can implement whatever.
– StoryTeller
Nov 18 at 15:28






@R.. - The question was about the standard's pov. The amount of specification that will need to be hacked and slashed around the effective type rule looks non-trivial to me. Of course implementations can implement whatever.
– StoryTeller
Nov 18 at 15:28














The quote is mostly irrelevant as you can never store a value with an array type into an object -- you can only store values with the array's element type into elements of the array. Similarly, you can never access a value with an array type -- only elements of the array. I think StoryTeller's comment identifies the main reason.
– Chris Dodd
Nov 18 at 20:28






The quote is mostly irrelevant as you can never store a value with an array type into an object -- you can only store values with the array's element type into elements of the array. Similarly, you can never access a value with an array type -- only elements of the array. I think StoryTeller's comment identifies the main reason.
– Chris Dodd
Nov 18 at 20:28




















 

draft saved


draft discarded



















































 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53359275%2fpointers-to-vlas%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Costa Masnaga

Fotorealismo

Sidney Franklin