• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 553
  • Last Modified:

calloc() and free()

In a project, function1 calls function2 in another file:

in file1:
void function1()
{
for (i=1; i<5; i++)
function2();
}

in file2:
float *point1;
function2()
{
point1 = (float *) calloc(number1, sizeof(float));
...
free(point1);
}

Any comment on this code and can you tell me that after free(point1), can point1 be calloc() again?
0
markdot
Asked:
markdot
1 Solution
 
imladrisCommented:
Nope.
Yup.

There is no apparent conceptual problem with this code. It is intended that a pointer be capable of being set to point to allocated memory. Then you can free that memory. Then you can set the pointer to point to new allocated memory. The issue with lots of dynamic allocation is to make sure that there is a corresponding "free" for every "calloc". If you miss a free, then you will have a memory leak.

0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now