473,386 Members | 1,798 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Dynamic memory allocation.

51
HI,
I am not sure whether this code is legal... Its works fine, but I am afraid that this is not a valid thing to be done. I am freeing the returned dynamically allocated memory in the main function. In the following code below, Is the freeing of memory in either of the functions is the same, or else there is a memory leak?.

Expand|Select|Wrap|Line Numbers
  1.  
  2. char * foo(void){
  3. char *a = (char*)malloc(sizeof(char)*2);
  4. return a;
  5. }
  6.  
  7.  
  8. int main(){
  9.  
  10. char *p = foo();
  11. //do some thing with 'p' here....
  12. free p;
  13. return 0;
  14. }
  15.  
  16.  

Thanks,
Gsi.
Sep 17 '07 #1
5 1289
Ganon11
3,652 Expert 2GB
Seems fine to me.
Sep 17 '07 #2
gsi
51
Hi,
Thanks for the quick reply. Since I am freeing a copy of pointer, but eventually it is the same in value context, is it the same as freeing the original pointer ?.

Thanks in advance,
Gsi.
Sep 17 '07 #3
ilikepython
844 Expert 512MB
Hi,
Thanks for the quick reply. Since I am freeing a copy of pointer, but eventually it is the same in value context, is it the same as freeing the original pointer ?.

Thanks in advance,
Gsi.
I believe so, yes. When you free a pointer you are freeing the memory allocated at the location in which the pointer points. So you are freeing the same memory you allocated in the function because you preserved the address.
Sep 17 '07 #4
weaknessforcats
9,208 Expert Mod 8TB
Be aware that after you free a pointer the memory it pointed at is up for grabs. Should there be another pointer in the program pointing at that memory location, the program will crash if you attempt to free the other pointer.

You can't free unless you are certain no copies of the address in the pointer are tucked away in the program somewhere.
Sep 17 '07 #5
gsi
51
Hi,


[QUOTE : Weaknessforcats]You can't free unless you are certain no copies of the address in the pointer are tucked away in the program somewhere.[/quote]

[QUOTE ilikepython]
When you free a pointer you are freeing the memory allocated at the location in which the pointer points. So you are freeing the same memory you allocated in the function because you preserved the address.[/quote]


Thanks guys, for clearing it up.


Thanks,
gsi.
Sep 17 '07 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

9
by: Tom | last post by:
What I mean is why can I only allocate const size stuff on the stack in C++? If I want to allocate a variable amount I need to use the OS API (Win32 in my case). Thanks, Tom.
6
by: chris | last post by:
Hi all, I need to know, what is the difference between dynamic memory allocation, and stack allocation ? 1. If I have a class named DestinationAddress, when should I use dynamic memory...
6
by: Sandeep Chikkerur | last post by:
Hi, If the entire heap memory for dynamic allocation is not available, does the compiler always return NULL ? eg: char *s; s = (char *)malloc(...);
13
by: xian_hong2046 | last post by:
Hello, I think dynamic memory allocation is supposed to be used when one doesn't know in advance how much memory to allocate until run time. An example from Thinking in C++ is to dynamically...
11
by: toton | last post by:
Hi, I have little confusion about static memory allocation & dynamic allocation for a cluss member. I have class like class Bar{ public: explicit Bar(){ cout<<"bar default"<<endl; }
24
by: Ken | last post by:
In C programming, I want to know in what situations we should use static memory allocation instead of dynamic memory allocation. My understanding is that static memory allocation like using array...
1
by: Peterwkc | last post by:
Hello all expert, i have two program which make me desperate bu after i have noticed the forum, my future is become brightness back. By the way, my problem is like this i the first program was...
3
by: ranjeetasharma81 | last post by:
Hi all, I have a big C-cod, in which there are lots of dynamic memory allocation used. I want to replace dynamic memroy allocation by static arrays. The following are the problems that i am...
14
by: vivek | last post by:
i have some doubts on dynamic memory allocation and stacks and heaps where is the dynamic memory allocation used? in function calls there are some counters like "i" in the below function. Is...
10
by: swornavidhya.mahadevan | last post by:
Which allocation (Static / Dynamic) is suitable for the situation when we are trying to allocate for a overloaded memory when the memory is full and no space to allocate. What will happen if both...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.