free() function translated into C# equivalent - commentsappreciated...

A

almurph

Hi,

Wondering can you help me with this one. I'm translating some C into
C# and have come accross the following line of code in C:

free(A)


where "A" is an array of type double. I'm trying to translate/
approximate that line into C# and am wondering what is the nearest
equivalent? I know "free()" function frees up some memory in C so I'm
thing of calling the garbabe collector at that point ie.

GC.Collect();


Does free reset the elements of the array? If so i could reset them
all to zero or just deleted everything in the array...

Would appreciate any comments/ideas/suggestions/approximations/code-
samples that you may be willing to share.

Thanking you,
Al.
 
L

Lew Pitcher

Hi,

Wondering can you help me with this one. I'm translating some C into
C# and have come accross the following line of code in C:

free(A)

where "A" is an array of type double.

No, "A" isn't an array of type double, if it is properly used in your
free(A);
statement
I'm trying to translate/
approximate that line into C# and am wondering what is the nearest
equivalent? I know "free()" function frees up some memory in C [snip]
Does free reset the elements of the array? If so i could reset them
all to zero or just deleted everything in the array...

Well, we can't answer C# questions; that would be a job for a newsgroup
dedicated to C#. You could ask the C# part of your question in
microsoft.public.dotnet.csharp.general or one of the other C# newsgroups

As for free(A), it returns to a "freepool" a block of storage allocated
using one of the alloc() functions. To figure out what the C# equivalent of
free() is, you probably need to know what the C alloc() and free()
functions do. Our resident experts can answer that one better than I.
However, I'll give it a try and let them correct me. ;-)

You start with a variable that is defined as a pointer (of some type).

