Solved

calloc() and free()

Posted on 2001-08-15
1
541 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 60 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

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!

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…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

717 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