?
Solved

calloc() and free()

Posted on 2001-08-15
1
Medium Priority
?
549 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

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…
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 and use pointers in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.

649 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