public class MyClass
{
int i = 0;
string str = "here";
MyStruct mystruct;
B b;
ArrayList myList = new ArrayList(10);
public MyClass()
{
}
....
}
public struct MyStruct
{
public int i;
public float f;
}
public class B
{
...
}
Want to learn how an instance of a class is created in the background. When this statement
MyClass myClass = new MyClass();
is evaluated. What will happen in the backgroud? My following statements are correct or not (for 32-bit OS machine)?
myClass
;int i
;string str
; the actual value of the str
is stored in other location (where?)MyStruct mystruct
(because MyStruct is 8 bytes);B b
object; memory for b object will be allocated in somewhere else when it is instantiated;ArrayList myList
; actual memory space for ArrayList myList
is allocated in other place and referenced in here as myList
;C is what's referred to as a compiled language, meaning you have to use a compiler to turn the code into an executable file before you can run it. The code is written into one or more text files, which you can open, read and edit in any text editor, such as Notepad in Windows, TextEdit on a Mac, and gedit in Linux.
%d is a format specifier, used in C Language. Now a format specifier is indicated by a % (percentage symbol) before the letter describing it. In simple words, a format specifier tells us the type of data to store and print. Now, %d represents the signed decimal integer.
The logical AND operator ( && ) returns true if both operands are true and returns false otherwise.
Operation: The -> operator in C or C++ gives the value held by variable_name to structure or union variable pointer_name. Difference between Dot(.) and Arrow(->) operator: The Dot(.) operator is used to normally access members of a structure or union.
You have the basic idea in place. The actual specifics of this, aside from what you include, is implementation specific. However, for a couple of your points:
3) The actual string is typically stored in its own memory space, just like any other class. However, since you're using a string literal in this case, it'll most likely be in the string intern pool, which is (I believe) stored in the large object heap. For details on string interning, see String.Intern. (If you allocated the string on the fly, instead of using a literal, the string would be stored in the normal managed heap of your application.)
That's about it.
The string is stored in the string table rather than as a free floating object in the heap. It's important to note that only one instance of that string is instantiated, regardless of the number of instances of MyClass
that are instantiated.
7.another 4 or 8 bytes from above memory space is used for object metadata
It would be 8 bytes. 4 for the syncblock and 4 for type information.
You can find more information regarding the subject in this question.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With