Solved

calloc() and free()

Posted on 2001-08-15
1
535 Views
Last Modified: 2012-05-04
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
Comment
Question by:markdot
1 Comment
 
LVL 16

Accepted Solution

by:
imladris earned 60 total points
Comment Utility
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now