4 ms·
Variable length arrays: If, because of parameters outside the programmer's control, a call to malloc() fails to allocate an array of n doubles, it returns NULL
by pascal_cuoq 14y ago
Variable length arrays:
If, because of parameters outside the programmer's control, a call to malloc() fails to allocate an array of n doubles, it returns NULL.
If, because of parameters outside the programmer's control, the variable-length definition “array double arr[n];” fails to allocate an array of n doubles, the behavior is undefined.
Your choice.
- cygx 14y agoSee http://stackoverflow.com/a/12412774/48015 http://stackoverflow.com/a/12412774/48015 for the rationale behind the introduction of VLAs.
- popee 14y agoHow do you "allocate" memory on stack? You can always do 'double arr[huge_constant]' and, depending on resources, overflow heap data. Or with recursion. Damn those bugs >:-) The thing about C is that programmer should do check size before calling function with double arr[n]. Isn't that point of C? On the other hand, user can make fast stack allocation which, depending on situation, can be good. Stack is insecure by default, OS should put it really far from other parts. In embedded systems, well there you must always think about these kind of things. Btw, how are (p)thread stacks implemented? Every thread has it's own stack, that's ein interesting situation.
- pascal_cuoq 14y ago> How do you "allocate" memory on stack? I have no idea why you would be asking me this, unless you are quibbling over the idiom associating the verb “allocate” and the complement “on the stack”, in which case Google should be able to find you about 56000 uses, in context, of this precise association. > The thing about C is that programmer should > do check size before calling function with double arr[n]. > Isn't that point of C? What part of “because of parameters outside the programmer's control” do you not understand? What limit, in a portable C program, do you check the value of n against before allocating a VLA of size n?