Solved

Memory leak

Posted on 2000-03-28
8
270 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
  • 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 50 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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
how to convert c++ code to Android App 3 107
Dynamically allocate memory 9 62
Safe conversion? 4 72
How to Correctly derive class from CWinThread in MFC 4 64
Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
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 clear a vector as well as how to detect empty vectors in C++.

790 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