malloc & free in c# 
Author Message
 malloc & free in c#

For example, if i have c Code like this.

===========c ========================
void test(int n)
{
   int* p;
   p = ( int*)malloc( sizeof(int)*n );
   free(p);

Quote:
}

How can i convert this code to C#?

I think memory allocation will be done by itself. But I don't know How
to free the memory.

If I let GC to collect obselete object it will take about 0.1 sec per
1~2 seconds. It is critical for real time program(ex: game).

How can I collect obselete object and free the memory it has?



Tue, 21 Jun 2005 16:55:57 GMT  
 malloc & free in c#
void test(int n)
{
    int[] p = new int[n];
    // do something with p

Quote:
}

You don't need to free p, the garbage collector will do it for you.

Note: the garbage collector is smart: if you assign p to a global variable
or to a field of an object inside test, the gc will notice that the array is
still referenced after the test method is existed, and it won't free it (but
it may free it later, for example if the global variable is reassigned or
set to null). This is magic!

Bruno.



Quote:
> For example, if i have c Code like this.

> ===========c ========================
> void test(int n)
> {
>    int* p;
>    p = ( int*)malloc( sizeof(int)*n );
>    free(p);
> }

> How can i convert this code to c#?

> I think memory allocation will be done by itself. But I don't know How
> to free the memory.

> If I let GC to collect obselete object it will take about 0.1 sec per
> 1~2 seconds. It is critical for real time program(ex: game).

> How can I collect obselete object and free the memory it has?



Tue, 21 Jun 2005 17:33:15 GMT  
 malloc & free in c#

Quote:

> If I let GC to collect obselete object it will take about 0.1 sec per
> 1~2 seconds. It is critical for real time program(ex: game).

Have you tried this, or are you guessing?

-Marco



Tue, 21 Jun 2005 17:50:04 GMT  
 malloc & free in c#

I have not tried. But I saw a post in www.gamedev.net forum. The post
has that phase. So... I just believed it.   It may not true for other
programming. But I think it may be true for 3d first-person view game
written by c#.

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!



Tue, 21 Jun 2005 22:08:07 GMT  
 malloc & free in c#


Quote:

> I have not tried. But I saw a post in www.gamedev.net forum. The post
> has that phase. So... I just believed it.   It may not true for other
> programming. But I think it may be true for 3d first-person view game
> written by c#.

You need to be familiar with the how the GC works to get it to
behave how you need it to.

If you allocate a bunch of reference types and dereference them
frequently,you're going to get lots of garbage collection
which will slow you down.

When working with time critical apps and when you have a lot
of short-lived objects (like each frame in a game), you
should be using value types to an extent. This will save
expensive heap allocation and garbage collection later on.

Also, you can force the GC to collect when you know it's
a good time (like at the end of a scene) to help prevent
it from going off when you don't want it to.

I remember seeing some good articles on this posted awhile
ago, but I don't have the URLs. Do a google for this and
I will too. If I find anything I'll post back.

-c



Wed, 22 Jun 2005 01:14:05 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. Malloc & free acting strange

2. simple question about malloc & free

3. ?malloc & free vs. new & delete

4. new & delete VS malloc & free

5. How can i Do malloc & free in c#?

6. Replacing malloc & free for libc functions?

7. malloc() & free() behaviour?

8. malloc&free

9. malloc/calloc & free

10. memory to free or not to free, to malloc or not to malloc ?

11. Override malloc,calloc,realloc and free?

12. malloc/free reference implementations

 

 
Powered by phpBB® Forum Software