HELP: free an array...

R

Ralph

Hi all,
I have initialises an array and use it. After using it, I wanted to
free the array and it return me an error... may I know what is the main
cause?

eg:

float *a;

a = (float*)malloc(sizeof(float)*M);
..... codes ....
free(a); <--- cause an error...
 
T

Tom St Denis

Ralph said:
Hi all,
I have initialises an array and use it. After using it, I wanted to
free the array and it return me an error... may I know what is the main
cause?

eg:

float *a;

a = (float*)malloc(sizeof(float)*M);

Don't need cast and should use

a = malloc(sizeof(*a) * M);
.... codes ....

Do you change "a" in the code?
free(a); <--- cause an error...

If a does not point to what malloc returned that calling free is a no-no.

Tom
 
S

Scott Fluhrer

Tom St Denis said:
Don't need cast and should use

a = malloc(sizeof(*a) * M);


Do you change "a" in the code?


If a does not point to what malloc returned that calling free is a no-no.

Another thing that can often cause this symptom is writing past the end of
the allocated memory (that is, writing to a[M] or beyond), or before the
beginning (a[-1]).
 
T

Tristan Miller

Greetings.

I have initialises an array and use it. After using it, I wanted
to
free the array and it return me an error... may I know what is the
main cause?

eg:

float *a;

a = (float*)malloc(sizeof(float)*M);
.... codes ....
free(a); <--- cause an error...

Apart from the stylistic no-no of casting the return value of malloc()
(and possibly also taking the sizeof float instead of the sizeof *a),
the code you posted is correct. Obviously there's something in the
".... codes ...." part which is causing the error. Have you perhaps
modified the value of a between the malloc() and the free()?

Regards,
Tristan
 
A

Al Bowers

Scott said:
Don't need cast and should use

a = malloc(sizeof(*a) * M);



Do you change "a" in the code?



If a does not point to what malloc returned that calling free is a no-no.


Another thing that can often cause this symptom is writing past the end of
the allocated memory (that is, writing to a[M] or beyond), or before the
beginning (a[-1]).

The op is focusing on the free() statement as the cause of the error.
I typically find this error is cause by the value of variable a being
modified such that it no longer represents a value previously assigned
with the return of functions malloc, calloc, or realloc. Another
typical cause is an error in the logic with attempts to free the
varible more than once.

Examples:

-----------------------------------------------------

float *a, b = 2.1f;
....
a = malloc(*a); /* Assume success */
.....
a = &b;
free(a); /* ERROR: variable a no longer represent the malloc value.*/

-----------------------------------------------------

float *a, *b;
....
a = malloc(*a);
b = malloc(*b); /* Successfule allocations */
....
free(a);
.....
free(a); /* ERROR: Attempt to free variable a twice */

------------------------------------------------------

float *a, *b;
....
a = malloc(*a);
b = malloc(*b);
......
if(b = a) /* suble bug here, should be "==". */
.....
free(a);
free(b); /* ERROR: Since a = b there was an attempt to free a twice. */
 
R

Ravi Uday

Ralph said:
Hi all,
I have initialises an array and use it. After using it, I wanted to
free the array and it return me an error... may I know what is the main
^^^^^^ In your case *a* is not an array !!
cause?

eg:

float *a;

a = (float*)malloc(sizeof(float)*M);
.... codes ....
free(a); <--- cause an error...

No it doesnt on mine. Wheres your *main* program. Give a snippet of
the *compilable* code for the people in c.l.c to give you a solution.
 
T

Tom St Denis

Al Bowers said:
The op is focusing on the free() statement as the cause of the error.
I typically find this error is cause by the value of variable a being
modified such that it no longer represents a value previously assigned
with the return of functions malloc, calloc, or realloc. Another
typical cause is an error in the logic with attempts to free the
varible more than once.

You're both right. In many implementations heap info is stored at the end
or start of a block. If you overwrite it free() won't be able to
reconstruct the heap.

Tom
 
P

Peter Shaggy Haywood

Groovy hepcat Ralph was jivin' on Wed, 12 Nov 2003 10:28:01 +0800 in
comp.lang.c.
HELP: free an array...'s a cool scene! Dig it!
I have initialises an array and use it. After using it, I wanted to
free the array and it return me an error... may I know what is the main
cause?

float *a;

a = (float*)malloc(sizeof(float)*M);
.... codes ....
free(a); <--- cause an error...

FCOL, show us something that will compile! How are we supposed to
know what is causing your problem if we can't see the code that's
doing it?

--

Dig the even newer still, yet more improved, sig!

http://alphalink.com.au/~phaywood/
"Ain't I'm a dog?" - Ronny Self, Ain't I'm a Dog, written by G. Sherry & W. Walker.
I know it's not "technically correct" English; but since when was rock & roll "technically correct"?
 
D

Dave Thompson

Scott Fluhrer wrote:
Another thing that can often cause [free() to fail] is writing past the end of
the allocated memory (that is, writing to a[M] or beyond), or before the
beginning (a[-1]).

The op is focusing on the free() statement as the cause of the error.
I typically find this error is cause by the value of variable a being
modified such that it no longer represents a value previously assigned
with the return of functions malloc, calloc, or realloc. Another
typical cause is an error in the logic with attempts to free the
varible more than once.

Examples:

-----------------------------------------------------

float *a, b = 2.1f;
...
a = malloc(*a); /* Assume success */
....

Of Course You Meant malloc( sizeof *a );
a = &b;
free(a); /* ERROR: variable a no longer represent the malloc value.*/
and similarly for the other examples, snipped.

- David.Thompson1 at worldnet.att.net
 

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
474,104
Messages
2,570,643
Members
47,247
Latest member
youngcoin

Latest Threads

Top