release the memory help

R

Rex_chaos

I have allocated a array like

double a[1024];

We know that a 1-D array just like an 1-D pointer. So I wonder that
should I release the memory of the array myself like

free(a);

Thanks in advance.
 
N

Noah Roberts

Rex_chaos said:
I have allocated a array like

double a[1024];

We know that a 1-D array just like an 1-D pointer. So I wonder that
should I release the memory of the array myself like

free(a);

No, a[] is automatic.

a is not just like a "1-D" pointer either, there are subtle differences;
I believe this is explained in detail in the C-FAQ.
 
R

Robert W Hand

double a[1024];

We know that a 1-D array just like an 1-D pointer. So I wonder that
should I release the memory of the array myself like

free(a);

An array is not the same as a pointer. In many contexts an array may
decay into a pointer, but they are not just like each other.

In particular free(a) is undefined. You can only pass free a null
pointer or a pointer that was generated by an earlier call to malloc,
calloc or realloc. If any other value is passed to free, or if free
is called for an argument that has already been deallocated; the
behavior is undefined.

Best wishes,

Bob
 
M

Mark Gordon

On 7 Oct 2003 01:59:07 -0700
I have allocated a array like

double a[1024];

We know that a 1-D array just like an 1-D pointer. So I wonder that

It's similar to, but it is NOT the same and the differences are
important.
should I release the memory of the array myself like

free(a);

You only have to free memory allocated by malloc and friends, or
functions which use them such as the non-standard strdup function.
Arrays follow the same rules as any other variable when it comes to
storage duration so you DON'T free them any more than you free an
integer variable.
 
R

Richard Bos

I have allocated a array like

double a[1024];

We know that a 1-D array just like an 1-D pointer.

You don't know that. You may think that, but you're mistaken. (You're
not alone in this, though; it's a very widely propagated myth.) An array
is really nothing like a pointer, it just mimics one in some, often-
encountered, circumstances. Behind the scenes, they're really quite
dissimilar.
For example, here you've asked for memory for 1024 doubles. You're
either getting it or not; you haven't got any error recovery. You cannot
make this memory "point to" another address. The thing is where it is.

Had you done this:

double a*;

a=malloc(1024 * sizeof *a);

you'd have asked for two bits of memory: one for the pointer itself, and
one for the 1024 doubles. The first bit is much like the array: it is a
single, unmovable object. The second bit, though, is more flexible. If
there isn't enough memory for 1024 doubles, you can recover from this,
because a is now a null pointer and you can check for that. If you want
more, you can realloc(). If you want a to point at another block of
doubles, you can point it there. It's much more flexible; but that
flexibility comes at the price of having to control it.
So I wonder that should I release the memory of the array myself like

free(a);

Certainly not. You only do that with pointers you got from any of the
*alloc() functions.

Richard
 
D

Dan Pop

In said:
I have allocated a array like

double a[1024];

We know that a 1-D array just like an 1-D pointer.

After reading the FAQ, we no longer "know" that! And we're supposed to
read the FAQ *before* posting!
So I wonder that
should I release the memory of the array myself like

free(a);

NEVER pass to free() a pointer value that was not obtained from one of
the *alloc functions.

There are three kinds of memory allocation in a C program:

Static allocation: the memory is allocated for the entire duration of the
program execution and there is nothing you can do about it.

Automatic allocation: the memory is allocated for the entire duration of
the block containing the object definition and automatically deallocated
when the program execution quits the block. Again, no programmer
intervention is possible.

Dynamic allocation: the memory is allocated as a result of on an *alloc
function call and stays allocated until the program calls free with the
pointer value returned by the corresponding *alloc function call (or
until it is reallocated with a realloc function call) or until program
termination.

Dan
 
A

Alex

Noah Roberts said:
Rex_chaos said:
I have allocated a array like

double a[1024];

We know that a 1-D array just like an 1-D pointer. So I wonder that
should I release the memory of the array myself like

free(a);
No, a[] is automatic.

.... or static.
a is not just like a "1-D" pointer either, there are subtle differences;
I believe this is explained in detail in the C-FAQ.

Not subtle at all. They are equivalent only in the formal function
parameter context.


Alex
 

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

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top