Solved

Memory leak

Posted on 2000-03-28
8
268 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
>> 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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 

Author Comment

by:shayad
Comment Utility
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
Comment Utility
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
Comment Utility
Don't tell me free(*p) worked ???
0
 
LVL 8

Expert Comment

by:stochastic
Comment Utility
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
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.

772 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

13 Experts available now in Live!

Get 1:1 Help Now