LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-24-2012, 05:37 PM   #1
devnull10
Member
 
Registered: Jan 2010
Location: Lancashire
Distribution: Slackware Stable
Posts: 572

Rep: Reputation: 120Reputation: 120
Freeing pointers to pointers


Hi all,
I've just a quick basic question about freeing up memory from pointers to pointers in c.

If I have a variable x and I do something like:

Code:
char ** x;
int i;

x=(char**)malloc(10*sizeof(char *));

for (i=0; i<10; i++) {
  x[i]=(char*)malloc(10*sizeof(char));
}
then at some point later in the program I want to free up that memory, is it sufficient to just:

Code:
free(x);
or do I have to first loop through all x[...] and free each element of that dimension before finally freeing x?

ie, does freeing up x automatically ripple through?

Thanks!
 
Old 07-24-2012, 06:21 PM   #2
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,780

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
Quote:
Originally Posted by devnull10 View Post
or do I have to first loop through all x[...] and free each element of that dimension before finally freeing x?
Yup. 1 malloc() --> 1 free()
 
Old 07-24-2012, 07:17 PM   #3
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,659
Blog Entries: 4

Rep: Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941
The "C" language knows nothing about what any particular memory block is "for."

If you allocated a memory block, you must subsequently free it. (To do that, you must know where it is and perhaps how big it is. How you manage to know that is up to you.)

Thus, if you are in the process of deallocating a block of memory that, known to you, contains "a table of pointers to allocated blocks," then rest assured that "C" has no idea that this is so. Having allocated those blocks, as well as the block which contains the table of pointers, you must free both.
 
Old 07-24-2012, 09:35 PM   #4
tushar_pandey
Member
 
Registered: Jun 2012
Location: ghaziabad , delhi , india
Posts: 105

Rep: Reputation: Disabled
Hi , i have a question ,

Quote:
Originally Posted by devnull10 View Post
Hi all,
I've just a quick basic question about freeing up memory from pointers to pointers in c.

If I have a variable x and I do something like:

Code:
char ** x;
int i;

x=(char**)malloc(10*sizeof(char *));

for (i=0; i<10; i++) {
  x[i]=(char*)malloc(10*sizeof(char));
}
then at some point later in the program I want to free up that memory, is it sufficient to just:

Code:
free(x);
or do I have to first loop through all x[...] and free each element of that dimension before finally freeing x?

ie, does freeing up x automatically ripple through?

Thanks!
Here in this program we are allocating memory ,
and at x=(char**)malloc(10*sizeof(char *)); this line we are creating a double pointer

and here ,
for (i=0; i<10; i++) {
x[i]=(char*)malloc(10*sizeof(char));
}

we are allocating 10 blocks ,

but if we write free(x) , than please tell me that all allocation are rubbed from the memory space . all means ( these 10 blocks + the double pointer )
 
Old 07-24-2012, 10:48 PM   #5
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,862
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
Certainly not. 11 malloc -> 11 free
 
Old 07-25-2012, 12:05 AM   #6
gchen
Member
 
Registered: May 2012
Location: Beijing China
Distribution: Asianux
Posts: 56

Rep: Reputation: Disabled
if call free(x), only x will be free, and left 10 are lost; this is called "Memory Leak".

you can not find the 10 pointer and memory again (if they are not saved in another variables);

And they still occupy the memory, but can not be used any more.

It is "Memory Leak"

: )
 
Old 07-25-2012, 01:07 AM   #7
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,842

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
yes, you need to free the elements of the array first and finally you can free the array itself.
you can use for example valgrind to find memory leaks.
 
Old 07-25-2012, 03:29 AM   #8
gchen
Member
 
Registered: May 2012
Location: Beijing China
Distribution: Asianux
Posts: 56

Rep: Reputation: Disabled
Smile

Quote:
Originally Posted by pan64 View Post
yes, you need to free the elements of the array first and finally you can free the array itself.
you can use for example valgrind to find memory leaks.
yes, truly have some tools for checking memory leak.

for user mode system service(mostly written by C, source code more than 100,000 lines), it suggests writting its own memory management module.

for running program itself, it is useless to find lost pointers, although we can truly find them, and also may know what it is if use own memory management module.

at last, commonly say: "we can detect memory leak by tools or our own source code, and the leaked memory are lost, we can not use them again."

: )
 
Old 07-25-2012, 06:07 PM   #9
devnull10
Member
 
Registered: Jan 2010
Location: Lancashire
Distribution: Slackware Stable
Posts: 572

Original Poster
Rep: Reputation: 120Reputation: 120
Hi,
Thank-you - the answer was as I expected.
 
