?
Solved

calloc() and free()

Posted on 2001-08-15
1
Medium Priority
?
543 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 16

Accepted Solution

by:
imladris earned 180 total points
ID: 6390296
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

Technology Partners: 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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
Suggested Courses

777 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