?
Solved

Memory leak

Posted on 2000-03-28
8
Medium Priority
?
274 Views
Last Modified: 2012-05-04
Hi,

Does this piece of code give rise to a memory leak.

  int *p = malloc( 100 );
  p++;
  p++;
   
  free(p);

Regards
Shayad
   









0
Comment
Question by:shayad
[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
  • 2
  • 2
  • 2
  • +2
8 Comments
 
LVL 8

Expert Comment

by:stochastic
ID: 2663460
shayad,

Of course it would give rise to a memory leak. Or worse! You are trying to free a pointer you never got through malloc. What you got was the original value of p, as returned by the malloc() call. What you are freeing is the pointer address p+2.

The 100-byte memory you got will NOT be freed. A table of pointers is maintained internally, along with their allocation. This (p+2) value will not be present in that table, so the freeing will not happen correctly. There could be a worse effect, but that would depend on the compiler.

When doing something like this, you must always preserve the original value of the pointer, so that at the end of the operation you can use that for the free() call. You could assign
q=p;
and then use q for your operations involving incrementing of the pointer
(q++ etc). The value of p should be left untouched, so that your free(p) does the correct thing to do at the end.

- stochastic
0
 

Accepted Solution

by:
hamiltonf earned 100 total points
ID: 2663519
Well yes you have a memory leek what you need to do is:
free (*p);

Then you free the value of the pointer and not the pointer itself.

when you free the pointer it self you have actually freed the way to the allocation place, by doing so you can't free it any more. You should be very careful when you free space so you won't loss the pointer to this space.

Please don't give me the points, I think that this is a question you got for home work, we try not to help in home work over here, but this time it was for progress. please don't give points and don't ask question from home work.

Thanks.
0
 

Expert Comment

by:nl001402
ID: 2663540
Try it and you will see.

Since a table of pointers is kept along with the size of the block however, it would be possible for a compiler to generate the correct code and have the block of memory deallocated properly.
0
Industry Leaders: 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!

 
LVL 7

Expert Comment

by:KangaRoo
ID: 2663639
>> free(*p)

Think you should read again. p is declared as
  int* p;

and you should
  free(p);

trying to
  free(*p);
will result in a compilation error since the compiler can not convert an int to a void*


Programm behaviour is 'undefined' when you free() memory that has not been allocated by malloc. In other words

int* p = malloc(100);
p++;
p++;
free(p);

May  
1. have no effect
2. launch you to the moon
3. crash the computer immidiately
4. crash the computer when you least expect it
0
 

Author Comment

by:shayad
ID: 2663773
Hey, this is no question from homework. I just found this piece of code porting an old application from Unix to windows.
0
 

Author Comment

by:shayad
ID: 2663774
Hey, this is no question from homework. I just found this piece of code porting an old application from Unix to windows.
0
 
LVL 7

Expert Comment

by:KangaRoo
ID: 2663861
Don't tell me free(*p) worked ???
0
 
LVL 8

Expert Comment

by:stochastic
ID: 2664046
Good grief! I find it hard to believe that free(*p) worked! I won't be able to sleep tonight. I have to re-learn my C!
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

765 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