Memory free() in C

Hi guys.
I've a question, if we are using a syscall that receives a string allocated dynamicaly to a determined size, or NUL and it will allocate the apropriate size. We should free the memory or the OS will do it for us?
If a function returns a pointer we should free that poiter when we are done with it?

Thanks.

The interface should document memory management responsibilities.

1, Only memory allocated explicitly or implicitly by malloc() should be free() manually (explicitly or use wrapper of free).

2, If you don't free them, OS will recycle them when the process terminates. But this causes memory leak.

3, depend on they type of the memory. If it is static memory, you need not and you can't. If it is allocated by malloc() you need.

4, you should explicitly free() memory if you called strdup() because it is a wrapper of malloc(), and it doesn't give a wrapper of free().

1 Like

free(3) is not a system call.

from gnu.org GLIBC manual

1 Like

Thank you so much for your reply. I am working with your approach but I'm getting errors when I call free(), I allocated a portion of memory with malloc() and when I call free() I get a error , if I comment out the free() instruction the program runs without this error.
Does you or anyone knows the cause?

*** glibc detected *** ./prog: free(): invalid pointer: 0x000000000160fd2b ***
======= Backtrace: =========
/lib/libc.so.6(+0x7366a)[0x7f5aaa03a66a]
/lib/libc.so.6(cfree+0x6c)[0x7f5aaa03e54c]
./prog[0x40286f]
./prog[0x4028e2]
./prog[0x40136f]
/lib/libc.so.6(__libc_start_main+0xed)[0x7f5aa9fe817d]
./prog[0x400e29]
======= Memory map: ========
00400000-00404000 r-xp 00000000 08:02 671794                             /home/pharaoh/Development/c_lang/pa_ficha6/exer_2/prog
00603000-00604000 rw-p 00003000 08:02 671794                             /home/pharaoh/Development/c_lang/pa_ficha6/exer_2/prog
0160f000-01630000 rw-p 00000000 00:00 0                                  [heap]
7f5aa4000000-7f5aa4021000 rw-p 00000000 00:00 0 
7f5aa4021000-7f5aa8000000 ---p 00000000 00:00 0 
7f5aa9db1000-7f5aa9dc6000 r-xp 00000000 08:02 2565373                    /usr/lib/libgcc_s.so.1
7f5aa9dc6000-7f5aa9fc6000 ---p 00015000 08:02 2565373                    /usr/lib/libgcc_s.so.1
7f5aa9fc6000-7f5aa9fc7000 rw-p 00015000 08:02 2565373                    /usr/lib/libgcc_s.so.1
7f5aa9fc7000-7f5aaa11e000 r-xp 00000000 08:02 16446                      /lib/libc-2.14.so
7f5aaa11e000-7f5aaa31e000 ---p 00157000 08:02 16446                      /lib/libc-2.14.so
7f5aaa31e000-7f5aaa322000 r--p 00157000 08:02 16446                      /lib/libc-2.14.so
7f5aaa322000-7f5aaa323000 rw-p 0015b000 08:02 16446                      /lib/libc-2.14.so
7f5aaa323000-7f5aaa328000 rw-p 00000000 00:00 0 
7f5aaa328000-7f5aaa33f000 r-xp 00000000 08:02 16394                      /lib/libpthread-2.14.so
7f5aaa33f000-7f5aaa53f000 ---p 00017000 08:02 16394                      /lib/libpthread-2.14.so
7f5aaa53f000-7f5aaa540000 r--p 00017000 08:02 16394                      /lib/libpthread-2.14.so
7f5aaa540000-7f5aaa541000 rw-p 00018000 08:02 16394                      /lib/libpthread-2.14.so
7f5aaa541000-7f5aaa545000 rw-p 00000000 00:00 0 
7f5aaa545000-7f5aaa564000 r-xp 00000000 08:02 16467                      /lib/ld-2.14.so
7f5aaa73d000-7f5aaa740000 rw-p 00000000 00:00 0 
7f5aaa760000-7f5aaa763000 rw-p 00000000 00:00 0 
7f5aaa763000-7f5aaa764000 r--p 0001e000 08:02 16467                      /lib/ld-2.14.so
7f5aaa764000-7f5aaa765000 rw-p 0001f000 08:02 16467                      /lib/ld-2.14.so
7f5aaa765000-7f5aaa766000 rw-p 00000000 00:00 0 
7fff3cd3a000-7fff3cd5b000 rw-p 00000000 00:00 0                          [stack]
7fff3cdff000-7fff3ce00000 r-xp 00000000 00:00 0                          [vdso]
ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0                  [vsyscall]
Aborted

can you post the code around the free? Looks like your giving it an invalid address to free.

You must free() the original pointer returned by malloc() without any arithmetic option to it.

There are no such system calls. You might be confusing with library functions.