Click here to Skip to main content
15,898,984 members
Home / Discussions / C#
   

C#

 
SuggestionRe: Table Adapter Pin
Richard MacCutchan5-Feb-15 21:59
mveRichard MacCutchan5-Feb-15 21:59 
QuestionReorganise algorithm Pin
sebyku5-Feb-15 11:27
sebyku5-Feb-15 11:27 
AnswerRe: Reorganise algorithm Pin
DelphiCoder5-Feb-15 17:24
DelphiCoder5-Feb-15 17:24 
QuestionImport C function from DLL with passing FILE stream pointer as parameter Pin
Miloš Križan5-Feb-15 9:26
Miloš Križan5-Feb-15 9:26 
AnswerRe: Import C function from DLL with passing FILE stream pointer as parameter Pin
Richard Andrew x645-Feb-15 14:41
professionalRichard Andrew x645-Feb-15 14:41 
QuestionSocket IAsyncResult - Catch SocketError Pin
sheldons5-Feb-15 8:47
sheldons5-Feb-15 8:47 
SuggestionRe: Socket IAsyncResult - Catch SocketError Pin
Richard MacCutchan5-Feb-15 21:56
mveRichard MacCutchan5-Feb-15 21:56 
GeneralRe: Socket IAsyncResult - Catch SocketError Pin
sheldons6-Feb-15 5:59
sheldons6-Feb-15 5:59 
GeneralRe: Socket IAsyncResult - Catch SocketError Pin
Richard MacCutchan6-Feb-15 6:01
mveRichard MacCutchan6-Feb-15 6:01 
QuestionWord document converted to image is showing transparent background Pin
Member 103642244-Feb-15 22:27
professionalMember 103642244-Feb-15 22:27 
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 

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.