Saturday 15 June 2013

c++ - How is memory allocated for a static multi-dimensional array? -


All,

This has been going to annoy me for a while. In C \ C ++ (I also think Java and .NET) we do not need to specify line index in multi-dimensional array. Therefore, for example, I can declare an array of ints like this:

int Array [] [100];

I think the static arrays in general are shown as the nearest memory on the stack. Therefore, taking a column-key representation, how does the compiler know how much memory is to allocate in the above case because it is one of the dimensions?

You can not do this in C ++ language

  Int Array [] [100]; / * Error: Incomplete type * /  

Because it will define an incomplete type of object, which is clearly illegal in C ++, you can use it in a non-defined declaration

  extern int Array [] [100];  

(or as a stable member of a class), but when it comes to the actual definition of the same array object, then both sizes will be clearly specified (or explicitly Initially received).

The situation in C is not very different, except that there are such things in C such as temporary definitions which let you write

  int array [ ] [100];  

However, a temporary definition in this regard is similar to a non-defined declaration, which is why it is allowed, finally you define the same object with the specified object explicitly (Some compilers do not need a non-standard extension). If you try to do something like this in a non-temporal definition, you will get an error

  Fixed int array [] [100]; / * Error: incomplete type * /  

So, if you think about it, with different definitions, the situation of C and C ++ is not very different: It is illegal to define an array of incomplete types and unspecified sizes in these languages ​​is an incomplete type.


No comments:

Post a Comment