Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6130613
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T16:52:43+00:00 2026-05-23T16:52:43+00:00

I always have this doubt in mind. Please see the below program: #include <stdio.h>

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T16:52:43+00:00Added an answer on May 23, 2026 at 4:52 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Ok, this is a doubt I always have when I'm writing any API, a.k.a.,
I always had this silly doubt in mind but never had come up with
I always have this notion that writing SQL queries in the code behind is
I always have heard this motto from many entrepeneurs: do what you are passionate
I have always found this to be a very useful feature in Visual Studio.
I always have trouble with complicated SQL queries. This is what I have $query
Right now I have this SQL query which is valid but always times out:
Is there builtin method that would do this or do I always have to
This seems like a pretty softball question, but I always have a hard time
Is this a valid http url http://www.example.com?x&y or we must always have = sign

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.