Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > release the memory help

Reply
Thread Tools

release the memory help

 
 
Rex_chaos
Guest
Posts: n/a
 
      10-07-2003
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.
 
Reply With Quote
 
 
 
 
Noah Roberts
Guest
Posts: n/a
 
      10-07-2003
Rex_chaos wrote:
> 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.
>
> Thanks in advance.


 
Reply With Quote
 
 
 
 
Robert W Hand
Guest
Posts: n/a
 
      10-07-2003
On 7 Oct 2003 01:59:07 -0700, http://www.velocityreviews.com/forums/(E-Mail Removed) (Rex_chaos) wrote:

>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
 
Reply With Quote
 
Mark Gordon
Guest
Posts: n/a
 
      10-07-2003
On 7 Oct 2003 01:59:07 -0700
(E-Mail Removed) (Rex_chaos) wrote:

> 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.
--
Mark Gordon
Paid to be a Geek & a Senior Software Developer
Although my email address says spamtrap, it is real and I read it.
 
Reply With Quote
 
Richard Bos
Guest
Posts: n/a
 
      10-07-2003
(E-Mail Removed) (Rex_chaos) wrote:

> 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
 
Reply With Quote
 
Dan Pop
Guest
Posts: n/a
 
      10-07-2003
In <(E-Mail Removed)> (E-Mail Removed) (Rex_chaos) writes:

>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
--
Dan Pop
DESY Zeuthen, RZ group
Email: (E-Mail Removed)
 
Reply With Quote
 
Alex
Guest
Posts: n/a
 
      10-07-2003
Noah Roberts <(E-Mail Removed)> wrote:
> Rex_chaos wrote:
>> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
convert "quick" release to "slow" release tripod head plate Mike Digital Photography 0 05-17-2009 11:01 AM
Adding quick-release to a non-quick-release tripod head ste7esmith@hotmail.com Digital Photography 4 11-20-2006 03:19 PM
J2ME: javac: target release 1.1. conflicts with default source release 1.5 John Goche Java 1 12-17-2005 08:12 PM
NEWS RELEASE: Leone's "Nobody" Films 1st time mastered in HD for DVD release with 240 Minutes Extras Torsten Kaiser \(TLEFilms\) DVD Video 1 06-06-2005 03:17 PM
GC does not release memory...memory keeps growing!!! Mahesh Prasad ASP .Net 1 02-22-2004 08:40 AM



Advertisments