1

I always have this doubt in mind. Please see the below program:

#include <stdio.h>
char * function1(void);
int main()
{
    char *ch;
    ch = function1();
    printf("hello");
    free(ch);
    return 0;
}

char* function1()
{
    char *temp;
    temp = (char *)malloc(sizeof(char)*10);
    return temp;
}

am i leaking the memory here? the program does not crash in ideone with some warnings:

prog.c: In function ‘main’:
prog.c:11: warning: implicit declaration of function ‘free’
prog.c:11: warning: incompatible implicit declaration of built-in function ‘free’
prog.c: In function ‘function1’:
prog.c:19: warning: implicit declaration of function ‘malloc’
prog.c:19: warning: incompatible implicit declaration of built-in function ‘malloc’

and prints hello.

I am just a beginner in C.so please help me understand what happens after the return statement in function1.does free really frees the memory allocated in funtion1?

Bo Persson
  • 90,663
  • 31
  • 146
  • 203
Vijay
  • 65,327
  • 90
  • 227
  • 319
  • Your code my be in error because of 1) cast the return value of malloc; 2) failure to include the proper header. In this specific case, the compiler assumes `malloc` returns an int, converts that `int` to a `char*` because of the cast (**and doesn't complain which is the error**) then assign the converted value to the `temp` object. The conversion from `int` to `char*` is an error. The compiler would have caught it without the cast and with `#include ` – pmg Jul 10 '11 at 09:45

4 Answers4

7

Memory leaking

You are code isn't leaking any memory because you do free(ch); which free's memory allocated by the malloc inside the function1 function. You can check this by printing the pointer addresses, i.e.:

char* function1()
{
  char *temp;
  temp=(char *)malloc(sizeof(char)*10);
  printf("temp: %p\n", temp);
  return temp;
}

and

ch = function1();
printf("ch: %p\n", ch);

You should see that both prints (ch and temp) will print the same address. Thus, free(ch); will free the correct malloced chunk of memory.

You can use valgrind too check if your code doesn't free allocated memory.

About the warnings

Functions free, malloc are defined at stdlib.h.

Add this in your code:

#include <stdlib.h>
#include <stdio.h>
...

Also, it's not such a good idea to cast malloc return value temp=(char *)malloc(...);. Have a read here.

Community
  • 1
  • 1
insumity
  • 5,311
  • 8
  • 36
  • 64
  • i understand your answer..but my question is whether its leaking the memory or not? – Vijay Jul 10 '11 at 06:23
  • as u said i have added the stdlib.h.the warnings are gone now.but what is happening after the return statement? – Vijay Jul 10 '11 at 06:27
  • @Luzhin: Why do you want him to add stdio.h twice. That's just not required because he already has it in his code. Has the implementation of C changed? – Programmer Jul 10 '11 at 06:27
  • 1
    @Programmer ... I don't want him to add stdio.h twice. Why are you saying this? – insumity Jul 10 '11 at 06:30
  • My doubt is when i return temp.temp is local to the function1 and it becomes out of scope.so does the address of the memory allocated is safely passed to the main and safely freed or not? – Vijay Jul 10 '11 at 06:32
  • @vijay: yes it is safely passed and freed as you have the address to the data – Programmer Jul 10 '11 at 06:58
  • @Luzhin: in your asnwer u tell him to add stdio.h when he already has that – Programmer Jul 10 '11 at 07:04
  • on x86 architectures the return value of a function is usually returned in the eax register (except for floating point numbers); once the content of the memory location which holds the pointer given by malloc gets "copied" into eax, the stack frame of the function can safely go away. you're returning the value kept in temp, not the address of temp (this would be an error). – ShinTakezou Jul 10 '11 at 09:07
2

You need to include stdlib.h to use free and malloc.

It just happens to not matter what free and malloc actually do in your code above, so it still works.

trutheality
  • 23,114
  • 6
  • 54
  • 68
0

Yes, free frees the memory allocated in function 1 and gives it back to the free memory pool so that it can be reused. this is important because if you dont free memory you can reach in a situtation where your RAM is full and calls to malloc fail.

Do include stdlib.h as it contains definition for malloc.

Also, if you use free, you are not leaking memory

Programmer
  • 6,565
  • 25
  • 78
  • 125
0

Suggested alternative:

#include <stdio.h>
#include <malloc.h>

char * function1(void);

int
main(int argc, char *argv[])
{
  char *ch = function1();
  if (!ch)
    printf("ERROR: Unable to allocate memory!\n");
  else
  {
    printf("Allocation successful.\n");
    free(ch);
  }
  return 0;
}

char*
function1()
{
  return (char *)malloc(10);
}

Compile:

gcc -Wall -pedantic -o x x.c (where "x.c" is the source file, and "x" the .exe)

paulsm4
  • 114,292
  • 17
  • 138
  • 190