Raj Pashwar said:
Sure, code is
#include<memory.h>
...
int* ip=calloc(4,ARYSIZ); // ARYSIZ denotes array size (read from file)
...
free(ip); // gives segmention fault
or
cfree(ip); // gives compile error
The Free works fine with other Malloc memory, but not this Calloc call :-(
No, that's not your code. That's a few scattered lines of your code
with "..." inserted between them. There's not enough information
here to tell what's causing the symptom you're seeing.
Try taking a copy of your actual code and removing piece of it,
narrowing it down to a small compilable program that exhibits the
problem you're seeing. You should be able to get it down to, say,
10-20 lines. You might even be able to solve the problem yourself
while you're doing this. If not, feel free to post a followup here.
But I can point out a few problems.
<memory.h> is not a standard header. The calloc and free functions
are declared in <stdlib.h>; you need to have "#include <stdlib.h>"
at the top of your source file. (There is a <memory.h> header on
my system, but I've never used it.)
Names in all-caps, like ARYSIZ, are usually macros; I'd expect
something like
#define ARYSIZE 1024
But you say it "denotes array size (read from file)". If it's a
variable, just as a matter of style and legibility, it shouldn't
have an all-caps name.
There is no cfree function, so you can stop worrying about it.
The correct way to release memory allocated by calloc() is to
call free().
Consider whether you really need to use calloc() rather than
malloc(). The main difference between them is that calloc() sets the
allocated memory to all-bits-zero. That's typically not a useful
thing to do; in most cases, you just shouldn't be accessing array
elements that you haven't assigned values to. That's not to say
that you should *never* use calloc(), just that you should think
about whether you need it.
Finally, please don't refer to the calloc and free functions as
"Calloc" and "Free". C identifiers are case-sensitive. "Calloc",
and "Free", if they existed, would be two completely separate
functions. (I'll refrain from digressing about C90 external
identifier rules.) Don't capitalize C function names; just call
them by their names, "calloc" and "free" -- even if they're at the
beginning of a sentence.