ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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 )
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, 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."
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
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).
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.
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.