|
|
HI,
Everybody,
I want to know that what is stack and heap in C#?What is it's difference???
Thanks...
In Advance...
|
|
|
|
|
Pick up any C# book, or use Google.
|
|
|
|
|
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:
public MyClass MyMethod(string name, int count)
{
int realCount = count + 1;
MyClass val = new MyClass();
val.Name = name;
val.Count = realCount;
return val;
}
public MyClass MyMethod(string name, int count) You enter the method, and insert your piece of paper.
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
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.
... = new MyClass(); You create an instance of MyClass on the Heap and assign the reference to it to "val".
val.Name = name;
val.Count = realCount;
Now, you can use the reference in "val" to access the MyClass properties and methods.
return val;
You return the reference to the MyClass instance to the method that called MyMethod
}
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...
|
|
|
|
|
You should write a book!
Regards,
Rob Philpott.
|
|
|
|
|
I think that he did just write a book. LOL....
|
|
|
|
|
imho the "best explanations" of .NET memory allocation using Stack and Heap are contained in three blog entries by Eric Lippert (a "major player" in the creation of .NET, and a very articulate writer): [^], [^], [^].
If these three articles were a movie, I'd give it the tag-line: "Memory moves in mysterious ways."
«I'm asked why doesn't C# implement feature X all the time. The answer's always the same: because no one ever designed, specified, implemented, tested, documented, shipped that feature. All six of those things are necessary to make a feature happen. They all cost huge amounts of time, effort and money.» Eric Lippert, Microsoft, 2009
|
|
|
|
|
am new to c# programming and for 3 months i've been learning and writing an application which am almost done, i have my user authen table for login purpose,profile table, features table, actions table as well as permission table in place in Msql database and also successful in inserting new permissions to database from winform and assigning those profile to a user. what am left to do is give permission right to users logon to access certain controls on my numerous winforms. am actually stuck here and dont know how to link application to read from database what permissions was assigned to a user and what controls to deny or permit. any help as to how to achieve this would be appreciated thank you.
Question is how do i make application read permission already assigned to a user and block or enable a control based on permissions assigned to that user.
|
|
|
|
|
We really can't give you a specific reply to this: we have no idea at all how your app works.
But I'd assume that you have a "logged in user" instance of a User class or similar - and I'd keep permissions as an enum property of that class, most likely.
So as part of your various Form.Load event handlers, check your user permissions and either set the appropriate Control.Enable or Control.Visible properties to false if the aren't allowed. (Or better, default to false and set to true if they are allowed).
But myself, I wouldn't do that - I would have "admin only" forms, which are only displayed if the user is permitted, and disable access to those instead.
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
|
|
|
|
|
it works pretty simple, during development phase i had to manually insert permission by IDs in permissions table and assigned to a profile table by profile ID. but now am able to insert permissions on winform by checking and unchecking checkboxs and saves them to database so now all i want to is to read those permissions stored when a user logs in and also in the permissions table are columns for actions which are buttons found on all winform so if that particular user do not have that action then it should be disabled when that form is opened.
|
|
|
|
|
Yes - but we have no idea how the rest of your application works!
You can clearly access the DB (or you couldn't log them in at all) so read the access permissions then, and store them as part of your logged in user.
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
|
|
|
|
|
could u please demonstrate that thanks
|
|
|
|
|
Demonstrate what? How to use an enum?
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
|
|
|
|
|
|
|
Congratulatons on getting as far as you've gotten on this project !
I recently wrote a long post here in response to a question on the "Design and Architecture Forum" describing what I think is fairly secure approach to having a WinForms Application provide customized forms to use based on Groups/Roles: [^].
CHill's response here: [^] to the first post of that question may be of interest to you.
I think you might find some ideas on that thread, and I'll be happy to respond to any questions you may have about my post.
«I'm asked why doesn't C# implement feature X all the time. The answer's always the same: because no one ever designed, specified, implemented, tested, documented, shipped that feature. All six of those things are necessary to make a feature happen. They all cost huge amounts of time, effort and money.» Eric Lippert, Microsoft, 2009
|
|
|
|
|
I have the following error "Only one usage of each socket address (protocol/network address/port) is normally permitted" in my coding. Need Help!!
|
|
|
|
|
Please edit your question and show the code that creates the error.
|
|
|
|
|
Angry Children Problem of hackerrank
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
class Solution
{
static void Main(String[] args)
{
long N = Convert.ToInt64(Console.ReadLine());
long K = Convert.ToInt64(Console.ReadLine());
long[] list = new long[N];
for (long i = 0; i < N; i++)
{
list[i] = (Convert.ToInt64(Console.ReadLine()));
}
long b;
for (long k = 0; k < list.Length - 1; k++)
{
for (long i = 0; i < list.Length - k - 1; i++)
{
if (list[i] < list[i + 1])
{
b = list[i + 1];
list[i + 1] = list[i];
list[i] = b;
}
}
}
long x = list[0] - list[N - 1];
long j = K - 1;
for (long i = 0; i <=N-K ; i++)
{
if ((list[i] - list[j]) < x)
x = list[i] - list[j];
j++;
}
Console.WriteLine(x);
/* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution */
}
}
|
|
|
|
|
Is there a reason you are posting your homework here?
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
What benefit to you would it be if you get a solution to a challenge on HackerRank [^] that is not your solution ?
«I'm asked why doesn't C# implement feature X all the time. The answer's always the same: because no one ever designed, specified, implemented, tested, documented, shipped that feature. All six of those things are necessary to make a feature happen. They all cost huge amounts of time, effort and money.» Eric Lippert, Microsoft, 2009
|
|
|
|
|
Hi,
First time poster long time reader.
Im having trouble with a shdocvw.dll and accessing internet pages. Particulary interacting with them.
1.my setup windows 8 64bit running visual studio 2012, ie11.
Im only having the trouble on 64 bit machines, 32 bit machines run the program fine. i wrote a sample a small sample to show you want i mean: The following runs fine on the x86 computers and i get my hello world message... but any 64bit machines i get an error. i have tried runing in x86 mode only, x64 mode only all error.
This is just a sample code to demonstrate whats doesnt work.
SHDocVw.InternetExplorer ie = new SHDocVw.InternetExplorer();
ie.Navigate("https://www.google.com.au");
while (ie.Busy)
{
}
ie.Visible = true;
string javaScriptcode = "alert('hello world !!')";
ie.Document.parentWindow.execScript(javaScriptcode, "javascript");
Ive run on ie 11 and ie8 on a x86 machines no worries. on the x64 machine with the the same ie security settings etc on ie11 give the error:
An unhandled exception of type 'System.UnauthorizedAccessException' occurred in System.Dynamic.dll
Additional information: Access is denied.
Id appreciate any help as ive exhausted google. Whats got me stumped is why it works in x86 machines but not in x64
David
|
|
|
|
|
In which line does the error occur?
SHDocVw.InternetExplorer ie = new SHDocVw.InternetExplorer() ; creates a new instance of Internet Explorer, and an iexplore process becomes visible in e.g. Task Manager.
Is that a 32bit or a 64bit process?
Did you check the security settings of that specific version, in comparison to IE on 32bit systems?
|
|
|
|
|
Thank you Bernhard,
Sorry i should have said its on the exescipt line calling java script.
checked and its shdocvw is launching 32 bit verison which is good.
When you talked about security settings i checked. From what i can see there all the same... I then read something about protected mode. where i read that if you use shdocvw you will run in protected mode.
from the msdn.
Quote: Furthermore, extensions that attempt to gain write access to securable objects by using an API function in one of the following binary files will receive Access Denied errors.
actxprxy.dll ieui.dll mswsock.dll sensapi.dll
Advapi32.dll iexplore.exe NAPINSP.dll Shdocvw.dll
Would that be whats going on? I still cant understand why its only happening on 64 bit machines.
|
|
|
|
|
Are you using SHDocVw as a control within your application or are you using SHDocVw to startup Internet Explorer and then attempting to communication with an open IE window?
If the latter, then setting up a secure HTTPS connection is going to resist your injecting JavaScript into IE. x64 versions of windows have more strict protection mechanisms in place when communicating with 32-bit applications. Have you tried an HTTP connection instead of HTTPS?
Lastly, using the browser controls included in .NET work well if you modify the browser's emulation mode[^] for your application. You must do this to use HTML5 in your desktop application or if will look and run like crap. After changing the emulation mode in the registry, I find the included WebBrowser control does all I ask of it. HTML5 within the WebBrowser control rocks.... I kicked SHDocVw to the curb years ago.
Later.....
|
|
|
|