Old 07-25-2012, 07:43 PM   #10
gchen
Member
 
Registered: May 2012
Location: Beijing China
Distribution: Asianux
Posts: 56

Rep: Reputation: Disabled
Smile

Quote:
Originally Posted by devnull10 View Post
Hi,
Thank-you - the answer was as I expected.
Happy to hear the news above.

these communications are also helpful for me: "learn with each other".

: )



by the way:

1) The "Garbage Collection" is the mechanism of memory management for helping releasing memory (it is truly relative with Memory Leak).

2) It is often used in another programming languages, such as JAVA, but it seems that it is not a quite suitable mechanism for them.

3) For C system programming, it is not recommanded. Instead of, we often use seperated processes (including monitor process) to bypass it.

: )
 
Old 07-26-2012, 12:52 AM   #11
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,842

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
Quote:
Originally Posted by gchen View Post
Happy to hear the news above.

these communications are also helpful for me: "learn with each other".

: )
in that case you (and OP) can add reputation..



Quote:
Originally Posted by gchen View Post
3) For C system programming, it is not recommanded. Instead of, we often use seperated processes (including monitor process) to bypass it.
: )
I do not really understand it. Garbage Collection works in JAVA, but not in C or C++. You need to free all the memory allocated in the program. There is no separated process to manage memory, but there are tools (partially linked/built into the app, and partially runs as another process) to analyze memory handling (but not to bypass memory management).
 
Old 07-26-2012, 02:26 AM   #12
gchen
Member
 
Registered: May 2012
Location: Beijing China
Distribution: Asianux
Posts: 56

Rep: Reputation: Disabled
Smile

Quote:
Originally Posted by pan64 View Post
I do not really understand it. Garbage Collection works in JAVA, but not in C or C++. You need to free all the memory allocated in the program. There is no separated process to manage memory, but there are tools (partially linked/built into the app, and partially runs as another process) to analyze memory handling (but not to bypass memory management).

My English is not quite good, maybe misunderstanding what I said.

1) for c programming, we need to free all the memory allocated in the program, or the Memory Leak.

2) "Garbage Coolection" can find the "Memory Leak", and release them.

3) For develop system service in C, we can implement "Garbage Collection" features by writing our own memory manager module.


And then:

1) I do not suggest to do that (implementing garbage collection features), it will make running system too complix (it isn't a good design).

2) For C programming, "Memory Leak" is a kind of Bug. but it is not quite easy to avoid, especially for system programming.

3) A simply and commonly way to bypass the "Memory Leak" effect is that design the running the system service with seperated processes (including a monitor process).


Can you understand what my explain(my english is not quit good)?

If you cannot understand or disagree with them, I will try to explain or disscus again.

: )
 
Old 07-26-2012, 03:00 AM   #13
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,842

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
Hi, I do not really agree:
memory leak means you never free allocated memory. There is no manager (in C or C++) which can decide if a memory is still allocated but will not be used any more, so can be freed. This problem is solved in java, that is the garbage collector - but in java you cannot allocate memory directly. The problem with the memory leak is the program eats up more and more memory and forgets to free up. A long running process can therefore allocate all the available resources and it will make the system unusable. When a process terminated all the resources will also be released, so the leaked memory will also be freed.
As you wrote, memory leak is a bug, and you must avoid leaking memory. A common way to find memory problems is a tool to analyze the program, for example valgrind or purify will find memory related problems (leaks can only be detected when the process terminates by identifying the non-freed, still allocated memory). In a living environment such tools are not available, not used, because they need a lot of resources also and they modify the behavior of the app.
 
Old 07-26-2012, 03:08 AM   #14
tushar_pandey
Member
 
Registered: Jun 2012
Location: ghaziabad , delhi , india
Posts: 105

Rep: Reputation: Disabled
pan64 ..

"but in java you cannot allocate memory directly" , is this related with dynamic allocation in c language , malloc or calloc functions !

or something else , please explain !
 
Old 07-26-2012, 03:17 AM   #15
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,842

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
In java there are classes, but there are no calloc, malloc or similar calls. Memory is allocated by instantiating classes (that is the constructor call). All the classes belong to a scope (that means part of the code, where it is visible and usable). When the program leaves the given scope all the classes will be automatically marked as unused and garbage collector will free that memory when needed.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Pointers Rameses Linux - Newbie 1 07-26-2010 12:03 PM
help with pointers yfaye Programming 4 04-01-2009 04:15 PM
pointers in c++ marios_auth Programming 1 06-16-2004 08:20 AM
Looking for some pointers... p3ngu!n Linux - Newbie 17 10-30-2003 06:46 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:37 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration