You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: content/posts/cpp-for-cscharp.md
+8-6Lines changed: 8 additions & 6 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -278,8 +278,8 @@ the recommended way to use the standard library for plain C in modern C++ code.
278
278
**Stack / Heap:**
279
279
280
280
C++ allows you to initialize objects on the stack or the heap. But In C# you mostly don't get to choose, classes will be on the heap most of the time like most other reference types,
281
-
and structs on the stack most of the time because they are value types, but structs are in some cases not on the stack but on the heap too, this is something C# decides at runtime based on a number of factors like its scope and more.
282
-
So generally speaking in C# you have not much control over what gets allocated on the stack and what on the heap.
281
+
and structs on the stack most of the time like most other value types, but structs are in some cases not on the stack but on the heap too, this is something C# decides at runtime based on a number of factors like for example its scope.
282
+
So generally speaking in C# you have not much control over what gets allocated on the stack and what on the heap. But generally you can assume reference types are on the heap, and value types are on the stack.
283
283
284
284
and in C# you must always use the ``new`` keyword when creating an object it doesnt matter if the object you are creating is a reference or value type or if its on the stack or on the heap, ``new`` always gets used,
285
285
while in C++ ``new`` means initialization on the heap. So if you want to initialize something on the heap in C++, just pick any of the normal ways you would initialize/create a variable on the stack, and use the ``new`` keyword
@@ -337,10 +337,12 @@ Type test = [x, y, z]; // works on collections
337
337
**Things to remember about initialization:**
338
338
339
339
- C++ gives more control over stack vs heap
340
-
- C++ gives more control about when copy operations are used
341
-
- C++ the semantics of the new keyword are completely different
342
-
- C# decides for you most of the time if stack or heap is used
343
-
- C# always requires the new keyword for both structs and classes
340
+
- C++ the new keyword is for heap allocation
341
+
- C++ gives more control about when a copy is performed
342
+
- C++ generally speaking curly brace initialization is best
343
+
- C# always requires the new keyword for any object creation
344
+
- C# most of the time reference types are on the heap but not always
345
+
- C# most of the time value types are on the stack but not always
0 commit comments