Results 1 to 9 of 9

Thread: stack, heap and C#

  1. #1
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Arrow stack, heap and C#

    Hello, I read that C# allow 2 types: value and reference. Class object are always reference types; "struct" and base type (int, float, bool) are value types; but I read that if I embed any value types in a reference type (eg an int32 inside a class declaration), all goes on the heap (even the int32); outside of an object that int32 should go on the stack (the variable with its value).
    Qt Code:
    1. class Program {
    2. struct Address {
    3. public Address (string v, int n) {
    4. this.street = v;
    5. this.number = n;
    6. }
    7. internal string street;
    8. internal int number;
    9. };
    10. Address addr = new Address ("donner",99);
    11. public Int32 m = 9;
    12. static void Main(string[] args) {
    13. System.Int32 i = 99;
    14. }
    15. }
    To copy to clipboard, switch view to plain text mode 
    Above I see 'm' and 'addr' be value types: it should be on the stack. But reading better I can see that they're inside the class 'Program'; I think that Someone create an instance of class 'Program' (I don't know who and where); but the question is: in the manner of C#, every type is inside a Class declaration; so everything goes on the heap; but I know that it isn't possible; then, what happen exactly??? I hope you understand...
    thanks..
    Regards

  2. #2
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: stack, heap and C#

    I think you misunderstood the meaning of value and reference types in C#.

    Value types contain directly their data, while reference types are very similar to pointers in C++. A reference is exactly what the name suggests: a reference to some object on the heap.


    So, in your example, even if you instantiate that struct in the class scope, it will not be a reference. The data contained by it will be placed in a contiguous block at a certain offset in the memory allocated on the heap for the CLASS.

    So the class data itself is allocated on the heap, but the struct will not have its own heap memry allocated. Instead it uses the class space -- some kind of static allocation.

    If you were to allocate a class instead of the struct, then class Program would actually contain a reference to your other class, which would be allocated on the heap.
    But using a struct makes class Program containing the actual data in its own memory blocks.

    regards

  3. #3
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: stack, heap and C#

    sorry, but I don't understand. I'm very confused. I read value types go on the stack and ref type on the heap (I hope this right):
    Qt Code:
    1. //MyClass class is defined elsewhere
    2. class Program {
    3. struct Address {
    4. public Address (string v, int n) {
    5. this.street = v;
    6. this.number = n;
    7. }
    8. internal string street;
    9. internal int number;
    10. };
    11. MyClass cl = new myClass();
    12. Address addr = new Address ("abbay",99);
    13. public Int32 m = 9;
    14. static void Main(string[] args) {
    15. System.Int32 i = 99;
    16. }
    17. }
    To copy to clipboard, switch view to plain text mode 
    AFAIK the main() method must be inside a class (e.g. Program). So I repeat the previous question: Does all that is inside Program class go on the heap?
    If yes, I'm wondering a declaration of any type that goes on the stack; could you insert in my program above a declaration that will be allocate on the stack?
    Probabily i'm still doing wrong....
    Regards

  4. #4
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: stack, heap and C#

    Ok. Once again you're missing the essential.

    Qt Code:
    1. //MyClass class is defined elsewhere
    2. class Program {
    3. struct Address {
    4. public Address (string v, int n) {
    5. this.street = v;
    6. this.number = n;
    7. }
    8. internal string street;
    9. internal int number;
    10. };
    11. MyClass cl = new myClass(); //Allocated on the heap. Will have its own heap space
    12. Address addr = new Address ("abbay",99); //Will be allocated directly in the Progream class heap space since it is a value type. addr does not point to a separate heap location but to a location relative to the classes memory space base address.
    13. public Int32 m = 9; //Value type, therefore allocated in class space
    14. static void Main(string[] args) {
    15. System.Int32 i = 99; // allocated on main() stack
    16. Address a = new Address("x", 10); //allocated on the current function's stack
    17. }
    18. }
    To copy to clipboard, switch view to plain text mode 
    Does all that is inside Program class go on the heap?
    Yes but depends... Value types go on the class's heap, while ref types get their own heap.
    Regards
    Last edited by marcel; 18th August 2007 at 19:06.

  5. #5
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: stack, heap and C#

    //error to remove, please
    Last edited by mickey; 18th August 2007 at 23:41.
    Regards

  6. #6
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: stack, heap and C#

    Sorry......
    Qt Code:
    1. //myclass.cs
    2. MyClass {
    3. public int a;
    4. public void swapl(ref x, ref y) {
    5. int temp;
    6. temp = x;
    7. x = y;
    8. y = temp;
    9. }
    10. }
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. Class Program {
    2. .........................................
    3. static void Main(string[] args) {
    4. System.Int32 i = 99, u = 10; // 1
    5. MyClass simple = new MyClass(); //2
    6. simple.swap (i,u); //XX
    7. Address a = new Address("x", 10); // 3
    8. }
    9. }
    To copy to clipboard, switch view to plain text mode 
    //1 - value type; allocate on the stack because local variabile of a function (call
    function)
    //2 - ref type; local class; it'll die with end of main(); on the stack of main () ????
    //3 - value type - local struct; on the main () 's stack.
    Now: when the application arrival to execute //XX inside main(), everything inside Myclass::swap goes on the stack (I hope). But why? because is it inside main() function or because is it a call function, independently from where it is?

    Then: where is allocated Program class??? Who create an instance of Program??
    thanks
    Regards

  7. #7
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: stack, heap and C#

    It is:
    Qt Code:
    1. //myclass.cs
    2. MyClass {
    3. public int a;
    4. public void swapl(ref int x, ref int y) {
    5. int temp;
    6. temp = x;
    7. x = y;
    8. y = temp;
    9. }
    10. }
    To copy to clipboard, switch view to plain text mode 

    You actually have to specify the variable type even when passing vaue types by reference.


    1 - Allocated on the stack of main() because they are value types and you create them in main. Will be dealocated when main exits.

    2 - No. For the third time, reference types are allocated on the heap, no matter where you create them. The class will be deallocated by the garbage collector, not necessarily when main exits.

    3 - Correct

    Now: when the application arrival to execute //XX inside main(), everything inside Myclass::swap goes on the stack (I hope). But why? because is it inside main() function or because is it a call function, independently from where it is?
    Because it is inside the swap function. It is like in C++. Do you know C++?

    Then: where is allocated Program class??? Who create an instance of Program??
    I am not sure of this. I am not sure even if the main class gets instantiated as one would normally do.
    But usually, the class that contains the entry point in the application( the Main() function ) should not provide any functionality, meaning that you don't implement in this class things that you will use in other parts of the application. Therefore you won't ever need to instantiate the class manually.

    Regards

  8. #8
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: stack, heap and C#

    Hi, this continue to confuse me:
    Yes but depends... Value types go on the class's heap, while ref types get their own heap.
    In the last post you said "value type" goes on the stack; here above no. value type goes on the stack or on the class' heap ? (I guess in this case we're speaking of eg Int32 declared inside a class eg int a ot int temp in my last example). In this wai I understand thta only value types that go on the stack are those are in the main() function and not else where; is it so?
    Regards

  9. #9
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: stack, heap and C#

    Whatever... I say to skip things you don't understand, and you'll get them along the way.

    Yes but depends... Value types go on the class's heap, while ref types get their own heap.
    I was referring strictly to member objects.

    I suggest taking C++ and C#, put them side by side and compare them. The basic concepts are almost the same.

    Regards

Similar Threads

  1. Simple: Operator overloading with heap objects
    By durbrak in forum General Programming
    Replies: 12
    Last Post: 25th April 2007, 14:20
  2. QString .toStdString heap bug
    By bpetty in forum Qt Programming
    Replies: 1
    Last Post: 3rd October 2006, 18:10
  3. Heap Error
    By avis_phoenix in forum Qt Programming
    Replies: 2
    Last Post: 8th August 2006, 04:15
  4. QThread and heap
    By paranoid_android in forum Qt Programming
    Replies: 2
    Last Post: 9th March 2006, 11:13

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.