473,385 Members | 1,615 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,385 software developers and data experts.

Dynamic Memory Allocation-Urgent

I am writing a program for portable devices hence memory management is a big constraint.

To my knowledge only if u free the dynamically allocated memory it can be used further..

My doubt is
I have a function which is returing a pointer.
pointer is dynamically allocated .....if I free the pointer before returning then it is useless or dangling pointer problem arises....or if I free after returning the ptr the control is lost.........

So when to free the memory............


eg
:
char* example (int )
{
char *filepath,*filename;
filepath=(char*)malloc(100));


return filepath; ///Where to free the filepath memory
}
May 5 '08 #1
6 1663
oler1s
671 Expert 512MB
.if I free the pointer before returning then it is useless or dangling pointer problem arises....or if I free after returning the ptr the control is lost
Yes, very good, you see why passing pointers are a pain. You have a pointer that indicates no ownership semantics. This is how C is. Maybe you could come up with some GC or memory management system, but...either way you still have ugly pointers lying about.

Really, this is how C is. You're stuck passing around pointers. Rely on some good programming practices and a good design and you'll survive the pointer mess.
May 5 '08 #2
JosAH
11,448 Expert 8TB
Yes, very good, you see why passing pointers are a pain. You have a pointer that indicates no ownership semantics. This is how C is. Maybe you could come up with some GC or memory management system, but...either way you still have ugly pointers lying about.

Really, this is how C is. You're stuck passing around pointers. Rely on some good programming practices and a good design and you'll survive the pointer mess.
... as they attempt to do in Objective C: define an owner for an allocated piece
of memory. The owner is responsible for the memory, i.e. it should delete/free
it when the memory is no longer needed.

Allocated memory can change ownership (as in the OP's original example) and
the new owner takes the responsibility for the memory. This 'conceptual'
ownership helps you out in that tangled mess of pointers and allocated memory
except for circular references; a third conceptual owner has to be defined for
the entire circular shebang.

kind regards,

Jos
May 5 '08 #3
... as they attempt to do in Objective C: define an owner for an allocated piece
of memory. The owner is responsible for the memory, i.e. it should delete/free
it when the memory is no longer needed.

Allocated memory can change ownership (as in the OP's original example) and
the new owner takes the responsibility for the memory. This 'conceptual'
ownership helps you out in that tangled mess of pointers and allocated memory
except for circular references; a third conceptual owner has to be defined for
the entire circular shebang.

kind regards,

Jos

I am new to c...so kindly help me in defining a owner for a memory
May 5 '08 #4
JosAH
11,448 Expert 8TB
I am new to c...so kindly help me in defining a owner for a memory
It's all a conceptual notion so you can blurr or ignore it whatever you want. An owner
creates and/or deletes allocated memory when it doesn't expose it to others or
when the others just use the memory, as in:

Expand|Select|Wrap|Line Numbers
  1. char* f() { 
  2.    char* cp= malloc(42);
  3.    /* do something with cp[0], cp[1], ... */
  4.    g(cp); * expose it to something else */
  5.    /* but keep ownership */
  6.    /* do something with cp[0], cp[1], ... */
  7.    return cp; /* pass ownership to something else */
  8. }
  9. void g(char* cp) {
  10.    /* do something with the memory but don't release it */
  11.    /* do something with cp[0], cp[1], ... */
  12.    /* g() is not the owner of it */
  13. }
  14. void h() {
  15.    char* cp= f();
  16.    /* now h() is the owner of the memory */
  17.    /* do something with cp[0], cp[1], ... */
  18.    /* so if it's done with it, release it */
  19.    free(cp);
  20. }
  21.  
In this little scenario f() was the owner of the memory but it gave ownership of it
to function h() when it returned; h() frees the memory at the end because it was
the new owner of it; function g() just used the memory so it doesn't free it because
it wasn't the owner.

In C you have to keep track of the 'ownership' yourself; C allows you to shoot
yourself in the foot big times when you don't take care. In the example above it
may seem trivial but in larger real world code things might get tricky. Keeping
track of that ownership might help you to keep things organized.

kind regards,

Jos
May 5 '08 #5
weaknessforcats
9,208 Expert Mod 8TB
Why is C a requirement? If you can use C++, then you can use handles and avoid this mess.
May 5 '08 #6
JosAH
11,448 Expert 8TB
Why is C a requirement? If you can use C++, then you can use handles and avoid this mess.
Yep, and if the OP had used Java the garbage scavenger had taken care of it all.

kind regards,

Jos ;-)
May 5 '08 #7

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

Similar topics

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...
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...
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...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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.