c - Assert the allocation of a variable-length array -


I apologize for possible duplicate (can not find the answer):

We need to make sure that allocation of a variable-length array is successfully completed?

For example:

  zero func (int size) {int arr [size]; If (arr == NULL) {// exit with failure} and {// continue as planned}}  

It seems that the answer is yes

Thanks


Update:

I accept the fact that I am sure It has not been done that the above code has been compiled (assuming it does this).

If it does not compile, then it means that no one has a way to emphasize the allocation of a variable-length array.

Therefore, I think that if the allocation fails, the program crashes immediately.

This will be a very strange matter, because it makes sense for the event to crash after an illegal memory allocation (read or write).

Or maybe the allocation will not be a reason, but as soon as I reach an array on an entry that "falls" out of the stack, I may have memory access violation (such as in stack-overflow W) ...?

Honestly, I can not even see how VLA is allocated on the stack if more local variables follow them (especially other VLAs), then I appreciate the answer Also on that issue

This question is derived from a small faulty first base. You can not see that an array NULL , because this is a popular discussion topic. An array is storage object, in-place.

You can not find the code, where the name of the array can be accessed without allocation. A local array is exactly like the other local variable: its existence is in vogue and it is going on for the surrounding code, and there is no perception in the language to check whether any slot given "alloted" Whether or not (as a commentary on the question note, there is a notion under "Stack" level C - language assumes that "it happens", by unspecified magic). It is believed that this code is always successful in order to understand at the most basic level.

What happens in a case where the array can not be allocated, so it is as if the runtime can not allocate space for any other local variable - status is undefined and undefined because the perception created by the C language abstract machine was violated. There is no (completely formal) concept in language that can express it, check it alone or retrieve it, so the test is probably out of the scope, like stack overflow, it is basically a fatal The cause of the accident is guaranteed.

This makes not VLAs useless for a variety of reasons:

  1. Many use of VLA is dangerous for life giant Are not going to happen. Perhaps the only use of variation is to choose a number between 3 and 5? There is no worse for this place compared to some other scalar local people.

  2. Just as the programmer needs to prove some qualities to avoid the eternal reconstruction, which the compiler does not do, in the same way you can reduce your program With a weak weak space VLA will be allowed to be consumed at any time. For example, you can prove yourself that a VLA function is never recursive, or a recursive function is called, and none of them use like more than 10K space - It should be very useful and safe. / P>

  3. You can view VLA as customization , which can help you save space, where you would otherwise have a local array of static sizes You can allocate (like the first example, always assigned 5 instead of 3) as long as you know, and design around, static upper bound, they are effective for making their program with safe overflow Provide guarantees when it is not In that it is not too much space forever. Necessary.


Comments

Popular posts from this blog

java - org.apache.http.ProtocolException: Target host is not specified -

java - Gradle dependencies: compile project by relative path -

ruby on rails - Object doesn't support #inspect when used with .include -