473,408 Members | 2,025 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

release the memory help

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.
Nov 13 '05 #1
6 1885
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.


Nov 13 '05 #2
On 7 Oct 2003 01:59:07 -0700, re*******@21cn.com (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
Nov 13 '05 #3
On 7 Oct 2003 01:59:07 -0700
re*******@21cn.com (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.
Nov 13 '05 #4
re*******@21cn.com (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
Nov 13 '05 #5
In <f7*************************@posting.google.com> re*******@21cn.com (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: Da*****@ifh.de
Nov 13 '05 #6
Noah Roberts <nr******@dontemailme.com> 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
Nov 13 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: ss | last post by:
Hi I built an exe in console debug mode & Release mode . The console debug mode -exe works fine ...but the release mode EXE starts well but fails mid way . The exe is being started as a...
3
by: Marc | last post by:
I am using Visual Studio 2003 with .NET Framework 1.x on XP SP2. I am executing the following code (see also http://dturini.blogspot.com/2004/06/on-past-few-days-im-dealing-with-som e.html) ...
10
by: babak | last post by:
Hi everybody I'm working with a project in embedded Visual Studio 4 and I have a general problem which I hope that somebody can help me with. My problem is the following: My project works fine in...
5
by: Rodrigue | last post by:
I have code where I populate a data table using visual basic.net code. I call the clear method on the table but it does not seem to release the memory. When I populate the table again, the memory...
2
by: sanu | last post by:
Hi, Need your help, >From our vb.net web service we are calling a unmanaged C++ win32 dll function. The dll function takes a string (consists of huge data) parameter byref. Below code sample....
2
by: kevinding | last post by:
Hi All, We meet an evil condition for our project. Our project has 3 layers. A C# layer to do some business logic, and Managed C++ layer translate managed values to native ones or vice...
6
by: laikon | last post by:
Hi, everyone, below is my program to test static pointer data member; class A { private: static A* p; protected: A() {} public: static A* init()
2
by: Epetruk | last post by:
Hello, I have a problem where an application I am working on throws an OutOfMemoryException when I run it in Release mode, whereas it doesn't do this when I am running in Debug. The...
2
by: Dave Johansen | last post by:
I just converted a solution from Visual Studio 2003 to Visual Studio 2005 and the Debug mode seems to be running just fine, but the Release mode crashes on the following code: std::ifstream...
7
by: Dmitriy V'jukov | last post by:
On Jun 16, 3:09 pm, Anthony Williams <anthony....@gmail.comwrote: Yes, I've already read this. It's just GREAT! It's far more useful and intuitive. And it contains clear and simple binding...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.