Click here to Skip to main content
15,888,065 members
Home / Discussions / C#
   

C#

 
AnswerRe: Word document converted to image is showing transparent background Pin
BillWoodruff5-Feb-15 0:58
professionalBillWoodruff5-Feb-15 0:58 
AnswerRe: Word document converted to image is showing transparent background Pin
DelphiCoder5-Feb-15 17:50
DelphiCoder5-Feb-15 17:50 
QuestionDataGridView Pin
Member 113627094-Feb-15 21:25
Member 113627094-Feb-15 21:25 
QuestionDataGridView Pin
Member 113627094-Feb-15 21:14
Member 113627094-Feb-15 21:14 
AnswerRe: DataGridView Pin
Richard MacCutchan4-Feb-15 21:34
mveRichard MacCutchan4-Feb-15 21:34 
QuestionStack and heap Pin
Member 111616254-Feb-15 19:49
Member 111616254-Feb-15 19:49 
AnswerRe: Stack and heap Pin
Richard MacCutchan4-Feb-15 21:33
mveRichard MacCutchan4-Feb-15 21:33 
AnswerRe: Stack and heap PinPopular
OriginalGriff4-Feb-15 22:00
mveOriginalGriff4-Feb-15 22:00 
They are both memory, but...
THe Heap is a big chunk of memory (up to the max that your system can provide) that is used to allocate objects via the new keyword: whenever you allocate a class instance (rather than a struct) you are creating the actual object on the Heap, where it will remain until the Garbage Collector decides it is no longer being used and recycles the memory back into the "pool" of available Heap memory. This means that any object has the potential to be accessed anywhere in your application once it has been created.

The Stack is different: it's a lot smaller (typically 1MB for Windows apps, 256 KB for 32-bit ASP.NET apps and 512 KB for 64-bit ASP.NET apps) and it is part of the Thread that is executing. It's also allocated differently: imagine a stack of coins. When you enter a method, you place a little piece of paper on the top of the coin stack, and every time you create a variable (value type or a reference to a reference type) you add a coin to represent the new variable. When you leave the method you remove the piece of paper, and all the coins above it. That's roughly how the stack works: which means that any variable on the stack is available only until the method exits at which point it is destroyed immediately.

So:
C#
public MyClass MyMethod(string name, int count)
   {
   int realCount = count + 1;
   MyClass val = new MyClass();
   val.Name = name;
   val.Count = realCount;
   return val;
   }

C#
public MyClass MyMethod(string name, int count)
You enter the method, and insert your piece of paper.
C#
int realCount = count + 1;
You create a value type variable on the stack called "realCount" and assign it a value. Add a coin to your coin stack
C#
MyClass val = ...
You create a MyClass reference variable on the stack called "val" - this is a simple variable that can hold only a reference to a MyClass instance, but as yet contains nothing at all. Add another coin to your coin stack.
C#
... = new MyClass();
You create an instance of MyClass on the Heap and assign the reference to it to "val".
C#
val.Name = name;
val.Count = realCount;
Now, you can use the reference in "val" to access the MyClass properties and methods.
C#
return val;
You return the reference to the MyClass instance to the method that called MyMethod
C#
}
You exit the method.
At this point the "piece of paper" and all the coins above it.
"val" and "realCount" are no longer accessible by anything because the stack memory has been reclaimed - but the MyClass instance you created still exists on the Heap and can be used by the method that called MyMethod.
The next call to any method will reuse the same stack memory locations for it's variables that MyMethod used for "val" and "realCount".

Make any sense?
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...

GeneralRe: Stack and heap Pin
Rob Philpott5-Feb-15 4:53
Rob Philpott5-Feb-15 4:53 
GeneralRe: Stack and heap Pin
DelphiCoder5-Feb-15 17:56
DelphiCoder5-Feb-15 17:56 
AnswerRe: Stack and heap Pin
BillWoodruff5-Feb-15 0:56
professionalBillWoodruff5-Feb-15 0:56 
Questionsetting user permissions to winform controls c# Pin
Member 111236393-Feb-15 23:33
Member 111236393-Feb-15 23:33 
AnswerRe: setting user permissions to winform controls c# Pin
OriginalGriff4-Feb-15 0:06
mveOriginalGriff4-Feb-15 0:06 
GeneralRe: setting user permissions to winform controls c# Pin
Member 111236394-Feb-15 0:24
Member 111236394-Feb-15 0:24 
GeneralRe: setting user permissions to winform controls c# Pin
OriginalGriff4-Feb-15 0:42
mveOriginalGriff4-Feb-15 0:42 
GeneralRe: setting user permissions to winform controls c# Pin
Member 111236394-Feb-15 0:48
Member 111236394-Feb-15 0:48 
GeneralRe: setting user permissions to winform controls c# Pin
OriginalGriff4-Feb-15 1:33
mveOriginalGriff4-Feb-15 1:33 
GeneralRe: setting user permissions to winform controls c# Pin
Member 111236394-Feb-15 1:42
Member 111236394-Feb-15 1:42 
GeneralRe: setting user permissions to winform controls c# Pin
manchanx4-Feb-15 2:09
professionalmanchanx4-Feb-15 2:09 
GeneralRe: setting user permissions to winform controls c# Pin
BillWoodruff4-Feb-15 6:08
professionalBillWoodruff4-Feb-15 6:08 
QuestionERROR:Only one usage of each socket address (protocol/network address/port) is normally permitted Pin
Prabhu Sridharan3-Feb-15 21:03
Prabhu Sridharan3-Feb-15 21:03 
AnswerRe: ERROR:Only one usage of each socket address (protocol/network address/port) is normally permitted Pin
Richard MacCutchan3-Feb-15 22:49
mveRichard MacCutchan3-Feb-15 22:49 
QuestionTerminated due to timeout.. Working properly with small input like 100 but not with large example 10000 Pin
Member 114256643-Feb-15 20:25
Member 114256643-Feb-15 20:25 
AnswerRe: Terminated due to timeout.. Working properly with small input like 100 but not with large example 10000 Pin
Mycroft Holmes3-Feb-15 20:57
professionalMycroft Holmes3-Feb-15 20:57 
AnswerRe: Terminated due to timeout.. Working properly with small input like 100 but not with large example 10000 Pin
BillWoodruff3-Feb-15 21:04
professionalBillWoodruff3-Feb-15 21:04 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.