Initially, this variable points nowhere; you can't use it to store or
retrieve information yet. However, what you do is set this pointer to the
value returned by an alloc() call; the alloc() call will allocate an amount
of memory from a pool of available memory (I'll call this a "freepool"),
and will return a pointer to the beginning of that allocated memory. The
memory block is of fixed size (the size you asked for as part of the
alloc() call, and is aligned in such a manner that any data type can
properly sit in the block.

Now, your variable points to a block of memory. You can use this block of
memory like an array. You can pass the pointer around, make copies of it,
and do pretty much anything with it and the memory that you could do with a
variable that had been allocated through the builtin manners ("auto"
or "static" or the default).

When you are done with the block of memory, you pass the original pointer to
the free() function, which does it's magic. You aren't concerned
about /what/ magic it does (it usually "returns" the block to a "freepool"
where it might be given out to another alloc() call; it /might/ initialize
the block to a fixed value, but it probably doesn't); you only know that
the specific pointer value you had is now unusable (any other pointers
derived from that pointer value are likewise unusable), and the storage
space the pointer(s) pointed to is no longer available for your use.

HTH
--
Lew Pitcher

Master Codewright & JOAT-in-training | Registered Linux User #112576
http://pitcher.digitalfreehold.ca/ | GPG public key available by request
---------- Slackware - Because I know what I'm doing. ------
 
R

Richard Bos

Wondering can you help me with this one. I'm translating some C into
C# and have come accross the following line of code in C:

free(A)

where "A" is an array of type double.

You can't free() an array. You can only free() memory which you have
previously allocated using malloc() or a related function. (And you can
free() a null pointer, for convenience.)
I'm trying to translate/approximate that line into C# and am wondering
what is the nearest equivalent?

Can't help you with C#, I'm afraid, but here's what free(ptr) does:

- if ptr does not hold a pointer value you previously got from an
allocation function, nor a null pointer: has undefined behaviour.
- if ptr holds a null pointer: does nothing.
- if ptr holds a value that was previously received from malloc() or
chums, deallocates the memory at that spot in memory. Nothing else.
It does not wipe any memory. It does not _guarantee_ that you can no
longer access that memory, but it would be highly inadvisable to
gamble on anything. The pointer is now invalid for _all_ purposes,
even (potentially, at least) for comparing to other pointers.

If A, above, was declared as a real array of doubles, such as

double A[A_size]={1.5, 6.34};

then calling free(A) is a bug. If, OTOH, it was declared as a pointer to
double, either as

double *A;

or (confusingly) in a function declaration where it _looks_ like an
array but really is a pointer, like this:

int func(double A[], double *B)

(after which both A and B are, within that function, pointers to double,
_not_ arrays), then whether you can free(A) depends on how memory for A
was allocated (including how func() was called).

Richard
 
A

almurph

   Wondering can you help me with this one. I'm translating some C into
C# and have come accross the following line of code in C:

   where "A" is an array of type double.

You can't free() an array. You can only free() memory which you have
previously allocated using malloc() or a related function. (And you can
free() a null pointer, for convenience.)
I'm trying to translate/approximate that line into C# and am wondering
what is the nearest equivalent?

Can't help you with C#, I'm afraid, but here's what free(ptr) does:

- if ptr does not hold a pointer value you previously got from an
  allocation function, nor a null pointer: has undefined behaviour.
- if ptr holds a null pointer: does nothing.
- if ptr holds a value that was previously received from malloc() or
  chums, deallocates the memory at that spot in memory. Nothing else.
  It does not wipe any memory. It does not _guarantee_ that you can no
  longer access that memory, but it would be highly inadvisable to
  gamble on anything. The pointer is now invalid for _all_ purposes,
  even (potentially, at least) for comparing to other pointers.

If A, above, was declared as a real array of doubles, such as

  double A[A_size]={1.5, 6.34};

then calling free(A) is a bug. If, OTOH, it was declared as a pointer to
double, either as

  double *A;

or (confusingly) in a function declaration where it _looks_ like an
array but really is a pointer, like this:

  int func(double A[], double *B)

(after which both A and B are, within that function, pointers to double,
_not_ arrays), then whether you can free(A) depends on how memory for A
was allocated (including how func() was called).

Richard

Richard,

Thanks, your right - "A" is a pointer not just an array. Still not
sure what to do - i think the best in this case (C#) is to do
nothing...

Al.
 
K

Keith Thompson

Lew Pitcher said:
As for free(A), it returns to a "freepool" a block of storage allocated
using one of the alloc() functions.
[...]

Just to be clear, the alloc() functions are malloc(), calloc(), and
realloc().

And here's what the standard says about free():

The free function causes the space pointed to by ptr to be
deallocated, that is, made available for further allocation. If
ptr is a null pointer, no action occurs. Otherwise, if the
argument does not match a pointer earlier returned by the calloc,
malloc, or realloc function, or if the space has been deallocated
by a call to free or realloc, the behavior is undefined.

There have been long discussions here about just what "made available
for further allocation" means.

Bottom line: free() deallocates allocated memory. Your question is
how (and perhaps whether) to do this in C#; that's a C# question, not
a C question.
 
B

Ben Bacarisse

Thanks, your right - "A" is a pointer not just an array. Still not
sure what to do - i think the best in this case (C#) is to do
nothing...

If, as your post suggests, C# has GC, then calling the collector
instead of free will have no effect at all. The CG will still find
the memory via the pointer A.

I suspect the you are right that doing nothing is often the correct
translation. It will depend on the lifetime of the pointer variable.
If it is long-lived, setting A to C#'s version of NULL will allow the
GC to collect the space. If you want to force a collection at that
time it, feel free, but it is unlikely to matter.
 
J

jacob navia

Hi,

Wondering can you help me with this one. I'm translating some C into
C# and have come accross the following line of code in C:

free(A)


where "A" is an array of type double. I'm trying to translate/
approximate that line into C# and am wondering what is the nearest
equivalent? I know "free()" function frees up some memory in C so I'm
thing of calling the garbabe collector at that point ie.

GC.Collect();


Does free reset the elements of the array? If so i could reset them
all to zero or just deleted everything in the array...

Would appreciate any comments/ideas/suggestions/approximations/code-
samples that you may be willing to share.

Thanking you,
Al.

The equivalent of free(A) is

A = NULL;

This will signal the GC that the memory pointed to by A is free.
 
R

Richard Tobin

jacob navia said:
The equivalent of free(A) is

A = NULL;

This will signal the GC that the memory pointed to by A is free.

Provided, presumably, that nothing else points to the same memory.

-- Richard
 
B

Bartc

Hi,

Wondering can you help me with this one. I'm translating some C into
C# and have come accross the following line of code in C:

free(A)

How did you translate the corresponding malloc() or similar function to
create A? That might give a clue.
 
A

almurph

How did you translate the corresponding malloc() or similar function to
create A? That might give a clue.

Hi Bartc,

Thanks, to answer ur question, the author is doing stuff like:

double *A;

A = (double *) calloc(MaxValue+1, sizeof(double));

A is then populated
A is then used
free(A);

then the cycle begins all over again as this routine is inside a
function that can call itself (self-recursion)

Hope this help. I'm nearly sure that if I do nothing in C# it will
still work as expected. in face I havn't even implemented a pointer. i
think i can get away with it.
Comments appreciated though,
Al.
 
B

Bartc

Thanks, to answer ur question, the author is doing stuff like:

double *A;

A = (double *) calloc(MaxValue+1, sizeof(double));

A is then populated
A is then used
free(A);

OK, but how do you translate the creation of A, into C#? What ever method
you used can determine whether and how you free A afterwards.
 
K

Keith Thompson

Thanks, to answer ur question, the author is doing stuff like:

It's spelled "your", not "ur". This isn't a chat room.
double *A;

A = (double *) calloc(MaxValue+1, sizeof(double));

A is then populated
A is then used
free(A);

Ah, you're not just translating C to C#, you're translating *bad* C
to C#.

The cast is unnecessary and can mask errors. The use of calloc
assumes that all-bits-zero is the representation for 0.0, something
that's not guaranteed by the language; this might be acceptable in
code that's not intended to be portable. If it then assigns values to
the array elements, then using calloc was simply a waste of time.

Here's a better way:

double *A;
A = malloc(*MaxValue + 1) * sizeof *A);

The array elements are initially garbage, but since the array is then
immediately populated, that shouldn't matter.
then the cycle begins all over again as this routine is inside a
function that can call itself (self-recursion)

Hope this help. I'm nearly sure that if I do nothing in C# it will
still work as expected. in face I havn't even implemented a pointer. i
think i can get away with it.

If you're asking how to write C#, why don't you ask in a C# forum?
We can tell you what the C code means, but we can't help with C#
questions.
 
B

Ben Bacarisse

Here's a better way:

double *A;
A = malloc(*MaxValue + 1) * sizeof *A);

Off by one key to the left. You intended to type:

A = malloc((MaxValue + 1) * sizeof *A);
 
K

Keith Thompson

Ben Bacarisse said:
Off by one key to the left. You intended to type:

A = malloc((MaxValue + 1) * sizeof *A);

D'oh! My cat must have stepped on the '*' key at just the wrong
moment.
 
I

Ike Naar


You regularly frown upon the use of sms lingo in this forum (and you
have a valid point in doing so).

Just for your information (no offense intended): to a non-native
English speaking person like me, phrases like ``i m'', ``u r'' and
``plz'' are uneasy on the eye, but at least they do make sense.
While ``Duh!'' or ``D'oh!'' mean as much to me as ``aouw meuk!''
would mean to you.

If we are supposed to drop the chatroom speak, then could we drop
the Teletubbie hiccups as well? Please? :)
 
K

Keith Thompson

You regularly frown upon the use of sms lingo in this forum (and you
have a valid point in doing so).

Just for your information (no offense intended): to a non-native
English speaking person like me, phrases like ``i m'', ``u r'' and
``plz'' are uneasy on the eye, but at least they do make sense.
While ``Duh!'' or ``D'oh!'' mean as much to me as ``aouw meuk!''
would mean to you.

If we are supposed to drop the chatroom speak, then could we drop
the Teletubbie hiccups as well? Please? :)

Yes, I guess it is a bit US-specific.

If you're curious: http://en.wikipedia.org/wiki/D'oh

Perhaps in the future I'll just write "annoyed grunt".
 
K

Kaz Kylheku

You regularly frown upon the use of sms lingo in this forum (and you
have a valid point in doing so).

``Doh'' isn't SMS lingo. It comes from the animated television series _The
Simpsons_, and dates back to 1989. It caught on remarkably fast. People in
America used ``doh'' in face-to-face conversation long before instant
messaging. And it has now been a fixture in the Oxford English Dictionary since
2001.
Just for your information (no offense intended): to a non-native
English speaking person like me, phrases like ``i m'', ``u r'' and
``plz'' are uneasy on the eye, but at least they do make sense.
While ``Duh!'' or ``D'oh!'' mean as much to me as ``aouw meuk!''
would mean to you.

``Doh'' has a Wikipedia entry, whereas ``aouw meuk'' is currently
off the Google radar. Are there alternate spellings?
 
K

Kenny McCormack

``Doh'' isn't SMS lingo. It comes from the animated television series
_The Simpsons_, and dates back to 1989. It caught on remarkably fast.
People in America used ``doh'' in face-to-face conversation long before
instant messaging. And it has now been a fixture in the Oxford English
Dictionary since 2001.

Which somehow makes it OK?

What this shows, of course, is that the "anti-SMS-speak" thing in this
newsgroup is just thinly-veiled racism.

It's just proof again that the regs are getting caught on the horns of
infinite reducibility.
 
I

Ike Naar

``Doh'' has a Wikipedia entry,

So have ``u'', ``ur'', ``b4'' and ``plz''.
whereas ``aouw meuk'' is currently off the Google radar.
Are there alternate spellings?

``Ouwe meuk''; it's just an uninteresting phrase, intended to make
no sense to the average comp.lang.c reader (sorry for that).
It means ``junk'' or ``old crap''.

This discussion is drifting way off topic. Time to quit?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top