3

Basically, I have not been able to find much information about this on the internet, but I understand that the basic class instantiation is: -> operator new() -> allocates memory from somewhere -> constructor -> assigns values to "data types"

Now, what I want to know is, how does C++ allocate methods/functions of the class rather than its members. According to my web research, this cannot happen in new() because it is only allocating raw memory, and as far as I have gotten, I have not quite been able to figure out how this could be done in the constructor with functions (rather than function pointers). Also, I assume that because of the existence of the keyword static, without this keyword, it is allocated as part of the parent class. How and where does this happen?

Also, if the functions are included in the memory of the class, does the function sizeof() give the size of just the class and its members, or does it also include the related functions?

  • 1
    The virtual functions are put in a [v-table][1] [1]: http://stackoverflow.com/questions/1963926/when-is-a-v-table-created-in-c – doctorlove Jun 28 '13 at 11:15
  • What do you think it should do with the functions when you create objects? the code for the functions is already there, in your binary, thats what the compiler was for. – PlasmaHH Jun 28 '13 at 11:16
  • The OS app loader, (and maybe virtual paged memory manager), does it. Don't really understand your question? – Martin James Jun 28 '13 at 11:17
  • What if you don't have an OS app loader, or paged memory manager - in other words, if you are building a kernal, ect. – David Turner Jun 28 '13 at 11:19
  • As @PlasmaHH pointed out this is the job of the compiler and linker. Same applies for firmware that runs without OS loader. – πάντα ῥεῖ Jun 28 '13 at 11:25
  • Found it out: specifically the compiler adds the code to reference the vtable in the constructor (hidden from the programmer). Thank you for your comments, they helped put me in the right direction – David Turner Jun 28 '13 at 13:23

1 Answers1

3

While compiling the code compiler takes stores the addresses of the starting point of the functions in the raw code. This address can be relative the starting location of the program or an absolute memory address.

The point is when the function is called the(assuming that scope issues are taken care of) in the code, while compiling the compiler just insert a jump statement to the address where the code of the function is present. For returning to the same location, there is some other operations taking place.

So when you say space is allocated, it just the space occupied by bytecode of the function plus the entry in a table inn compiler which says this function is present at this address

This is pretty much the case with every programming language(which compiles) not only C++.

As for your other part: sizeof(type) returns size in bytes of the object representation of type which is basically an aggregation of size of its members(if we leave out the padding which is done by compiler for optimization).

Aman Deep Gautam
  • 8,091
  • 21
  • 74
  • 130
  • If I wanted to move the function to a specific location, from where it was, how would I go about getting its location and size? Also, can I change the function references, or does this have to be done with function pointers – David Turner Jun 28 '13 at 11:24
  • @DavidTurner What do you mean by `move the function`?? – Aman Deep Gautam Jun 28 '13 at 11:25
  • Move its location in memory, say to a set area, eg you want your function to be stored at memory location 0x123456 – David Turner Jun 28 '13 at 11:26
  • 1
    you can't move functions. They're located in read-only memory – bcrist Jun 28 '13 at 11:27
  • @DavidTurner this is handled by the compiler. I do not think you can do that but anyway why would you need such a feature – Aman Deep Gautam Jun 28 '13 at 11:28
  • 1
    You could copy a function to dynamically allocated space, and call it using a function pointer, but most platforms will consider this "executing data" and throw fits. – bcrist Jun 28 '13 at 11:28
  • It seems like yet another XY problem - what exactly are you trying to achieve David? – aryan Jun 28 '13 at 11:28
  • Read-only means you can only read them. Copy could be defined as a read. I assume that the read only sections are still part of RAM, right, just with a bit or something set to tell the controllers involved that that location is read only. Couldn't you remove said bit/ revoke that area of memory. Presumably this is what happens when normal programs completely finish, and have all the memory allocated to them returned – David Turner Jun 28 '13 at 11:29
  • Your OS is controlling memory access, including that it's read only. Also your OS collects memory after process terminates. But once again - why would you try to do this? – aryan Jun 28 '13 at 11:32
  • What I want to achieve is that I am trying to create a C++ OS kernel as a class, preferably without implementing a temporary memory allocation system. So far I have figured out that I can use placement new to get the memory I need, but I needed to know where the functions that ran everything was stored, what state they were in, how they were referenced, and also whether they were modifiable by external entities (eg. other peoples functions, security risk). Also it would not be a good idea to try and overwrite said memory using a memory manager. – David Turner Jun 28 '13 at 11:33
  • You are correct that it is still in RAM, but user-mode programs do not have absolute control over the system. The operating system controls what areas of memory are actually mapped to a process's virtual address space, and what areas are marked as read-only, no-execute, etc. – bcrist Jun 28 '13 at 11:33
  • Yes, but how do I let my memory manager know that that segment of memory containing my functions is read-only, because presumably I will just have a massive memory map handed to my by GRUB or a similar bootloader, which gives me no idea where my read only functions are located so I can protect them. As the memory manager itself is level 0 it can still allocate restricted memory, or do whatever it likes, though this would probably just crash whichever program It gave it to, not exactly an optimal solution – David Turner Jun 28 '13 at 11:35
  • 4
    You don't... Clearly you are attempting to undertake a project which is over-ambitious for your current level of understanding. – bcrist Jun 28 '13 at 11:37
  • And yes, @bcrist, this project is probably over-ambitious, generally I do osdev for a month or two, then go to other programming and back. Its a motivator to increase my knowledge, as well as a learning experience into how everything works under the hood. Thank you all for putting up with an inexperience person like me, though, it is very kind of you – David Turner Jun 28 '13 at 11:56