Confused about allocating space to objects

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Confused about allocating space to objects

baboonOfTheNorth
Suppose I call a constructor Class2.new(...) in a subroutine from Class1. I need to know how much space to allocate to the new instance of Class2, and the lectures recommend that I look up the symbol table of Class2 to see how many field and static declarations there are.

What if Class2 has not yet been compiled? The lectures also say that the symbol table can be recycled after compiling each class, so even if it has been compiled, the symbol table for Class2 will be gone if I do this.

So the only solution I can see is to generate the symbol tables for all the classes in the folder before compiling the class subroutines, and storing them in a global variable which can be accessed by the CompilationEngines. I don't like this solution though, it seems messy and inelegant. I also saw an admin in the forums claim that compilation can be done in one pass, so I think I am missing some information here. Can anyone help?
Reply | Threaded
Open this post in threaded view
|

Re: Confused about allocating space to objects

dolomiti7
For the call itself, you don't need to know anything about the other class. You can assume, that the code that you compile is correct. Therefore you can derive the type of the called function from.the syntax:
1. ClassName.func() -> static function or Constructor (doesn't matter here. The call is the same)
2. objVar.func -> method call
3. func() -> method call in own class

The allocation of memory only occurs in the constructor itself. The calling function doesn't need to know (and shouldn't know) anything about that. It will just call the constructor and expect the address of the newly generated object as return value.

Reply | Threaded
Open this post in threaded view
|

Re: Confused about allocating space to objects

baboonOfTheNorth
Oh I see! Thank you so much
Reply | Threaded
Open this post in threaded view
|

Re: Confused about allocating space to objects

WBahn
Administrator
In reply to this post by baboonOfTheNorth
As already explained, you don't need to know how much memory is needed by a function in order to call that function, you just need to know how much space is needed on the stack in order to pass the arguments to it, and that information is available to the compiler when it compiles the call. But there is a caveat here -- the compiler has to assume that the person that wrote the call did so properly. In the context of N2T, that is taken care of by the authors explicitly stating that you are allowed to assume that all of the code you will be asked to compile will be correct -- and they did that specifically to simplify the implementation of the compiler (and other tools) given the broad scope of the course.

A more proper/professional compiler implementation would examine both the number and type of each call and throw an error (or at least a warning) if it doesn't match the function definition. But this runs directly into the problem that the compiler needs to know this information for functions defined in files that it hasn't gotten to yet. There are a couple of ways to tackle this. One is to do an initial pass over all the files to collect this information (similar to how the assembler makes a first pass through the code to identify all of the labels). Another way is to do it like C does via function prototypes, which are explicitly there to provide the compiler with the number and type of arguments that need to be passed and the type, if any, of the return value.
Reply | Threaded
Open this post in threaded view
|

Re: Confused about allocating space to objects

pm100
In reply to this post by baboonOfTheNorth
The space is allocated by the constrictor method of class2, not by the caller