473,670 Members | 2,563 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1902
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************ *************@p osting.google.c om> 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******@donte mailme.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
3849
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 service. So I need the Release mode EXE. I've checked the dlls being used . they are working fine for other services.
3
3326
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) HttpWebRequest wr = (HttpWebRequest)WebRequest.Create("http://www.microsoft.com/"); using (HttpWebResponse wrp = (HttpWebResponse)wr.GetResponse()) { using (Stream s = wrp.GetResponseStream())
10
1581
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 debug version but behaves strangely and sometimes crashes when I run it in release version. I suspect that it is some sort of memory problem but it is not that easy to find out what the problem exactly is (the project is rather big with maybe...
5
1931
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 used by the application seems to keep increasing . the code I use seems to be pretty straightforward QueueRowTable.Clear() The memory is never reclaimed. What do I need to do to release the memory to the system? Thanks for any help.
2
1641
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. Do we have to take care in .net for the memory allocated in the c++ dll (SysAllocStringByteLen ?). Or does .net on exiting handles this. How
2
3556
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 verse.(vs2005, /clr:oldSyntax) . UI layer are written in native C++. Currently, we meet a random crash bug in release build. It is ok in debug build. If we replace the managed c++ dll with the debug one, it works also. So we think there must be...
6
14397
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
2371
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 application is developed using C++/Managed C++ and built using VS 2003 under .NET framework 1.1. In Debug, it uses of up to 600Mb of memory, whereas in Release it only gets
2
2700
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 in("myfile.txt"); float value; in >value; //The crash happens here in the getloc() function The above code is actually from a library built in Debug mode that is linked into the Release build of the executable. Does anyone have any
7
2819
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 to memory model, i.e. relations between acquire/release fences; and between acquire/release fences and acquire/release operations.
0
8466
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8896
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8590
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8659
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5683
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4387
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2798
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2035
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1790
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.