C Memory cleanup struct array

Hi there!
I'm not quite sure how to clean up memory, and i don't wanna have a mem leak.


1
2
3
4
5
6
7
8
9
10
11
12
13
  
typdef struct(
int bla;
void *blabla;
)foo;

int main(){
 foo *bar;
 bar=malloc(sizeof(foo)*20);
 .
 .
 . 
 free(bar);


this /should/ free the allocated memory, right?
or does it only free the pointer, and i need to clean up the rest with a loop?

P.S. realloc(bar) should work, too?

(i hope so)
Last edited on
Use valgrind to determine memory leaks in your program.

And no, realloc won't clean up the memory. All it does is reallocate the memory. As an example, if you have a pointer bar that points to 20 structs of type foo and you realloc, you could change how many structs you have room for. So you could make bar point at 40 foos, or 100 foos. The trick is that realloc is expensive, because it always creates contiguous memory, so it would probably be better to just use a linked list, depending on what you're doing with it.

Yes, though, your code should (on a very limited level) work for freeing memory. You might want to error-test it, though:
1
2
3
foo * bar = NULL;
if(!(bar = malloc(sizeof(foo) * x)))
        return ENOMEM;

You will, of course, need far more robust error testing than that if you want to use multi-dimensional arrays, or if you want to actually distribute your code.
I don't realy want to redsitribute, but i don't realy have muhc knowlegda about the behavior of Malloc and free with structs and functions.

is there some tutorial on memory handling?

I'm only learnign C vie teh intarwebz, but that issue is kinda hard to find infos about.

and I kinda dislike not knowing enough about that. :/
btw: its not gonne be long arrays; less than 30 members. I doubt that linked lists wiould be that much better.
Last edited on
The program you posted is correct. Here, you can further read up on malloc and free:

http://www.cplusplus.com/reference/cstdlib/malloc/?kw=malloc
http://www.cplusplus.com/reference/cstdlib/free/?kw=free
Linux man pages about malloc: http://man.he.net/?topic=malloc&section=all They're hard to interpret if you're not used to it, but you should be able to glean some information from it each time you look until you've got a good understanding.

Online C tutorial: http://www.codingunit.com/c-tutorial-the-functions-malloc-and-free I can't vouch for this site, but it looked fairly decent at a glance.

Stack overflow: http://stackoverflow.com/questions/1963780/when-should-i-use-malloc-in-c-and-when-dont-i#1963812 Read the accepted answer for some extra insight.

cplusplus.com reference page: http://www.cplusplus.com/reference/cstdlib/malloc/ This has handy information in it, and although it's not really comprehensive, the other three sites should give you a good handle on the situation.

Last, but not least: Valgrind only works in linux. Check this out for a memory testing tool. Again, I'm not sure of how good they are. http://stackoverflow.com/questions/413477/is-there-a-good-valgrind-substitute-for-windows
In the code you showed you allocated memory only for 20 items of type foo.

bar=malloc(sizeof(foo)*20);

You did not allocate memory for structure data member void *blabla;
If you would allocate memory for this data member you should also free this memory before freeing memory for an item of type foo.
Last edited on
Topic archived. No new replies allowed.