Click here to Skip to main content
15,906,766 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
i cannot find definition and use of malloc can somebody explain it to me via. example
Posted
Updated 18-Jan-14 5:06am
v2

C# does not have, or use malloc in any way, shape or form.

.NET applications are Managed: which means they have their own memory management built in (via the new keyword) and which is tied into the Garbage Collection system. They do not use malloc, or free as the memory is handled by the system for you.
 
Share this answer
 
Comments
Rahul VB 18-Jan-14 10:29am    
Fantastic OG 5+!
OriginalGriff 18-Jan-14 11:22am    
Afternoon Rahul!
What are you doing working on a Saturday? :laugh:
Rahul VB 18-Jan-14 11:27am    
oh gosh how do you know?:laugh: no no. Just planning for tonight with friends, what else. Afternoon? its 10 pm now! Its party time OG. Enough of this week. I am enjoying bachelorhood no girl, no nagging hahah :laugh:.
OriginalGriff 18-Jan-14 11:34am    
Enjoy!
Griff is right (mostly) :)

99.999% of the time, working with objects in .NET you will only use the "new" operator, which works with managed memory.

However, there are rare circumstances that you need to allocate unmanaged memory, just like you would with malloc. .NET does provide a method for doing this with the Marshal class, specifically the Marshal.AllocHGlobal[^] and Marshal.FreeHGlobal[^].

Its also worth noting that you don't really want to use the above unless you are working with some specialized unmanaged code. Allocating memory and forgetting to free it creates memory leaks.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900