Solved

Memory leak

Posted on 2000-03-28
8
269 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

911 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now